Android activity over lock screen - android

I need help with my project , I'm supposed to make a BroadcastReceiver fires up a full screen activity over current lock screen .
I tried this on my activity :
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.i("onCreate","success");
PowerManager pm = (PowerManager) this.getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock wakeLock = pm.newWakeLock((PowerManager.SCREEN_BRIGHT_WAKE_LOCK | PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP), "TAG");
wakeLock.acquire(10*60*1000L /*10 minutes*/);
getWindow().addFlags(
WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED |
WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD |
WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON |
WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
this.setShowWhenLocked(true);
this.setTurnScreenOn(true);
setContentView(R.layout.activity_stop);
}
BroadcastReceiver:
Intent i = new Intent(context,StopTheAlarm.class);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
AndroidManifest.xml:
<activity android:name=".Stop"
android:showOnLockScreen="true"
android:screenOrientation="sensorPortrait">
</activity>
but it doesn't work on my android 7.0.1 phone

Update :
i removed
this.setShowWhenLocked(true);
this.setTurnScreenOn(true);
which were causing app to crash

Your solution won't work on Android API 27 and later. Use code from this thread.

Related

How To Show Activity On Lock Screen When Password Is Set In Android

I am trying to show an activity when the screen is locked. I am running a service in the background, when an event occurs, I want to open an activity even if the app is locked(Similar to alarm app, which wakes up the screen and displays its activity). I have followed the following steps,
When OnReceive() is called, I want to open the activity on the lock screen.
public void OnReceive() {
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP, "myalarmapp:alarm.");
wl.acquire(5000);
Intent startAlarmActivity = new Intent(MainScreen.this, AcceptScreen.class);
startActivity(startAlarmActivity);
wl.release();
}
Added below code in the activity's onCreate method which I want to show,
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O_MR1) {
setShowWhenLocked(true);
setTurnScreenOn(true);
KeyguardManager keyguardManager = (KeyguardManager) getSystemService(Context.KEYGUARD_SERVICE);
keyguardManager.requestDismissKeyguard(this, null);
}
else {
getWindow().addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD |
WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED |
WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON |
WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
}
Added this in manifest for the activity which I want to show,
<activity
android:name=".v2.ui.orderaccept.AcceptScreen"
android:exported="true"
android:label="#string/title_activity_accept_screen"
android:theme="#style/AppTheme.NoActionBar"
android:showOnLockScreen="true"
android:screenOrientation="sensorPortrait"/>
It is working as expected when the phone does not have a lock screen password. But it does not work when there is a password for the lock screen.
Finally, I got the solution.
add below code in the activity's onCreate method which you want to show on the lock screen,
final Window win = getWindow();
win.addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
win.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
| WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON
| WindowManager.LayoutParams.FLAG_ALLOW_LOCK_WHILE_SCREEN_ON);
Manifest,
<activity
android:name=".v2.ui.orderaccept.AcceptScreen"
android:exported="true"
android:label="#string/title_activity_accept_screen"
android:theme="#style/AppTheme.NoActionBar"
android:launchMode="singleInstance"/>
Call the activity,
val intent = Intent(context, targetclass)
intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK)
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
startActivity(intent)

Launch Activity from FCM

In my application, i need to launch an activity and turn screen on, when high-priority FCM message is received. It's an alarm activity, which is very, very important for users.
On most Android devices, the code is working fine. However, on some Huawei or LG devices, the activity is not launched when the device is in Doze mode, or in the pocket (proximity sensor). The behaviour should be similar like Alarm clocks, calls etc.
Here is my code:
FirebaseMessagingService:
#Override
public void onMessageReceived(RemoteMessage remoteMessage) {
Intent dialogIntent = new Intent(getBaseContext(), AlarmActivity.class);
dialogIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
dialogIntent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
dialogIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
dialogIntent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND);
getApplication().startActivity(dialogIntent);
}
Alarm Activity:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// set flags so an activity fire on the screen
getWindow().addFlags(
WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD |
WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON |
WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED |
WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON
);
setContentView(R.layout.activity_alarm);
.
.
}
I wanted to use SCREEN_BRIGHT_WAKE_LOCK before I launch an activity, but it's deprecated.
I am using in my activity this code:
#Override
protected void onCreate(Bundle savedInstanceState) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
setShowWhenLocked(true);
setTurnScreenOn(true);
KeyguardManager keyguardManager = (KeyguardManager) getSystemService(Context.KEYGUARD_SERVICE);
if (keyguardManager != null && Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
keyguardManager.requestDismissKeyguard(this, null);
}
} else {
//noinspection deprecation
getWindow().addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD |
WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED |
WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON
);
}
}
.....
}
To prevent different behavior on different devices I use in my activity layout this:
<android.support.design.widget.CoordinatorLayout
....
android:keepScreenOn="true"
>

AlarmManager awakes Activity from Sleep but the Activity Closes Undesirably

