0

I've built one small reminder app demo. Now the problem is sometimes reminder coming or sometimes not? Now, I have this feature that user can set the alarm for day, month, week, year.

Now how can I check for the month or year that notification is coming or not?

Obviously, when I manually go and change the Android date and time settings, alarm not coming. (Even for some minutes also)

See: When I change Android phone date and time, notification not getting, otherwise, if I do nothing, I get notification on time

Edit: Code:

public void setRepeatAlarm(Context context, Calendar calendar, int ID, long RepeatTime, int requestCode) {
        mAlarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);

        // Put Reminder ID in Intent Extra
        Intent intent = new Intent(context, AlarmReceiver.class);
        intent.putExtra(REMINDER_ID, Integer.toString(ID));


        mPendingIntent = PendingIntent.getBroadcast(context, ID, intent, PendingIntent.FLAG_CANCEL_CURRENT);
        mPendingIntent = PendingIntent.getBroadcast(context, requestCode, new Intent(context, AlarmReceiver.class),
                PendingIntent.FLAG_NO_CREATE);
         isAlarmSet = (mPendingIntent != null);
         if (isAlarmSet) {
             showLog("isAlarmSet: " + isAlarmSet);
         } else {
             showLog("isAlarmSet: " + isAlarmSet);
         }
        // Calculate notification timein
        Calendar c = Calendar.getInstance();
        long currentTime = c.getTimeInMillis();
        long diffTime = calendar.getTimeInMillis() - currentTime;

        // Start alarm using initial notification time and repeat interval time
        mAlarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME,
                SystemClock.elapsedRealtime() + diffTime,
                RepeatTime, mPendingIntent);

        // Restart alarm if device is rebooted
        ComponentName receiver = new ComponentName(context, BootReceiver.class);
        PackageManager pm = context.getPackageManager();
        pm.setComponentEnabledSetting(receiver,
                PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
                PackageManager.DONT_KILL_APP);
    }
Pooja Singh
  • 121
  • 6

3 Answers3

0

You can check for existing alarm using this :

PendingIntent pendingIntent = PendingIntent.getBroadcast(this, requestCode,
           new Intent(this, AlarmReceiver.class),
           PendingIntent.FLAG_NO_CREATE);
boolean isAlarmSet = (pendingIntent!=null);

Note that the requestCode here should be the same one used while setting the alarm. Also, AlarmReceiver is your BroadcastReceiver class for alarm


I have not tested this in the scenario you mentioned (changing the phone time), but you can refer this and set your alarm again according to the new time and cancel the previous one.

Shrey Garg
  • 1,317
  • 1
  • 7
  • 17
  • Hi, Thanks for the answer, Please check my edits, it is returning true, but I'm setting this also: PendingIntent.FLAG_CANCEL_CURRENT, is it correct? – Pooja Singh Mar 29 '19 at 06:12
  • @PoojaSingh, I tried to reproduce this scenario, I too got isAlarmSet as true. – Shrey Garg Mar 29 '19 at 06:23
  • See, we're just checking that pendingIntent is null or not, that doesn't mean that our alarm will come on correct time & date? How can I ensure? can I check something on my Android phone, any system file or something, that store this alarm information? – Pooja Singh Mar 29 '19 at 06:26
  • I suggest you to refer https://stackoverflow.com/questions/5481386/date-and-time-change-listener-in-android/6230951#6230951 and reset your alarms accordingly, if you want to persist alarms on time change. Also, please use a separate object of PendingIntent (in your code added to question) for checking isAlarmSet as you're using the same object to set an alarm later. – Shrey Garg Mar 29 '19 at 06:26
  • _that doesn't mean that our alarm will come on correct time & date?_ **correct.** _can I check something on my Android phone?_ **unfortunately, you cannot query AlarmManager for current Alarms** – Shrey Garg Mar 29 '19 at 06:32
  • When I create separate object of pending intent, it is returning false – Pooja Singh Mar 29 '19 at 07:17
  • That would be because you might not be using the **same** requestCode/ID i.e, the 2nd parameter in PendingIntent.getBroadcast(). Please use the same one and check again – Shrey Garg Mar 29 '19 at 07:22
  • I hv two class, one is main activity from where I call setRepeatAlarm method, and other is AlarmReceiver class which extends WakefulBroadcastReceiver and in this class setRepeatAlarm is written – Pooja Singh Mar 29 '19 at 07:24
  • Okay, then just move your first '_mPendingIntent =_' statement after _else {showLog("isAlarmSet: " + isAlarmSet);}_. This should do the trick – Shrey Garg Mar 29 '19 at 07:26
  • Still showing false, I don't understand, that where we're checking request code, I pass 1 as a request code in this method parameter. That's it (check the last param) – Pooja Singh Mar 29 '19 at 07:32
0

You have pretty knowledge about Design Pattern. Please follow the Observer Pattern to mange below thing and get always new time when you change time manually or not. It will check current time is changed or not. More details Please read Observer Pattern Documentation.

// Calculate notification timein
        Calendar c = Calendar.getInstance();
        long currentTime = c.getTimeInMillis();
        long diffTime = calendar.getTimeInMillis() - currentTime; 
Viral Patel
  • 1,296
  • 1
  • 11
  • 24
0

You should listen to time changes in the android system and act accordingly

<receiver android:name=".TimeChangedReceiver">
    <intent-filter >
        <action android:name="android.intent.action.TIME_SET"/>
    </intent-filter>
</receiver>

Broadcast receiver class

public class TimeChangedReceiver extends BroadcastReceiver{

@Override
public void onReceive(final Context arg0, Intent arg1) {
//Call your Alarm setting code
 // change your alarm accordingly
    }
}
Manoj Perumarath
  • 9,337
  • 8
  • 56
  • 77
  • I added, but not working in this scenario: https://stackoverflow.com/questions/55258407/mismatch-date-time-with-reminder – Pooja Singh Mar 29 '19 at 06:44