我使用了这个权限:
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
接收者是:
<receiver android:name=".auth.NotificationBroadcast" android:enabled="true" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
代码中的接收者是:
@Override
public void onReceive(Context context, Intent intent) {
System.out.println("BroadcastReceiverBroadcast--------------------ReceiverBroadcastReceiverBroadcastReceiver----------------BroadcastReceiver");
if (intent != null) {
String action = intent.getAction();
switch (action) {
case Intent.ACTION_BOOT_COMPLETED:
System.out.println("Called on REBOOT");
// start a new service and repeat using alarm manager
break;
default:
break;
}
}
}
重新启动后,它仍然没有在 Lollipop 中被调用,但在棉花糖上它正在运行。
最佳答案
尝试将此行放入接收者的 intent-filter 中。
<action android:name="android.intent.action.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE" />
如果您的应用程序安装在 SD 卡上,您应该注册它以获取 android.intent.action.BOOT_COMPLETED 事件。
更新:由于您的应用正在使用闹钟服务,因此不应将其安装在外部存储设备上。引用:http://developer.android.com/guide/topics/data/install-location.html
关于Android 重启广播接收器后未运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39555388/