Question

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);
Was it helpful?

Solution 2

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.

OTHER TIPS

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

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.

  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);

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top