1

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. :(

  • AlarmManager works independently of your app and will fire Intents even when the app is killed. What are you trying to achieve exactly? – SpaceBison Jun 20 '17 at 10:50
  • Alarm should only be fired when specific time occurs as mentioned while setting alarm. Not after that.In current situation if i restart my app and app time is way after the actual alarm time is set, but it starts firing pending intent. Why is that so? –  Jun 20 '17 at 11:08

1 Answers1

0

Are you sure the alarm is actually firing after you kill your app and it is not simply your intent being delivered again as you are requesting by returning START_REDELIVER_INTENT in your NotifyService.

'START_REDELIVER_INTENT' is basically requesting that the intent should be repeated if the task is killed. For you use-case, it would appear that you need to return START_NOT_STICKY as you only want to fire the intent once.

What is START_STICKY,START_NOT_STICKY and START_REDELIVER_INTENT Service

Also, in answer to

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.

Any alarms that are set for a time in the past will be fired instantly.

Kuffs
  • 35,581
  • 10
  • 79
  • 92
  • I need to know one more thing, in my code I am setting 5 alarms with different specific timings. Now I want to set 5 more alarms when last alram hits. For this I am sending "id" with every intent and in method onStartCommand() I am checking "id" key (that was sent via key value pair of intent) if that has a key with "id" and value is number 5 I need to generate 5 more alarms. and I am doing the same but my question is do i need to cancel previous 5 alarms? will previous 5 alarms be invoked because they were also registered?If I need to cancel previous 5 then how can i do so? –  Jun 21 '17 at 03:50
  • I googled and found that I need to get same PendingIntent to cancel how can I get the same pending intent?As i create 5 alarms with each pendingintent how is that possible to keep that same intent store? Any help? –  Jun 21 '17 at 03:51
  • 1
    You should open a new question rather than adding additional questions as comments to answers on existing questions. Your additional questions would not be easy to answer in the space provided for comments. If my answer above has answered your original question, please mark it as accepted. If you post a new question, paste the link here and I will make sure I take a look. – Kuffs Jun 21 '17 at 09:18
  • your answer was really very helpful and that solved my problem. –  Jun 22 '17 at 06:06