Wake Android Device up

Hey, I need to wake my sleeping Android device at a specific time. Any suggestions?

PS Wake up: turn on the display and possibly unlock the phone.

+35
android wakeup
Sep 01 '10 at 19:45
source share
7 answers

It is best to use the appropriate combination of these window flags:

http://developer.android.com/reference/android/view/WindowManager.LayoutParams.html#FLAG_DISMISS_KEYGUARD
http://developer.android.com/reference/android/view/WindowManager.LayoutParams.html#FLAG_SHOW_WHEN_LOCKED
http://developer.android.com/reference/android/view/WindowManager.LayoutParams.html#FLAG_KEEP_SCREEN_ON
http://developer.android.com/reference/android/view/WindowManager.LayoutParams.html#FLAG_TURN_SCREEN_ON

If you want to run on older versions of the platform that do not support the desired flag (flags), you can directly use wake locks and key lock locks ... but this way is fraught with danger.

ONE IMPORTANT NOTE . Your activity must be full screen for the combination of the above flags to work. In my application, I tried using these flags with an activity that is not full-screen (Dialog Theme), and this did not work. After looking at the documentation, I found that for these flags the window should be full-screen.

+32
Sep 02 '10 at 1:09 on
source share

To wake up the screen:

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 unlock the screen:

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

And the manifest should contain:

 <uses-permission android:name="android.permission.WAKE_LOCK" /> <uses-permission android:name="android.permission.DISABLE_KEYGUARD" /> 

For more information on PowerManager, see the API documentation: http://developer.android.com/reference/android/os/PowerManager.html.

EDIT: This answer is deprecated.

+73
Apr 16 '12 at 18:55
source share

I found a way, and it’s not so difficult ... it works with any version of the API.

You need to use the PowerManager.userActivity(l, false) method and register your activity as a broadcast received for the SCREEN_OFF intent:

In your OnCreate activity, put something like:

 mReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { Log.v(TAG, "Screen OFF onReceive()"); screenOFFHandler.sendEmptyMessageDelayed(0, 2000L); } }; 

It will start the handler after 2 seconds of the Screen Off event.

Register the receiver in the onResume () method:

 IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_OFF); registerReceiver(mReceiver, filter); Log.i(TAG, "broadcast receiver registered!"); 

Create a handler as shown below:

 private Handler screenOFFHandler = new Handler() { @Override public void handleMessage(Message msg) { super.handleMessage(msg); // do something // wake up phone Log.i(TAG, "ake up the phone and disable keyguard"); PowerManager powerManager = (PowerManager) YourActivityName.this .getSystemService(Context.POWER_SERVICE); long l = SystemClock.uptimeMillis(); powerManager.userActivity(l, false);//false will bring the screen back as bright as it was, true - will dim it } }; 

Request permission in manifest file:

 <uses-permission android:name="android.permission.WAKE_LOCK" /> 

Remember to unregister the broadcast receiver when you are done. You can do this in onDestroy (), for example (which is not guaranteed)

 unregisterReceiver(mReceiver); Log.i(TAG, "broadcast UNregistred!"); 
+8
Dec 27 '10 at 21:32
source share

If you see a window upon waking up, you can easily add it easily by adding several flags to your activity without using tracking lock.

 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.my_activity); getWindow().addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD | WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED | WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON); } 
+1
Jan 21 '17 at 15:59
source share

Setting an alarm will wake up the phone programmatically (play a sound), and I assume that an option will appear on the display.

I do not think that an API will be opened that automatically unlocks the phone.

0
01 Sep '10 at 19:48
source share
 getWindow().addFlags(LayoutParams.FLAG_DISMISS_KEYGUARD); 

rejects the general key lock and forces the device to unlock.

0
Jan 12 '14 at 12:09
source share

On newer devices, you should use something like this, as the flags mentioned are deprecated.

 class AlarmActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_alarm) // Keep screen always on, unless the user interacts (wakes the mess up...) window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON) setTurnScreenOn(true) setShowWhenLocked(true) (getSystemService(KeyguardManager::class.java) as KeyguardManager).requestDismissKeyguard(this, object: KeyguardManager.KeyguardDismissCallback(){ override fun onDismissCancelled() { Log.d("Keyguard", "Cancelled") } override fun onDismissError() { Log.d("Keyguard", "Error") } override fun onDismissSucceeded() { Log.d("Keyguard", "Success") } } ) } } 

KeyguardManager.requestDismissKeyguard only wakes up the device if the setTurnScreenOn(true) was called before.

I tested this on my Android Pie device.

0
Mar 21 '19 at 14:38
source share



All Articles