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
Related
I want to call a method before shotdown or restart an android phone.
As they said on the official site
Broadcast Action: Device is shutting down. This is broadcast when the device is being shut down (completely turned off, not sleeping). Once the broadcast is complete, the final shutdown will proceed and all unsaved data lost. Apps will not normally need to handle this, since the foreground activity will be paused as well.
As of Build.VERSION_CODES#P this broadcast is only sent to receivers registered through Context.registerReceiver.
In different sources, I read that a foreground service requires a wake lock to stay active after the device goes to sleep. But when I test it by starting a foreground service and turning the screen off while the device is unplugged (both on emulator and on a real Samsung device), the foreground service keeps running.
Does a foreground service require a (partial) wake lock to stay active after the screen is off?
From my experience of developing a timer, the answer is yes, especially when the screen is off.
Without a wake lock, the foreground service will be killed or suspended in a few minutes(2 ~ 10m in my tests). Sometimes, when the screen is off, the code won't be executed but the foreground notification still exists and the code only starts being executed after the screen is turned on. This makes debug very hard. This situation is more common if the test device is from Chinese manufacturers(Foreground service + Wake Lock + Letting user whitelist your app seems the only solid option if your app targets Chinese market).
Use a wake lock if you want your service keep running after the screen is off.
I've searched a bit but had a question about the true function of the wakelock with GCM.
Does the wakelock prevent the device from going into a sleep mode that would prevent the network layer from shutting down -- so it can receive a GCM message from the network-level in the first place
Or when GCM is enabled and working (via manifest permissions, properly coded GCM code, etc), it will have a network connection to Google for GCM up regardless, and the wakelock is only needed from a UI or application level to turn the screen back on so you can do other things (interact with user, etc) after receiving it.
Or none/some of the above (please elaborate :)
Thanks
geremy
According to me you need to acquire WAKE_LOCK to start GCM Service and then release it,
as your CPU should not sleep before starting service.
CPU will go in sleep mode in some time after your screen turns off. When your device is in sleep mode your threads will be suspended.
If you acquire wake lock and does not release it, it will surely consume huge amount of battery.
You can receive GCM messages while your device is in idle mode.
When message arrives it depends on your code whether to wake device and show notification or not.
You can also delay the message till device comes out of idle state by using a flag delay_while_idle.
You can handle messages by registering BroadcastReceiver
Please refer to the questions and answer on SO :
Wakelock in deep sleep
Power management
GCM in standby
WAKE_LOCK
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.
I want to do something when the mobile device is closing, but I don't know what is the method; could you tell me how to detect if mobile device is shutting down?
I know the method about mobile restart android.intent.action.BOOT_COMPLETED, but I cannot find similar power off.
http://developer.android.com/reference/android/content/Intent.html#ACTION_SHUTDOWN
public static final String
ACTION_SHUTDOWN
Since: API Level 4 Broadcast Action:
Device is shutting down. This is
broadcast when the device is being
shut down (completely turned off, not
sleeping). Once the broadcast is
complete, the final shutdown will
proceed and all unsaved data lost.
Apps will not normally need to handle
this, since the foreground activity
will be paused as well. This is a
protected intent that can only be sent
by the system.
Constant Value:
"android.intent.action.ACTION_SHUTDOWN"
This would appear to be it? I just Googled your command and it was on the Standard Broadcast Actions list. :)