Search code examples
androidalarmmanagerandroid-pendingintent

Getting 'java.lang.IllegalArgumentException: Can't use FLAG_RECEIVER_BOOT_UPGRADE here' in device running android 2.3.3


I have an activity which lets user to schedule an event by choosing the date and time.I have done it using the AlarmManager

Intent i=new Intent(context, AlarmReceiver.class);
i.putExtra("status",status);
i.putExtra("mode",mode);
PendingIntent p=PendingIntent.getBroadcast(context, id, i, PendingIntent.FLAG_CANCEL_CURRENT);
AlarmManager alarms = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarms.set(AlarmManager.RTC_WAKEUP, sched_time, p);

In the broadcast receiver a new Activity is launched

    @Override
    public void onReceive(Context context, Intent data) {
    // Start the scheduled post
    String status=data.getExtras().getString("status");
    int mode=data.getExtras().getInt("mode");

    Intent i=new Intent(context,PostScheduled.class);
    i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    i.putExtra("status", status);
    i.putExtra("mode", mode);

    context.startActivity(i);
}

In the emulator this code runs fine but in actual device(running android 2.3.3) it crashes at the line

PendingIntent p=PendingIntent.getBroadcast(context, id, i, PendingIntent.FLAG_CANCEL_CURRENT);

Here's the Stack trace

java.lang.IllegalArgumentException: Can't use FLAG_RECEIVER_BOOT_UPGRADE here
at android.os.Parcel.readException(Parcel.java:1326)
at android.os.Parcel.readException(Parcel.java:1276)
at android.app.ActivityManagerProxy.getIntentSender(ActivityManagerNative.java:2254)
at android.app.PendingIntent.getBroadcast(PendingIntent.java:230)
at com.social.autofunnystatus.ScheduleDialog.onClick(ScheduleDialog.java:158)
at android.view.View.performClick(View.java:2538)
at android.view.View$PerformClick.run(View.java:9152)
at android.os.Handler.handleCallback(Handler.java:587)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:3687)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:842)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
at dalvik.system.NativeStart.main(Native Method) 

Anyone has any clue what's going wrong?


Solution

  • The FLAG_RECEIVER_BOOT_UPGRADE flag is a "decoration" tacked on by the ActivityManagerService when you specifically define the broadcast target like you do here:

    Intent i=new Intent(context, AlarmReceiver.class);
    

    If you were to define an action, e.g. "com.whateveryour.package.is.ACTION_ALARM" and declare the AlarmReceiver in a <receiver /> in your AndroidManifest.xml - and then create the Intent like this:

    Intent i=new Intent("com.whateveryour.package.is.ACTION_ALARM");
    i.putExtra("status",status);
    i.putExtra("mode",mode);
    PendingIntent p=PendingIntent.getBroadcast(context, id, i,PendingIntent.FLAG_CANCEL_CURRENT);
    

    you'd get rid of that pesky flag that's blocking you.