3

I needed to restore alarm after reboot for this 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 registered in 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 result of broadcast I'm doing this:

public class BootService extends IntentService {

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

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

I guess I'm not receiving BOOT_COMPLETE broadcast in ClsRestartAlarm class, because after restart alarm was not set and I was unable to get notification(the starting alarm start a job scheduler for sending notification and end alarm cancels job scheduler)also I have BOOT_COMPLETE permission like this:

<uses-permission android:name="ANDROID.PERMISSION.RECEIVE_BOOT_COMPLETED"/>
blackHawk
  • 6,047
  • 13
  • 57
  • 100

1 Answers1

0

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" />
Vyacheslav
  • 26,359
  • 19
  • 112
  • 194