18

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);
manfcas
  • 1,933
  • 7
  • 28
  • 47
yuva ツ
  • 3,707
  • 9
  • 50
  • 78

6 Answers6

25

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

Junaid
  • 3,477
  • 1
  • 24
  • 24
  • 1
    if I use second way (using window flags), I dont need to request permission in androidmanifest.xml ? – Dika Jun 13 '17 at 02:34
  • 2
    Yes, you don't the permission @Dika – Junaid Nov 06 '17 at 05:12
  • it worked for me too when the screen is locked it launches my activity but the other work in onCreate method is not being called. For ex. I am playing a sound which is not being played if screen is locked. – Chirag Jain Jan 30 '19 at 10:51
  • sound play when I commented mpPlay.stop(); from onPause – Chirag Jain Jan 30 '19 at 11:46
  • but problem is when activity is not displayed it should stop sound. I am stuck please help. – Chirag Jain Jan 30 '19 at 11:47
  • I followed below link too but no success https://stackoverflow.com/questions/22643175/no-sound-when-the-activity-starts-from-lock-screen – Chirag Jain Jan 30 '19 at 11:47
  • @ChiragJain, I haven't worked on Android for a long time now. Please post your issue as another question. You may link this answer to your posted question though – Junaid Feb 10 '19 at 17:33
  • 1
    @Junaid Thanks for your response, actually somehow I managed it by adding mp.start method in onResume in place of onCreate because onResume again being called in last if (mpPlay.isPlaying()) { mpPlay.stop(); mpPlay.reset(); } in onDestroy method. when working with this screen lock scenario activity open. – Chirag Jain Feb 11 '19 at 06:17
22

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.

Pradip
  • 3,189
  • 3
  • 22
  • 27
10

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); 
Ahad Khan
  • 271
  • 3
  • 4
2

As of Android version 10 (SDK version 29), the other answers will no longer work if the app is running this in the background, for example in a BroadcastReceiver.

In order to make it work on Android 10 and onwards, you should use a full-screen intent if you really need to start an activity from the background [source]:

Android 10 (API level 29) and higher place restrictions on when apps can start activities when the app is running in the background. These restrictions help minimize interruptions for the user and keep the user more in control of what's shown on their screen.

In nearly all cases, apps that are in the background should display time-sensitive notifications to provide urgent information to the user instead of directly starting an activity. Examples of when to use such notifications include handling an incoming phone call or an active alarm clock.

This can be achieved as follows [source]:

val fullScreenIntent = Intent(this, CallActivity::class.java)
val fullScreenPendingIntent = PendingIntent.getActivity(this, 0,
    fullScreenIntent, PendingIntent.FLAG_UPDATE_CURRENT)

val notificationBuilder =
        NotificationCompat.Builder(this, CHANNEL_ID)
    .setSmallIcon(R.drawable.notification_icon)
    .setContentTitle("Incoming call")
    .setContentText("(919) 555-1234")
    .setPriority(NotificationCompat.PRIORITY_HIGH)
    .setCategory(NotificationCompat.CATEGORY_CALL)

    // Use a full-screen intent only for the highest-priority alerts where you
    // have an associated activity that you would like to launch after the user
    // interacts with the notification. Also, if your app targets Android 10
    // or higher, you need to request the USE_FULL_SCREEN_INTENT permission in
    // order for the platform to invoke this notification.
    .setFullScreenIntent(fullScreenPendingIntent, true)

val incomingCallNotification = notificationBuilder.build()

Portions of this answer are reproduced from work created and shared by the Android Open Source Project and used according to terms described in the Creative Commons 2.5 Attribution License.

Community
  • 1
  • 1
notquiteamonad
  • 1,159
  • 2
  • 12
  • 28
1

For Some Android 8,9,

You need to use SYSTME_ALERT_WINDOW permission in Manifest and add Flags to Start Activity Intent .setFlags(Intent.FLAG_ACTIVITY_NEW_TASK)

like this

 val i = Intent(context, AlarmActivity::class.java)
.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK))
 it.startActivity(i)

This is in Kotlin

For Android 10+ Device

You need to use FullScreen Intent Notification with pending intent to start the activity you want.

Learn more about fullscreen intent

Fullscreen Intent Example

Wai Yan
  • 137
  • 1
  • 9
0
  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);