Is there any way to receive a notification when the user powers off the device?
You can use the ACTION_SHUTDOWN
Intent which is broadcast when the phone is about to shutdown. The documentation says:
Apps will not normally need to handle this, since the foreground activity will be paused as well.
In other words, if you respond to all the lifecycle events for your Activity appropriately, there's no need to use this unless you really want to do something specific related to shutdown.
The ACTION_SHUTDOWN
Intent was introduced in API Level 4, in other words it'll only be sent on phones running Android 1.6 or later.
You'll trap the Broadcast with a BroadcastReceiver
. It will look something like this:
public class ShutdownReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
//Insert code here
}
}
You'll also need an entry in your Manifest like the following:
<receiver android:name=".ShutdownReceiver">
<intent-filter>
<action android:name="android.intent.action.ACTION_SHUTDOWN" />
</intent-filter>
</receiver>
Depending on what you're doing, another option would be to use the ACTION_BOOT_COMPLETED
Intent which is sent when the phone is restarted.
Notifications when iPhone is turned off or app is uninstalled
When the phone is turned off, the notifications will remain on APNS for the expiry time and APNS will try to deliver it until it expires. See this and search for
apns-expiration
If the app is uninstalled, you will get an error on server side that the device token to which the notifications is sent is not valid any more.
You must read the docs carefully. It is all mentioned there.
Related Topics
Edittext: Disable Paste/Replace Menu Pop-Up on Text Selection Handler Click Event
How to Show an Activity Before My App Is Uninstalled (Android)
How to Avoid "Illegalstateexception: Scrollview Can Host Only One Direct Child"
Error Java.Lang.Classnotfoundexception: Com.Google.Android.Gms.Maps.Mapfragment in Google Map V2
Some Androids Apps Won't Connect Through Fiddler
Understanding Command Through Adb Shell and Through Code - Android
Navigation Drawer (Google+ VS. Youtube)
Android - Application (Apk) Maximum Size
How to Globally Force Screen Orientation in Android
How to Display Progress Dialog Before Starting an Activity in Android
Could Not Find Com.Android.Tools.Build:Aapt2:3.2.0
Android Studio Fails to Debug with Error Org.Gradle.Process.Internal.Execexception
Gles10.Glgetintegerv Returns 0 in Lollipop Only
Android Firebase Dynamitemodule: Failed to Load Module Descriptor
Using Contextmenu with Listview in Android
Android Activity Ondestroy() Is Not Always Called and If Called Only Part of the Code Is Executed