Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wake up device programmatically

I want to wake up and unlock a device. Then, I'd like show an activity when the user has new message from Firebase.

I wrote this on onResume() method:

window = this.getWindow();
window.addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED | WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON | WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);

This works for devices with API > 19. The problem is that in KitKat, it either does nothing or it wakes up the screen but doesn't unlock the device.

Also I set the right permissions in the AndroidManifest.xml:

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

Also in MyFirebaseMessagingService.class, I'm starting an activity with FLAG_ACTIVITY_NEW_TASK as flag.

Does anyone know what am I missing?

Thanks for the help.

like image 972
Vasileios Pallas Avatar asked Nov 08 '22 07:11

Vasileios Pallas


1 Answers

Ok i found it. I just add that piece of code and it worked

KeyguardManager manager = (KeyguardManager) this.getSystemService(Context.KEYGUARD_SERVICE);
KeyguardManager.KeyguardLock lock = manager.newKeyguardLock("abc");
lock.disableKeyguard();
like image 92
Vasileios Pallas Avatar answered Nov 15 '22 05:11

Vasileios Pallas