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?
Related
I want to check when a user Unlocks his device, and run a piece of code. How can I know If user unlocked?
I tried creating a broadcast receiver, registering it in the manifest with an intent filter for action USER_PRESENT. But from android Oreo restrictions were imposed on broadcasts.
I've tried an implicit broadcast receiver, but its life ends with the app getting killed.
"How to get unlock or USER_PRESENT event even after the app is killed"
can a background service be used here?
or any other broadcast?
My application relies on the intent ACTION_USER_PRESENT being fired, and so I set up a receiver in the manifest and I have a class that starts a service when it receives the intent.
However, when a user is using a lock-screen replacement app like WidgetLocker, the ACTION_USER_PRESENT intent may never be sent, or can be sent a bunch of times. (Once it was sent 5 times...) WidgetLocker's website explains that the application does send its own intent for unlocking, com.teslacoilsw.widgetlocker.intent.UNLOCKED. In certain configurations of WidgetLocker, ACTION_USER_PRESENT may be fired before the user even unlocks the screen, so I was told that it would be best to set up a check for com.teslacoilsw.widgetlocker.intent.LOCKED, and then wait to receive the UNLOCKED intent and do my work.
My problem is that I'm not sure how to set up a receiver for a third party intent. I've added the actions to my receiver in the manifest just find, and I know that my broadcast receiver picks them up, but I need to filter them out. Mainly, if I pick up the LOCKED intent, I want to ignore any ACTION_USER_PRESENT intents, and instead wait for the UNLOCKED intent, but I don't know how to wait for an intent upon receiving a different one.
Mainly, if I pick up the LOCKED intent, I want to ignore any ACTION_USER_PRESENT intents, and instead wait for the UNLOCKED intent, but I don't know how to wait for an intent upon receiving a different one.
Step #1: Create separate BroadcastReceivers for the WidgetLocker actions vs. ACTION_USER_PRESENT.
Step #2: Upon receipt of LOCKED, use PackageManager and setComponentEnabledSetting() to disable your ACTION_USER_PRESENT receiver.
Step #3: Upon receipt of UNLOCKED, use PackageManager and setComponentEnabledSetting() to re-enable your ACTION_USER_PRESENT receiver.
This could get a bit dicey in edge cases (e.g., user pops out the battery while LOCKED), but it's a starting point.
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.
can we get notification if user restarts, shuts down, power up or down the device
Ajay,
The two Broadcast Actions you are most likely interested in are:
ACTION_BOOT_COMPLETED: This is broadcast once, after the system has finished booting.
ACTION_SHUTDOWN: Device is shutting down. This is broadcast when the device is being shut down (completely turned off, not sleeping).
Please keep in mind to receive ACTION_BOOT_COMPLETE you must have the RECEIVE_BOOT_COMPLETED permission in your AndroidManifest.xml
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.