Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
238 views
in Technique[技术] by (71.8m points)

bootcompleted - Android ACTION_SHUTDOWN Broadcast not working

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" />
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...