In my app, I am using alarm manager. I am setting alarms for a specific times.I am setting multiple alarms for specific future time as mentioned in below code:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.e("test", "on create is called");
SharedPreferences preferences = getSharedPreferences("testing", MODE_PRIVATE);
if (!preferences.getBoolean("alarm", false)) {
Log.d("test", "generating alrams");
setAlarm(getFirstAlarmTime(), 1, "one");
setAlarm(getSecondAlarmTime(), 2, "two");
setAlarm(getThirdAlarmTime(), 3 ," three");
setAlarm(getFourthAlarmTime(), 4, "four");
setAlarm(getFifthAlarmTime(), 5, "five");
preferences.edit().putBoolean("alarm", true).apply();
}
}
private long getFirstAlarmTime() {
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 13);
calendar.set(Calendar.MINUTE, 7);
calendar.set(Calendar.SECOND,0);
return calendar.getTimeInMillis();
}
private long getSecondAlarmTime() {
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 13);
calendar.set(Calendar.MINUTE, 8);
calendar.set(Calendar.SECOND,0);
return calendar.getTimeInMillis();
}
private long getThirdAlarmTime() {
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 13);
calendar.set(Calendar.MINUTE, 9);
calendar.set(Calendar.SECOND,0);
return calendar.getTimeInMillis();
}
private long getFourthAlarmTime() {
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 13);
calendar.set(Calendar.MINUTE, 10);
calendar.set(Calendar.SECOND,0);
return calendar.getTimeInMillis();
}
private long getFifthAlarmTime() {
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 13);
calendar.set(Calendar.MINUTE, 11);
calendar.set(Calendar.SECOND,0);
return calendar.getTimeInMillis();
}
private void setAlarm(long alarmTime, int id, String type){
Intent intent = new Intent(this, NotifyService.class);
intent.putExtra("id", id);
intent.putExtra("type", type);
PendingIntent alarmIntent = PendingIntent.getService(getApplicationContext(), id, intent, 0);
AlarmManager manager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
manager.set(AlarmManager.RTC_WAKEUP, alarmTime, alarmIntent);
}
}
And below is the service NotifyService I am calling in my intent
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
int id = 0;
String type = null;
if (intent != null) {
id = intent.getExtras().getInt("id");
type = intent.getExtras().getString("type");
Log.e("test", "current id is " + id);
Log.e("test", "type is " + type);
}
PendingIntent activity = PendingIntent.getActivity(getApplicationContext(), id, intent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationManager notificationManager = (NotificationManager) getApplicationContext().getSystemService(NOTIFICATION_SERVICE);
Uri azanSound = Uri.parse("android.resource://" + getApplicationContext().getPackageName() + "/raw/makkah_azan");
NotificationCompat.Builder builder = new NotificationCompat.Builder(getApplicationContext());
if(!muteStatus) {
builder.setContentTitle(type)
.setContentText(type + " time has started...")
.setAutoCancel(false)
.setSmallIcon(R.mipmap.ic_launcher)
.setSound(azanSound) // put sound here
.setContentIntent(activity);
}
Notification notification = builder.build();
notification.flags = Notification.FLAG_AUTO_CANCEL;
notificationManager.notify(id, notification);
return START_REDELIVER_INTENT ;
}
Every alarm is firing on specific time as mentioned in setAlarm() method, but the problem is when I kill my app and restart my app, Alarm manager starts firing pending intent again. Why is that so am i missing anything? I have spent a lot of time and google but could not found any solution. Any help?
One thing i have noticed that if cell phone time is lets say 12 o clock and alarm is set for 11 o clock and app is started alarm keeps repeating. :(