Turning on screen from receiver/service

后端 未结 3 1861
再見小時候
再見小時候 2020-11-27 17:31

I would like my app to be able to turn the screen on and display my app. Let\'s say I\'m setting an alarm and every hour I want my app to be displayed for 2 mins before the

相关标签:
3条回答
  • 2020-11-27 17:38

    I don't know what you're talking about, wakelock is definitely not deprecated. Certain types are no longer the Google preferred way of doing things, but normal wakelocks are still around and still the easiest way of doing this. Make sure to add the ACQUIRE_CAUSES_WAKEUP flag when taking the lock. In fact notice that a WakefulBroadcastReceiver is implemented by using wakelocks.

    0 讨论(0)
  • 2020-11-27 17:53

    You can use this code to turn the screen on.

    lock = ((KeyguardManager) getSystemService(Activity.KEYGUARD_SERVICE)).newKeyguardLock(KEYGUARD_SERVICE);
    powerManager = ((PowerManager) getSystemService(Context.POWER_SERVICE));
    wake = powerManager.newWakeLock(PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP, "TAG");
    
    lock.disableKeyguard();
    wake.acquire();
               getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
                    | WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD
                    | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
                    | WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON
                    | WindowManager.LayoutParams.FLAG_ALLOW_LOCK_WHILE_SCREEN_ON);
    

    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" />
    

    EDIT (USE THIS ONE, NOTHING IS DEPRECATED):
    There is an one more alternative for doing this, for that you need to launch an activity, In the activity onCreate() you need to add the flags to the window. For example:

       @Override
        protected void onCreate(Bundle savedInstanceState) {
          getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED | WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON | WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON | WindowManager.LayoutParams.FLAG_ALLOW_LOCK_WHILE_SCREEN_ON);`
    }
    
    0 讨论(0)
  • 2020-11-27 17:53

    You can use this code to turn the screen on.

    private void turnScreenOn() {
        int flags = WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD | WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED | WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON;
        getWindow().addFlags(flags);
    }
    

    You can use this code to keep it on until the wake lock is dimissed.

    <uses-permission android:name="android.permission.WAKE_LOCK" />
    
    private PowerManager mPowerManager;
    private PowerManager.WakeLock mWakeLock;
    
    @Override
    public void onCreate() {
    super.onCreate();
        mPowerManager = (PowerManager) getSystemService(Context.POWER_SERVICE);
        mWakeLock = mPowerManager.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP, "Service");
    }
    
    private void acquireWakeLock() {
        try {
            mWakeLock.acquire();
        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    private void releaseWakeLock() {
        try {
            mWakeLock.release();
        }
        catch (Exception e) {
    
        }
    }
    
    0 讨论(0)
提交回复
热议问题