I have an AlarmManager setting an alarm:
Intent intent = new Intent(mContext, AwakeActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent pendingIntent = PendingIntent.getActivity(mContext, PENDING_INTENT_ID, intent, PendingIntent.FLAG_CANCEL_CURRENT);
AlarmManager alarmManager = (AlarmManager) mContext.getSystemService(mContext.ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + futureTime, pendingIntent);
and the alarm works as expected. When the alarm goes off I have AwakeActivity open. This also works as expected. In AwakeActivity I have:
#Override
public void onCreate(Bundle savedInstanceState)
{
// main
super.onCreate(savedInstanceState);
// inflate
setContentView(R.layout.awake);
Window window = this.getWindow();
window.addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD); //relates to IInputConnectionWrapper issue - but not cause of instant close of app
window.addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
window.addFlags(WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
window.addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
}
Here is my Manifest definition:
<activity
android:name=".AwakeActivity"
android:label="#string/activityAwake"
android:configChanges="keyboardHidden|orientation"
android:theme="#android:style/Theme.NoTitleBar"
android:screenOrientation="portrait"
android:launchMode="singleInstance"
android:noHistory="true"
>
</activity>
The issue I'm having is that when AwakeActivity starts up it doesn't stay open. It instantly closes. But... this only happens if the phone is in sleep mode, if I leave the screen on and AwakeActivity opens via the alarm - all is well.
The other but... is that this happens on my Droid X running 2.3.4 and not my Galaxy Nexus 4.2.2. I've been at this for a while and feel as though I've done things properly.
What am I missing here? Any ideas?
I myself was running into a similar issue. On some devices the device would wake up and on others the activity would close. Have you tried using a WakeLock to see if the same thing happens?
So declare a global wakelock variable:
PowerManager.WakeLock wakelock;
In your onCreate method do something like:
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
wakelock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK
| PowerManager.ACQUIRE_CAUSES_WAKEUP | PowerManager.SCREEN_BRIGHT_WAKE_LOCK
| PowerManager.ON_AFTER_RELEASE, "tag");
wakelock.acquire();
Now you just have to release the wakelock, say in your onPause method:
#Override
protected void onPause() {
super.onPause();
wakelock.release();
}
Also you need to include this in your manifest file:
<uses-permission android:name="android.permission.WAKE_LOCK"/>

Android: Which events do wakeup a device

I have written an android application and now there is a problem.
If my application is running and I power off my device, then sometimes it just wakes up after about 8 seconds and then the screen is on and it shows my application again.
My question is:
Which events or processing constraints (like handling broadcast intents) do wakeup a device?
Edit: Will an alarm with RCT_WAKEUP turn on the device screen?
Thanks in advance!
The AlarmManager won't actually turn the screen on for you. You can use a wakelock instead.
PowerManager.WakeLock wakelock;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
.........
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
wakelock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK
| PowerManager.ACQUIRE_CAUSES_WAKEUP | PowerManager.SCREEN_BRIGHT_WAKE_LOCK, "AlarmReceiver");
wakelock.acquire();
}
When you're done with the activity just be sure to release the wakelock:
#Override
protected void onStop() {
super.onStop();
wakelock.release();
}
You also need to add the wake lock permission in your manifest file:
<uses-permission android:name="android.permission.WAKE_LOCK"/>
Alternatively, if you don't want to have to include this extra permission you can use the following code in your activity instead of using a wakelock:
this.getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN |
WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD |
WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED |
WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON |
WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

Turning on screen programmatically

I would like to unlock screen and switching it on to show a popup on an event trigger. I am able to unlock the screen using
newKeyguardLock = km.newKeyguardLock(HANDSFREE);
newKeyguardLock.disableKeyguard();
on KeyGuardService but I cannot turn on the screen. I am using
wl = pm.newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK, HANDSFREE);
wl.acquire();
but with no success. The screen still remains off.
How can I achieve this?
Note from author: I wrote this back in 2012. I don't know if it works anymore. Be sure to check out the other more recent answers.
Amir's answer got me close, but you need the ACQUIRE_CAUSES_WAKEUP flag at least (Building against Android 2.3.3).
WakeLock screenLock = ((PowerManager)getSystemService(POWER_SERVICE)).newWakeLock(
PowerManager.SCREEN_BRIGHT_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP, "TAG");
screenLock.acquire();
//later
screenLock.release();
This is very popular question but the accepted answer now is outdated.
Below is latest way to Turn On Screen OR wake up your device screen from an activity:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O_MR1) {
this.setTurnScreenOn(true);
} else {
final Window window = getWindow();
window.addFlags(WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
}
Use WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON but FLAG_TURN_SCREEN_ON flag has been deprecated in API level 27 so you can use Activity.setTurnScreenOn(true) from API level 27 onward.
In your main activity's OnCreate() write following code:
((PowerManager)getSystemService(POWER_SERVICE)).newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK, "TAG").acquire();
It causes the device to wake up.
Do not forget disableKeyguard() to unlock the device...
undefined's answer with NullPointer check and set timeout :
private void turnOnScreen() {
PowerManager.WakeLock screenLock = null;
if ((getSystemService(POWER_SERVICE)) != null) {
screenLock = ((PowerManager)getSystemService(POWER_SERVICE)).newWakeLock(
PowerManager.SCREEN_BRIGHT_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP, "TAG");
screenLock.acquire(10*60*1000L /*10 minutes*/);
screenLock.release();
}
}
This is how you can do it:
PowerManager powerManager = (PowerManager) context.getSystemService(context.POWER_SERVICE);
PowerManager.WakeLock wakeLock = powerManager.newWakeLock(PowerManager.FULL_WAKE_LOCK |
PowerManager.ACQUIRE_CAUSES_WAKEUP |
PowerManager.ON_AFTER_RELEASE, "appname::WakeLock");
//acquire will turn on the display
wakeLock.acquire();
make sure to set permission in the manifest :
<uses-permission android:name="android.permission.WAKE_LOCK"/>

Categories

Resources