Start Activity screen even if screen is locked in Android

前端 未结 6 402
执笔经年
执笔经年 2020-12-02 19:22

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 Int         


        
相关标签:
6条回答
  • 2020-12-02 19:54

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

    0 讨论(0)
  • 2020-12-02 20:00

    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

    0 讨论(0)
  • 2020-12-02 20:02

    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.

    0 讨论(0)
  • 2020-12-02 20:05

    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.

    0 讨论(0)
  • 2020-12-02 20:05

    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); 
    
    0 讨论(0)
  • 2020-12-02 20:10
    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);
    0 讨论(0)
提交回复
热议问题