AndroidManifest内:
1、添加接收开机广播的权限
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
2、声明一个开机广播接收器,需要添加两个广播
android.media.AUDIO_BECOMING_NOISY 是开机铃声广播,有的手机不加这个的话,BOOT_COMPLETED好像收不到
<receiver
android:name=".receiver.BootReceiver"
android:exported="false">
<intent-filter android:priority="1000">
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.media.AUDIO_BECOMING_NOISY" />
</intent-filter>
</receiver>
3、广播接收处理
public class BootReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
KLog.i("BootReceiver 收到广播 : " + action);
if (Intent.ACTION_BOOT_COMPLETED.equals(action)) {
KLog.i("收到开机广播,启动程序");
Intent thisIntent = new Intent(context, SplashActivity.class);
thisIntent.setAction("android.intent.action.MAIN");
thisIntent.addCategory("android.intent.category.LAUNCHER");
thisIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(thisIntent);
}
}
}
注:广播在源码位置:
AudioManager.ACTION_AUDIO_BECOMING_NOISY