Detecting shut down and reboot in android

Hi, Have a great day frnds..

Now we going to look up the detecting shut down and reboot events in android.

Here is the code:

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;


public class ShutDownReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent.getAction().equals(Intent.ACTION_SHUTDOWN)) {
            Log.i("event", "shutdown");
        } else if (intent.getAction().equals(Intent.ACTION_REBOOT)) {
            Log.i("event", "reboot");
        }
    }
}

Then apply in your activity,

shutDownReceiver = new ShutDownReceiver();
filter.addAction(Intent.ACTION_REBOOT);
filter.addAction(Intent.ACTION_SHUTDOWN);

@Override
    protected void onPause() {
        super.onPause();
unregisterReceiver(shutDownReceiver);
}

@Override
    protected void onResume() {
        super.onResume();
registerReceiver(shutDownReceiver, filter);
}

Finally don't forget to add below code in Manifest.

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

Comments

Popular posts from this blog

Clear the app data programmatically in android

convert an integer to a string in java

Print the large string values in android log cat.