Code -
public class ShutdownReceiver extends BroadcastReceiver {
private static final String TAG = "ShutdownReceiver";
@Override
public void onReceive(final Context context, final Intent intent) {
Logger.i(TAG, "Shutting Down..........................");
if("android.intent.action.ACTION_SHUTDOWN".equals(intent.getAction())) {
//Power Off
}
}
}
Service -
private BroadcastReceiver mReceiver = null;
@Override
public void onCreate() {
super.onCreate();
// INITIALIZE RECEIVER
//It is used to register broadcast for ShutDown or Power Off
IntentFilter filter = new IntentFilter(Intent.ACTION_SHUTDOWN);
mReceiver = new ShutdownReceiver();
registerReceiver(mReceiver, filter);
}
@Override
public void onDestroy() {
unregisterReceiver(mReceiver);
}
Permissions -
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.CALL_PHONE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.CALL_PRIVILEGED" />
<uses-permission android:name="android.permission.CHANGE_CONFIGURATION" />
<uses-permission android:name="android.permission.GET_TASKS" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.DEVICE_POWER" />
I know this is old but I have a partial solution which follows the material above. At least the shutdown event is called and I am able to do that unique item on shutdown, but there is a side effect that I don't understand; a memory leak. In one sense it IS shutdown so the memory leak has no drastic effects but I don't understand it and that is a bother.
In any case I have no additions in the manifest.xml with respect to this BroadcastReceiver. It is completely defined in code.
In my main activity a define a class as above
private class ShutDownReceiver extends BroadcastReceiver
{
@Override
public void onReceive(Context context, Intent intent)
{
if(Intent.ACTION_SHUTDOWN.equals(intent.getAction()))
{
Log.i(TAG, "System shutting down");
context.stopService(new Intent(context, BluetoothPanService.class));
}
}
}
In the main activity's onResume() method I instantiate and register the receiver:
public void onResume()
{
super.onResume();
Log.i(TAG, "Measurement Log Activity has Resumed.");
IntentFilter filter = new IntentFilter(Intent.ACTION_SHUTDOWN);
mReceiver = new ShutDownReceiver();
registerReceiver(mReceiver, filter);
}
('mReceiver' is defined as a private class variable of the main activity)
And finally in the main activity's onPause() I unregister the receiver
public void onPause()
{
super.onPause();
Log.i(TAG, "Measurement Log Activity has Paused.");
this.unregisterReceiver(mReceiver);
}
When the phone is powered off the ACTION_SHUTDOWN event is indeed signaled and the service is stopped; it's onDestroy() method is signaled and a message to a remote MQTT broker service is published indicating that the client is no longer connected. The remote broker is getting the message so I know it is working.
However, after that all hell break loose and it's mainly because I cannot stop my service from re-starting (even though I set START_NOT_STICKY in the onStartCommand method). But at this time besides getting a lot of red error lines in logcat, the ACTION_SHUTDOWN is heeded and the action I need to invoke gets done.
Hope this helps someone (who perhaps isn't dealing with shutting down a service).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With