Goal is: run Activity to foreground from Service when phone has locked screen, and set window focus in launched Activity.
Prepare:
set phone to the locked state by power button,
run Activity from Service:
Intent i = new Intent(mCtx, RecActivity.class);
i.putExtra(RecActivity.INTENT_REQUEST, intentExtra);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
ctx.startActivity(i); //ctx is context
In Activity onCreate()
run this method:
private void checkIntentExtraData(){
try {
//Check Intent
Intent i = getIntent();
Bundle extras = i.getExtras();
if(extras != null) {
/*PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK
| PowerManager.ACQUIRE_CAUSES_WAKEUP
| PowerManager.ON_AFTER_RELEASE, "INFO");
wl.acquire();*/
window.addFlags(WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
window.addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD); //This constant was deprecated in API level 26.
window.addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED); //This constant was deprecated in API level 27. => setShowWhenLocked(
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { //API 26+ Android 8.0+
KeyguardManager km = (KeyguardManager) getSystemService(Context.KEYGUARD_SERVICE);
if(km != null)
km.requestDismissKeyguard(this, null);
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O_MR1) { //API 27+ Android 8.1+
setShowWhenLocked(true);
setTurnScreenOn(true);
}
Log.i(TAG, "hasWindowFocus: "+hasWindowFocus()); //TODO
}
} catch (Exception e){
e.printStackTrace();
}
}
Activity is run, callbacks are: onCreate()
=>onResume()
=>onPause()
.
I do not want to run onPause()
method. It's probably, because Activity has no focus (hasWindowFocus()
return false).
Any suggestions? Thank you.
I tried too, but not working:
android:showOnLockScreen="true"
in Manifest.