Android developers monitor the status of various broadcasting systems

Foreword

  Cipian various broadcasting systems for recording status blog

Radio listeners boot

  Registration static broadcast

        <receiver android:name=".receiver.MyReceiver">
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED"/>
            </intent-filter>
        </receiver>

  Add Permissions

   <! - to receive permission to broadcast the boot, the boot is very dangerous behavior, so they need this right -> 
    < uses-permission Android: name = "android.permission.RECEIVE_BOOT_COMPLETED"  />

  Receiving broadcast

public class MyReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent.getAction().equals("android.intent.action.BOOT_COMPLETED")){
            Le ( "the device is turned on" );
        }
    }
}

Broadcast monitor off

  Registration static broadcast

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

  Receiving broadcast

public class ShutdownReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent.getAction().equals("android.intent.action.ACTION_SHUTDOWN")){
            Le ( "equipment shut down ..." );
        }
    }
}

 

 

 

 

 

 

end

Guess you like

Origin www.cnblogs.com/guanxinjing/p/12111619.html