1. 开机启动广播
大家都知道,1.申请权限 2.注册广播
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<receiver android:name=".StartServiceAtBootReceiver"
android:enabled="true"
android:exported="false"
android:label="StartServiceAtBootReceiver">
<intent-filter>
<action android:name="android.intent.action._BOOT_COMPLETED"/>
</intent-filter>
</receiver>实际上, 不申请以下权限, 开机一样会启动。这可能是android的一个Bug。
android.permission.RECEIVE_BOOT_COMPLETED参考:http://stackoverflow.com/questions/4635353/is-android-permission-receive-boot-completed-not-required
2. 屏幕开启/关闭广播
在Manifest.xml中注册不行,在代码中注册才行.
<receiver android:name="IntentReceiver">
<intent-filter>
<action android:name="android.intent.action.SCREEN_ON"></action>
</intent-filter>
</receiver>像这样注册,当屏幕点亮时,是无法接收到广播的。只有这样才行..
registerReceiver(new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
// ...
}
}, new IntentFilter(Intent.ACTION_SCREEN_ON));3.
本文介绍了在Android中实现开机启动广播及屏幕开关广播的方法。对于开机启动广播,即使未声明特定权限,应用也能在设备重启后自动启动服务。而对于屏幕开关广播,直接在清单文件中注册接收器将无法正常工作,需要通过代码动态注册。
2215

被折叠的 条评论
为什么被折叠?



