Broadcast class not receiving broadcast for BOOT_COMPLETE

I needed to restore the alarm after reboot. I added this broadcast receiver:

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);
    }
}
}

      

and is registered in the manifest like this:

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

      

and as a result of the broadcast, I do this:

public class BootService extends IntentService {

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

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

      

I am guessing that I am not getting the BOOT_COMPLETE broadcast in the ClsRestartAlarm class because the alarm was not set after reboot and I could not receive the notification (the start alarm triggers the task scheduler to send the notification and end the alarm, cancels the task scheduler), also I have BOOT_COMPLETE permission:

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

      

+3


source to share


1 answer


Use WakefulBroadcastReceiver

instead. This is my workable solution:



    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" />

      

0


source







All Articles