Start Activity screen even if screen is locked in Android

前提是你 提交于 2019-11-27 12:54:59

You need the following permission in AndroidManifest.xml file:

<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.DISABLE_KEYGUARD" />

Check the manifest details here. You can check this link on you query.

You can achieve this in two ways:

  1. using wake lock as explained by @Yup in this post.

  2. using window flags.

Using window flags:

Open the Activity A which you want to start in onReceive(...). Paste this in onCreate() of that Activity A

final Window win= getWindow();
win.addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED | WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
win.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON | WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);

Make sure you're not pasting it before setContentView(...) :-)

Paste this in your onCreate method of the activity you want to open when the screen is locked, after setContentView()

getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON|
        WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD|
        WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED|
        WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON); 

You can check here regarding whether screen is locked or unlocked.

Then you can use wake lock and power management options to maintain screen without getting locked. you can find help here

  1. manifest file give permission uses-permission android:name="android.permission.WAKE_LOCK" then write code inside in your requirement activity onCreate()
  2. final Window win= getWindow(); win.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON | WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!