0

If I call the setinexactrepeating() alarm method in the onCreate , how does it affect the alarm which is being repeated at the specified interval

Here is the code of setting alarm, I am calling this method in the onCreate()

public void setAlarm()
{
    Intent myintent=new Intent(this,AlarmReciever.class);

    Random random = new Random();


    int ran = random.nextInt(total_words.size());


    String tempString=onlySearch(total_words.get(ran), 1);
    myintent.putExtra("word", total_words.get(ran));
    myintent.putExtra("meaning", tempString);
    myintent.putExtra("language", 1);


    alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);


    PendingIntent pendingIntent=PendingIntent.getBroadcast(this,101,myintent,PendingIntent.FLAG_UPDATE_CURRENT);

    //NotificationTime is the sharedPreference file, in which i am storing hours and minute got from timepicker        

    alarmManager.cancel(pendingIntent);
    Calendar calendar=Calendar.getInstance();
    calendar.setTimeInMillis(System.currentTimeMillis());
    calendar.set(Calendar.HOUR_OF_DAY, NotificationTime.getInt("hour", 12));
    calendar.set(Calendar.MINUTE, NotificationTime.getInt("min", 0));
    calendar.set(Calendar.SECOND, 0);


    if(calendar.before(Calendar.getInstance()))
        calendar.add(Calendar.DATE,1);



    alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
            AlarmManager.INTERVAL_DAY
            , pendingIntent);


    System.out.println("ALARM SET STATUS");
}

Alarm is first scheduling at the exact time perfectly but it does not gets repeated.

Actually I wanted to show notification daily at the specified time and I am passing a String with the intent. By default I had set to 12:00 PM but user can change its time. The first notification after setting time (or the first alarm) is working but its repetition is not working.

Please have a look, and if someone knows a better solution to show notification daily at the same specified time it would be appreciated if you tell me.

Thanks in advance

Aziz
  • 1,976
  • 20
  • 23

1 Answers1

0

user setRepeating for Repeating Alarm

 AlarmManager manager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
/* Repeating on every interval */
manager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);
Sanjay Kakadiya
  • 1,596
  • 1
  • 15
  • 32
  • what is the difference between setRepeating and setExactRepeating? – Aziz Jun 16 '16 at 09:26
  • You check difference here setRepeating https://developer.android.com/reference/android/app/AlarmManager.html#setRepeating(int, long, long, android.app.PendingIntent)) and setExactRepeating https://developer.android.com/reference/android/app/AlarmManager.html#setInexactRepeating(int, long, long, android.app.PendingIntent) – Sanjay Kakadiya Jun 16 '16 at 09:50
  • Thank you, but one more ques. When does our alarm is destroyed, i mean if i set the alarm on applications first run then, then whats the factor that might disable my alarm.? – Aziz Jun 16 '16 at 11:18
  • any factor does not disable your alarm still you do not call alarmManager.cancel(); – Sanjay Kakadiya Jun 16 '16 at 12:01
  • Does the repeating alarm also not be disabled? because when I close my app from the recent app list in my phone, the alarm does not repeated. – Aziz Jun 16 '16 at 12:21
  • I'm not fetching this issue. Did you add AlarmReciever in Manifest.xml ? – Sanjay Kakadiya Jun 17 '16 at 06:07
  • http://stackoverflow.com/q/37865748/6330489 here my complete code. You can check, i ve mentioned the actual issue in thi post – Aziz Jun 17 '16 at 08:54