Replacing ACTION_SCREEN_ON withy ACTION_USER_PRESENT? - android

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.

Related

I need to detect screen on or user present on Android

I am developing a widget and I need to detect a screen on or user present intent in order to update the widget when the screen is switched on.
I have checked similar questions and answers on stack overflow and came to the conclusion that registering a service that will listen to SCREEN_ON and SCREEN_OFF intents is not really a good solution since the service will be kept active all the time.
A possible solution is to detect screen on using the USER_PRESENT intent (which can be registered in the manifest and delivered to the widget, hence no need to keep a service in memory) and then update the widget as necessary. You can also register the screen on/off service on USER_PRESENT to handle the SCREEN_OFF intent and unregister it on screen off).
The problem is that, from previous experience, the USER_PRESENT intent is not always broadcasted. There are cases where the user does not have any screen lock defined (i.e., he selects the swipe method) and in such cases, the USER_PRESENT intent is not broadcasted.
Is there a way to make sure that the USER_PRESENT intent is broadcasted on all devices? If not, is there another way to detect when the screen turns on?

Altenative way to detect screen off/on

Is there an alternative way to detect screen on/off in Android?
I'm currently using ACTION_SCREEN_ON and ACTION_SCREEN_ON intents catched with a receiver, but in API 17 it's bugged (in combination with proximity wake lock).

broadcast receiver not working in sleep mode

I want a alarm System in my application, I want to open a dialog activity when alarm is fired,
it's working fine when phone is active, but the broadcast receiver is not working when phone is sleep or standby mode.
I also use wacklock logic in broadcast receiver but it can't work well.
you need to write service for that keep working in when phone is sleep or standby mode here is the link

Track restarts, shuts down, power up and power down

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

Android -- What happens when device is unlocked?

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.

Categories

Resources