广播类没有收到BOOT_COMPLETE的广播意图

时间:2017-07-21 15:30:40

标签: java android alarmmanager android-notifications bootcompleted

我需要在重新启动后恢复警报,我添加了这个广播接收器:

public class ClsRestartAlarm extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {

    if ("android.intent.action.BOOT_COMPLETED".equals(intent.getAction())) {

        Logging.logMessage("Broadcast");
        Intent i = new Intent(context, BootService.class);
        context.startService(i);
    }
}
}

并在清单中注册如下:

  <receiver android:name=".classes.ClsRestartAlarm"
        android:enabled="true">
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED" />
        </intent-filter>
    </receiver>

由于广播我正在这样做:

public class BootService extends IntentService {

public BootService() {
    super("boot service");
}

@Override
protected void onHandleIntent(Intent intent) {
    AlarmManagerUtils.setStartAlarm();
    AlarmManagerUtils.setEndAlarm();
}
}

我想我没有在ClsRestartAlarm类中接收BOOT_COMPLETE广播,因为重启后警报未设置且我无法获得通知(启动警报启动作业调度程序以发送通知和结束警报取消作业调度程序)也是拥有BOOT_COMPLETE权限:

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

1 个答案:

答案 0 :(得分:0)

请改用WakefulBroadcastReceiver。这是我可行的解决方案:

    public class BRAutoStart extends WakefulBroadcastReceiver {   
        private final String BOOT_COMPLETED_ACTION = "android.intent.action.BOOT_COMPLETED";
        @Override
        public void onReceive(Context ctx, Intent intent) {
            _A.APPCTX = ctx.getApplicationContext();
            if(intent.getAction().equals(BOOT_COMPLETED_ACTION)){
                //code
            }
        }
    }

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

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