Wake the Android Screen after a timer fires

2019-09-01 05:21发布

How can I have a timer fire a method to wake up the screen of an Android device?

I inserted this:

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

into the

@Override
protected void onCreate(Bundle savedInstanceState) {

method.

Additionally, I made a timer after a user clicks a button which runs the following program:

final int interval = 3000; // 3 Seconds
Handler handler = new Handler();
Runnable runnable = new Runnable(){
      public void run() {
           Toast.makeText(getApplicationContext(), "Here", Toast.LENGTH_SHORT).show();


                }
            };
handler.postAtTime(runnable, System.currentTimeMillis()+interval);
handler.postDelayed(runnable, interval);

I want to be able to click the power button of my Android device to sleep it within the 3 second interval and have it wake up after the run() gets fired. What do I call to trigger the screen to turn on?

2条回答
2楼-- · 2019-09-01 06:21

try to add to onCreate() :

        PowerManager pm = (PowerManager) getApplicationContext().getSystemService(Context.POWER_SERVICE);
        WakeLock wakeLock = pm.newWakeLock((PowerManager.SCREEN_BRIGHT_WAKE_LOCK |    PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP), "TAG");
        wakeLock.acquire();

To release the screen lock:

        KeyguardManager keyguardManager = (KeyguardManager) getApplicationContext().getSystemService(Context.KEYGUARD_SERVICE); 
        KeyguardLock keyguardLock =  keyguardManager.newKeyguardLock("TAG");
        keyguardLock.disableKeyguard();

Add to the manifest:

<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.DISABLE_KEYGUARD" />
查看更多
够拽才男人
3楼-- · 2019-09-01 06:26

This method also instantly turns on the screen:

PowerManager powerManager = (PowerManager) getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock TempWakeLock = powerManager.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK|PowerManager.ACQUIRE_CAUSES_WAKEUP, "TempWakeLock");
TempWakeLock.acquire();
TempWakeLock.release();
查看更多
登录 后发表回答