Search code examples
androidalarmmanagerandroid-notifications

Scheduled Notifications are not shown when process is killed


I have a scheduler kind of app that sends a notification to the user at a specified time in the morning and in the evening. I have done this using Alarm Manager. On the emulator the application works just fine as far as this is concerned. However in my device I use "Advanced Task Killer" application to kill running apps and free the device's memory. I noticed that on killing this application the notifications are not shown at the scheduled time. This definitely seems logical but is not at all what I want. I want the notifications to be shown at the scheduled time even though the process is killed. Is there a way to do so?

I figured a little bit of code might be helpful to realize my problem :

I have this in my Main Activity

public void setAlarm(){
    Intent intent = new Intent(MainActivity.this, AlarmReceiver.class);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(MainActivity.this, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
    am.cancel(pendingIntent);
    Cursor tempcursor = db.getAlarmTime();
    tempcursor.moveToFirst();
    int hour = tempcursor.getInt(tempcursor.getColumnIndex("Hour"));
    int minute = tempcursor.getInt(tempcursor.getColumnIndex("Minute"));
    tempcursor.close();
    db.close();
    GregorianCalendar alarmtime = new GregorianCalendar();
    alarmtime.set(GregorianCalendar.HOUR_OF_DAY, hour);
    alarmtime.set(GregorianCalendar.MINUTE, minute);
    alarmtime.set(GregorianCalendar.SECOND, 0);
    alarmtime.set(GregorianCalendar.MILLISECOND, 0);
    if(alarmtime.before(new GregorianCalendar()))alarmtime.add(GregorianCalendar.DAY_OF_MONTH, 1);
    am.set(AlarmManager.RTC_WAKEUP, alarmtime.getTimeInMillis(), pendingIntent);

}

And this in my AlarmReceiver class that extends BroadcastReceiver :

public void onReceive(Context context, Intent intent) {
    notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    Intent i = new Intent(context, Schedule_Today.class);
    PendingIntent pi = PendingIntent.getActivity(context, 0, i, PendingIntent.FLAG_CANCEL_CURRENT);

    AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    PendingIntent pi2 = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);

    am.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + AlarmManager.INTERVAL_DAY, pi2); //Setting another notification after 1 day as soon this notification broadcast is received.


        CharSequence from = "Scheduler_3";
        CharSequence message = "Test Notification";
        notification = new Notification(R.drawable.ic_launcher, "Attention",System.currentTimeMillis());
        notification.setLatestEventInfo(context, from, message, pi);


    notificationManager.notify(1, notification);
}

Solution

  • I want to prevent the application from being killed or at least a module of it running that can broadcast the alarmmanager at the required time so that the user receives notifications

    This is not strictly possible, except by making your own version of Android in your own ROM mod.

    On Android 2.1 and earlier, third-party task managers, like "Advanced Task Killer", had the ability to "force stop" an application. On Android 2.2 and higher, that ability was reserved for the OS itself, and is available to users via the "Force Stop" button on the app's screen in the list of applications in Settings.

    When an app is "force stopped", among other things, all scheduled alarms are removed. In addition, on Android 3.1+, nothing of that app will ever run again, until the user manually launches one of your activities (or something else manually runs one of your components).

    You are welcome to write two applications, one that is the main app and the other than ensures that, if the first one appears to have been force-stopped, the alarms are rescheduled. However, there is nothing stopping the user from force-stopping both of those applications.

    Also, bear in mind that some devices, like the SONY Xperia Z, block _WAKEUP alarms in general, if the user has activated "STAMINA Mode". See this blog post for more about this.

    Hence, I recommend that you redesign your application to take into account that your alarms are not guaranteed to run at all, let alone at the time you expect.