Start Activity screen even if screen is locked in Android
Asked Answered
D

6

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);
Dyne answered 21/11, 2013 at 5:26 Comment(0)
S
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.

Stuartstub answered 21/11, 2013 at 5:46 Comment(0)
C
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(...) :-)

Circumpolar answered 2/8, 2015 at 13:58 Comment(8)
if I use second way (using window flags), I dont need to request permission in androidmanifest.xml ?Danette
Yes, you don't the permission @DanetteCircumpolar
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.Forbidding
sound play when I commented mpPlay.stop(); from onPauseForbidding
but problem is when activity is not displayed it should stop sound. I am stuck please help.Forbidding
I followed below link too but no success #22643675Forbidding
@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 thoughCircumpolar
@Circumpolar 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.Forbidding
S
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.

Stuartstub answered 21/11, 2013 at 5:46 Comment(0)
L
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); 
Longshore answered 29/4, 2019 at 10:50 Comment(1)
For me it never gets to the onCreate method, something is stopping it before thatCatalinacatalo
P
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.

Pallmall answered 4/5, 2020 at 22:19 Comment(0)
D
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

Dextrose answered 19/1, 2022 at 10:37 Comment(0)
R
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);
Rockies answered 22/2, 2018 at 11:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.