Oiproks Oiproks - 3 years ago 205
Android Question

Reminder in Android doesn't repeat

I'm trying to set a reminder in my Android app, which will go off on certain days at the same set time.
Everything works fine for the first alarm, but then it doesn't repeat, nor goes off for other days.

Following is the interface:

enter image description here

Clicking on "Save Changes", it will be called the scheduleAlarm method for each selected day:

private void scheduleAlarm(int dayOfWeek) {
Intent myIntent = new Intent(this , NotifyService.class);
PendingIntent pendingIntent = PendingIntent.getService(this, 0, myIntent, 0);

AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);

if (dayOfWeek == 0)
alarmManager.cancel(pendingIntent);
else {
Calendar calendar = Calendar.getInstance();
String time_str[] = reminder_time.getText().toString().split(":");
calendar.set(Calendar.DAY_OF_WEEK, dayOfWeek);
calendar.set(Calendar.HOUR_OF_DAY, Integer.parseInt(time_str[0]));
calendar.set(Calendar.MINUTE, Integer.parseInt(time_str[1]));
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MILLISECOND, 0);

Long time = calendar.getTimeInMillis();
Long weekly = AlarmManager.INTERVAL_HOUR / 12; //AlarmManager.INTERVAL_DAY * 7;

alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, time, weekly, pendingIntent);
}
}


As you can see in this version I was trying to repeat the alarm every 5 minutes (
Long weekly = AlarmManager.INTERVAL_HOUR / 12;
)

The notification service called when the alarm goes off is as follows:

public class NotifyService extends Service {
public NotifyService() {
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
//TODO do something useful
return Service.START_STICKY;
}

@Override
public IBinder onBind(Intent intent) {
// TODO: Return the communication channel to the service.
throw new UnsupportedOperationException("Not yet implemented");
}

@Override
public void onCreate() {
Intent intent = new Intent(this , Splash.class);
intent.putExtra(getString(R.string.NOTIFICATION), true);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, intent, 0);

Bitmap bm = BitmapFactory.decodeResource(getResources(), R.drawable.notification_icon_ensafe);
long[] pattern = {500,500,500,500,500,500,500,500,500};

NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
.setContentTitle(getString(R.string.reminder))
.setContentText(getString(R.string.reminder_body))
.setLargeIcon(bm)
.setSmallIcon(R.drawable.notification_icon_ensafe)
.setContentIntent(contentIntent)
.setAutoCancel(true)
.setVibrate(pattern)
.setStyle(new NotificationCompat.InboxStyle())
.setSound(Settings.System.DEFAULT_NOTIFICATION_URI);

NotificationManager notificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(1, mBuilder.build());
}
}


Any idea why it is not working?

Answer Source

If your device goes off, all scheduled alarms are cancelled by the OS. Might that be your problem?

To solve this, you'll have to listen to device startups with a broadcast listener and schedule your alarms again when the device started broadcast comes in.

* EDIT *

All info you need is on this page: https://developer.android.com/training/scheduling/alarms.html

Especially on "Start an Alarm When the Device Boots" section: "By default, all alarms are canceled when a device shuts down. To prevent this from happening..."

It takes a bit of time to setup and test. Not a quicky.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download