Start activity screen even if the screen is locked on Android

How to start Activity on the device, even if the screen is locked. I tried as below, but it does not work.

Broadcast Receiver:

Intent alarmIntent = new Intent("android.intent.action.MAIN"); alarmIntent.setClass(context, Alarm.class); alarmIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); alarmIntent.addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED + WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD + WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON + WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON); context.startActivity(alarmIntent); 
+8
source share
5 answers

You need the following permission in the AndroidManifest.xml file:

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

Check out the details of the manifest here . You can check this link in the request.

+13
source

You can achieve this in two ways:

  • using tracking lock as explained by @Yup in this post.

  • using window flags.

Using window flags:

Open the operation A that you want to run in onReceive(...) . Insert this into the onCreate() this 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 don't insert it before setContentView(...)

+14
source

You can check here as to whether the screen is locked or unlocked.

You can then use the power-on and power-lock to save the screen without locking. you can find help here

+1
source
  • the manifest file gives permission use-permission android: name = "android.permission.WAKE_LOCK" then write the code inside your activity on demand onCreate ()
  • final Window win = getWindow (); win.addFlags (WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON | WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
0
source

Insert this into the onCreate method of the action 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); 
0
source

Source: https://habr.com/ru/post/958533/


All Articles