Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Start Activity screen even if screen is locked in Android

How to start an Activity on device even if screen is locked. I tried as below but it's not working.

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);
like image 453
yuva ツ Avatar asked Nov 21 '13 05:11

yuva ツ


1 Answers

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(...) :-)

like image 63
Junaid Avatar answered Oct 02 '22 14:10

Junaid