android开机广播;代码如下:
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;
/**
* 开机广播
* @author UNIVERSE
*
*/
public class BootCompletedReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Log.e("BootCompletedReceiver", "系统启动完毕");
}
}
AndroidManifest.xml文件中进行注册:
<receiver android:name="com.android.callrecords.receiver.BootCompletedReceiver">
<intent-filter
android:priority="1000">
<action android:name="android.intent.action.BOOT_COMPLETED"/>
<category android:name="android.intent.category.HOME" />
</intent-filter>
</receiver>
同时应添加所需要的权限:
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
既然Android系统在启动完毕后会发送广播,在系统关闭时是否也有对应的广播呢?通过查询帮助文档,找到了系统关闭的广播:
关机广播:
<receiver android:name=".ShutdownReceiver">
<intent-filter>
<action android:name="android.intent.action.ACTION_SHUTDOWN"/>
</intent-filter>
</receiver>
不过小米手机需要手动开启自动启动选项才能接收到广播,不然接受不到