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:
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?
EDIT
As @Frank has suggested, I implemented a BroadcastReceiver, but it's never called.
In the Manifest:
<receiver android:name=".synchro.BootReceiver"
android:enabled="false">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
The class:
public class BootReceiver extends BroadcastReceiver {
List<Integer> selectedDays;
SharedPreferences preferences;
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals("android.intent.action.BOOT_COMPLETED")) {
**\\ STUFF HAPPENS HERE \\ **
}
}
}
The broadcaster is initiated in the scheduleAlarm method listed above, as follows:
ComponentName receiver = new ComponentName(getApplicationContext(), BootReceiver.class);
PackageManager pm = getApplicationContext().getPackageManager();
pm.setComponentEnabledSetting(receiver,
PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
PackageManager.DONT_KILL_APP);
Any more ideas?