android monitors cell phone boot

Keywords: Android xml


Today, let's talk about it briefly.


android monitors cell phone boot.



Requirement Description: Sometimes, we need our program to run automatically after booting, and when the system is about to shut down, we can write some records to the specified files.

1. Boot-up radio monitoring:

After the Android system is started, the start-up completion broadcast (android.intent.action.BOOT_COMPLETED) will be issued, which will be received by all registered Broadcast Receiver receiving the start-up completion broadcast.

1. Add permission to grant application access to system boot events in AndroidManifest.xml file

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


2. Boot-up receiver sent by compiling system to complete broadcasting. The custom class inherits the BroadcastReceiver class with the following code:

package com.android.test;

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

/**
 * Class name: BootBroadcast Receiver 
 * Function Description: The Receiver of the Broadcast from the System at Start-up
 * #<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
 * @author android_ls
 */
public class BootBroadcastReceiver extends BroadcastReceiver {

    private static final String TAG = "BootBroadcastReceiver";

    private static final String ACTION_BOOT = "android.intent.action.BOOT_COMPLETED";

    @Override
    public void onReceive(Context context, Intent intent) {
        Log.i(TAG, "Boot this system , BootBroadcastReceiver onReceive()");

        if (intent.getAction().equals(ACTION_BOOT)) {
            Log.i(TAG, "BootBroadcastReceiver onReceive(), Do thing!");
        }
    }
}


3. Register a broadcast receiver in the Android Manifest.xml file

   <receiver android:name="com.android.test.BootBroadcastReceiver" >
            <intent-filter >
                <action android:name="android.intent.action.BOOT_COMPLETED" />

                <category android:name="android.intent.category.HOME" />
            </intent-filter>
        </receiver>


2. Shutdown radio monitoring:

The Android system provides shutdown broadcasting corresponding to boot-up broadcasting, which is issued when the system is about to shut down.

1. Write a receiver that broadcasts when the system is about to shut down. The custom class inherits the BroadcastReceiver class with the following code:

package com.android.test;

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

/**
 * Class name: Shutdown Broadcast Receiver
 * Function Description: Receiver for broadcasting when the system is about to shut down
 * @author android_ls
 */
public class ShutdownBroadcastReceiver extends BroadcastReceiver {

    private static final String TAG = "ShutdownBroadcastReceiver";
    
	private static final String ACTION_SHUTDOWN = "android.intent.action.ACTION_SHUTDOWN";
	
	@Override
	public void onReceive(Context context, Intent intent) {
	    Log.i(TAG, "Shut down this system, ShutdownBroadcastReceiver onReceive()");
	    
		if (intent.getAction().equals(ACTION_SHUTDOWN)) {
		    Log.i(TAG, "ShutdownBroadcastReceiver onReceive(), Do thing!");
		}
	}
}


2. Register a broadcast receiver in the Android Manifest.xml file

   <receiver android:name="com.android.test.ShutdownBroadcastReceiver" >
            <intent-filter >
                <action android:name="android.intent.action.ACTION_SHUTDOWN" />

                <category android:name="android.intent.category.HOME" />
            </intent-filter>
        </receiver>


One thing to note here is that this registered boot broadcast must be started once after the app installation before it can be monitored. And for Android below 4.0, it can be monitored normally. For those with android 4.0 or more, it is necessary to authorize app to boot from the phone to listen to the boot broadcast.


android listens on the phone and it's over.


It's that simple.



Posted by ganesh129 on Sat, 25 May 2019 15:40:53 -0700