开机广播

开机广播监听

     1.添加权限     

  2.编写一个广播
   
public class BootBroadcastReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)) {
            //example:启动程序
            Intent start = new Intent(context, MainActivity.class);
            start.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);//
            context.startActivity(start);
        }
    }
}
   3.在AndroidManifest.xml文件中注册广播接收器
 
  
<receiver android:name=".BootBroadcastReceiver" >
    <intent-filter>
        
        <action android:name="android.intent.action.BOOT_COMPLETED" />
    intent-filter>
receiver>

 
  
 
  
 
  

你可能感兴趣的:(开机广播)