静态注册广播接收器

新建继承自BroadcastReceiver的类,重写onReceive()方法

public class BootCompleteReceiver extends BroadcastReceiver {

    //接受设备开机的广播
    @Override
    public void onReceive(Context arg0, Intent arg1) {
        // TODO Auto-generated method stub
        Toast.makeText(arg0, "开机完成", Toast.LENGTH_SHORT).show();
    }

}

然后在AndroidManifest.xml文件中静态注册广播接收器

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

注意要声明权限,否则程序会崩溃

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

你可能感兴趣的:(android,静态注册广播接收器)