I am trying to understand the intents that get launched when the device is unlocked.
For eg: Say my activity is running, and I press the power button (screen off, to lock the phone). INTENT.ACTION_SCREEN_OFF is launched. The activity is paused and the screen goes blank.
Now, when I press the power button again (INTENT.SCREEN_ON gets launched), the activity's onResume method is called. But the device is not yet unlocked.
What happens when the device is unlocked? To put it simply, what is the intent's action and category?
Check out ACTION_USER_PRESENT.
Additionally to
ACTION_USER_PRESENT
Broadcast Action: Sent when the user is present after device wakes up
(e.g when the keyguard is gone). (API Lvl 3)
Android SDK 24 added
ACTION_USER_UNLOCKED
Broadcast Action: Sent when the credential-encrypted private storage
has become unlocked for the target user. This is only sent to
registered receivers, not manifest receivers.
Related
I know that there is a system broadcast called android.intent.action.SCREEN_OFF, but I want to receive broadcast when user lock his phone not screen off.
I am developing an application that can tell user how long his phones is locked.
I don't see the broadcast you're asking for in the list of available broadcasts listed here https://developer.android.com/about/versions/11/reference/broadcast-intents-30). However, you could use the ACTION_SCREEN_OFF broadcast, and in its onReceive(), use one of the KeyguardManager class's methods to check if the device is locked. The method used would depend on your API version. For example, isKeyguardSecure() supports API 16 and above.
My app does not receive ACTION_SCREEN_ON or ACTION_USER_PRESENT on Android O device after device remains idle for some time.
My app has widgets. I update those widgets with data from backend when the user unlocks the phone. This functionality has been working when I am targetting SDK 25.
Now I am compiling the app with target SDK 26. I converted the service that gets data for widgets to JobService. Everythings works fine when the user adds the widget to the home screen. Then if I kill the app and turn off the screen and say after 5 minutes if I unlock my phone, my app receives ACTION_SCREEN_ON and ACTION_USER_PRESENT intent I am able to start the service and update data on the widget. However, If I leave device idle for a couple of hours and then when I unlock the screen, the application does not receive ACTION_SCREEN_ON and ACTION_USER_PRESENT intent.
Is this due to Android O limitations on implicit intents. If so how have you solved the problem?
If not then what method has worked for you?
If android application is installed in mobile but never launched and considered i have implemented boot completed receiver for this application.Now i am going to reboot the device .here boot completed receiver will call or what will happens?
No You have to launch the app once to register receiver with the device. Without launching the application Device will not detect your broadcast receiver.
Set the RECEIVE_BOOT_COMPLETED permission in your application's manifest. This allows your app to receive the ACTION_BOOT_COMPLETED that is broadcast after the system finishes booting (this only works if the app has already been launched by the user at least once)
The documentation clearly states that it works only if the app has been launched at least once. So the answer of your question is NO, it wont be called.
The documentation says:
Broadcast Action: Sent when the user is present after device wakes up
(e.g when the keyguard is gone).
Are there any cases when ACTION_USER_PRESENT is sent except for the screen unlocking?
I have a long-running service listening for sensor input. To conserve battery power, I've stopped the sampling of the sensors on ACTION_SCREEN_OFF and started it back up again on ACTION_SCREEN_ON.
Often, however, I just turn my phone on for a quick glance at the clock on the lock-screen (to check the current time) and then turn it off again without unlocking the phone. In that case, there's no reason to spin up the sensors just to shut then down again at once.
So therefore I tried replacing ACTION_SCREEN_ON with ACTION_USER_PRESENT in my broadcast listener. This worked fine except for one special case: When the screen goes off and I press the power button (or home button) at once, the lock screen is skipped. And then the ACTION_USER_PRESENT is never received, only the ACTION_SCREEN_ON.
Is there a way for me broadcast receiver, upon receiving an ACTION_SCREEN_ON to know if the screen-lock is active and to expect a ACTION_USER_PRESENT later? Or if the lock-screen is skipped, not to wait upon ACTION_USER_PRESENT and go ahead and restart the sampling at once?
Till Android 4.4, if screen is turned on and lock screen is disabled, only ACTION_SCREEN_ON is fired. From Android 5.0, in this case, both ACTION_SCREEN_ON and ACTION_USER_PRESENT are fired.