2

I want to know in my app if the phone could turn off given the battery might be low. I am doing some client / server data exchange.

There seems to be broadcast action ACTION_SHUTDOWN. Will this broadcast be sent if the phone may turn off due to critically low battery ?

Jake
  • 16,329
  • 50
  • 126
  • 202

1 Answers1

2

Yes, your application can get that message if you implement a BroadcastReceiver class like the one below.

public class ShutdownReceiver extends BroadcastReceiver {
  @Override
  public void onReceive(Context context, Intent intent) 
  {
    //Insert your code here
  }
}

and do't forget to add the following in your manifest file.

 <uses-permission android:name="android.permission.DEVICE_POWER" />

and this:

 <receiver android:name=".ShutdownReceiver">
  <intent-filter>
   <action android:name="android.intent.action.ACTION_SHUTDOWN" />
  </intent-filter>
</receiver>

You can also do the same for ACTION_BATTERY_LOW.

Christian Abella
  • 5,747
  • 2
  • 30
  • 42
  • the DEVICE_POWER permission is only granted to system apps? – slashdottir Nov 08 '15 at 09:37
  • I was able to get it to work without needing to add the DEVICE_POWER permission -- seems not to be required. Also as noted [link]http://stackoverflow.com/questions/10448304/handling-phone-shutdown-event-in-android here, `action android:name="android.intent.action.QUICKBOOT_POWEROFF"` should be included in the intent-filter. – dazed Dec 02 '15 at 17:13