0

first I did read other questions similar to this one, the one that seemed most helpful is this one, and tried using the top two answers to fix my issue, I have two alarm managers in main activity as shown here

private static final int NOTIFICATION_REMINDER_PROMPT = 0;
private static final int NOTIFICATION_REMINDER_UPDATE = 1;

if(!alarmIsRunning(this, new Intent(this, PromptReceiver.class))) {

    Intent notifyIntent = new Intent(this, PromptReceiver.class);
    PendingIntent pendingIntent = PendingIntent.getBroadcast
            (this, NOTIFICATION_REMINDER_PROMPT, notifyIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
    alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(),
            1000 * 60 * 60, pendingIntent);

}

if(!alarmIsRunning(this, new Intent(this, UpdateReceiver.class))) {
    Intent notifyIntent2 = new Intent(this, UpdateReceiver.class);
    PendingIntent pendingIntent2 = PendingIntent.getBroadcast
            (this, NOTIFICATION_REMINDER_UPDATE, notifyIntent2, PendingIntent.FLAG_UPDATE_CURRENT);
    AlarmManager alarmManager2 = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
    alarmManager2.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(),
            1000 * 60 * 60 * 50, pendingIntent2);
}

also the code for function that is used in the if function is as follows

public static boolean alarmIsRunning(Context context, Intent intent){
    return PendingIntent.getBroadcast(context, 0,
            intent, PendingIntent.FLAG_NO_CREATE) != null;
}

Now my issue is that when I debug, the first if condition always gives true, and the second always gives false and runs the code inside, but when I went for the second way which is using the adb shell dumpsys alarm line in my terminal, only the second one is working. There is something that seems to be I don't understand about this.

Rahul Singh Chandrabhan
  • 2,531
  • 5
  • 22
  • 33
newbieandroid
  • 161
  • 1
  • 2
  • 7

1 Answers1

1

When you fetch a pending intent to see if it exists, you need to recreate it exactly as you did before. So you'll need to pass the original request code to the alarmIsRunning() method.

Here is an extract from one of the methods I use for reference:

private fun doesPendingIntentExist(requestCode: Int, intent: Intent): Boolean {
    val tempPendingIntent = PendingIntent.getBroadcast(context,
            requestCode, intent,
            PendingIntent.FLAG_NO_CREATE)
    tempPendingIntent?.cancel()
    return tempPendingIntent != null
}

TL;DR: You need to pass the original request code to the PendingIntent.getBroadcast() method.

Benjamin Scholtz
  • 823
  • 6
  • 15
  • what is the piID and how do I get it? I get you are providing it is the requestcode, but how can i get it if I don't have reference to the original intent. – newbieandroid Jun 06 '18 at 10:58
  • I have renamed `piID` to `requestCode`. You're going to need to use the request code you originally used, no other way to do it - you can keep track of it by having a static variable, or keeping it in SharedPrefs, lots of options. In your case `NOTIFICATION_REMINDER_UPDATE` and `NOTIFICATION_REMINDER_PROMPT` are your request codes. – Benjamin Scholtz Jun 06 '18 at 12:41