Wake phone from unlock screen android

半城伤御伤魂 提交于 2019-12-06 03:19:29

If you are simply looking for MainActivity to show when the phone is locked, there are Window flags that can be set in onCreate() to make that happen.

FLAG_SHOW_WHEN_LOCKED will enable your activity to be visible to the user when the phone is locked, and FLAG_DISMISS_KEYGUARD will unlock the phone IF the user has not configured a secure (pin/pattern/face/etc) keyguard.

    Window window = this.getWindow();
    window.addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
    window.addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);

You can only cause the screen to awaken via the alarm using a wakelock. You cannot unlock the screen programmatically. That would bypass the security the user has selected, which is not allowed.

Also, note that right now you are using a PendingIntent with your alarm to start the Service which in turn starts the Activity. This is not guaranteed to work when the device is in a low power state. If you need to wake the device, use a PendingIntent for a BroadcastReceiver which in turn takes a wakelock and starts your Service. The Service can then take its appropriate action and release the wakelock. The system will ensure a wakelock is held long enough for your receiver's onReceive() method to complete. It does not (and cannot) do the same for starting a Service or Activity from an alarm.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!