一、在配置清单里静态注册BroadCastReceiverandroid
代码app
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.broadcastreceiver.boot"
android:versionCode="1"
android:versionName="1.0" >ide
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="21" />
<!-- 添加开机启动完成的权限 -->
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />spa
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />xml
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!-- 静态注册广播接收者(实施监听) -->
<receiver
android:name=".BootReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
</application>utf-8
=================get
二、BootReceiver类string
代码it
public class BootReceiver extends BroadcastReceiver {io
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
Toast.makeText(context, "监听到开机完成", Toast.LENGTH_LONG).show();
Intent intent1 = new Intent(context,MainActivity.class);
//必需要添加这个标签 不然启动失败
intent1.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//跳转
context.startActivity(intent1);
}
}