30942

Why repeatingAlarm only runs for 1 time only

Question:

I am working on repeating alarm . problem is that my repeat alarm only runs for one time only on set time but not repeat on that time next day. If it is 5/28/2015 and time is 1:40PM and I set alarm to 8:30am with repeating mode then it should repeat everyday on 8:30am but problem is that it runs on 5/29/2015 on 8:30AM but will not run on 5/30/2015 at 8:30AM and further on. <strong>Here is my code:</strong>

if(AM_PMSet!=null) { Calendar gcClone = Calendar.getInstance(); Calendar gc = (Calendar) gcClone.clone(); //Log.e("-------------------","---"+hourSet); gc.set( Calendar.HOUR_OF_DAY, hourSet ); gc.set( Calendar.MINUTE, minuteSet); gc.set( Calendar.SECOND, 0 ); gc.set( Calendar.MILLISECOND, 0 ); if(gc.compareTo(gcClone) <= 0){ //Today Set time passed, count to tomorrow gc.add(Calendar.DATE, 1); } if(repeatBool==true){ long timeToAlarm = gc.getTimeInMillis(); PendingIntent pendingIntent; Intent myIntent = new Intent(AyatRuqyaActivity.this, MyReceiver.class); pendingIntent = PendingIntent.getBroadcast(AyatRuqyaActivity.this, RQS_1, myIntent,PendingIntent.FLAG_UPDATE_CURRENT); AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE); alarmManager.set(AlarmManager.RTC_WAKEUP, gc.getTimeInMillis(), pendingIntent); alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, timeToAlarm,24*60*60*1000, pendingIntent); settings = getSharedPreferences(PREFS_NAME, 0); settings.edit().putBoolean("setAlarm", true).commit(); settings.edit().putInt("setAlarmHour", hourSet).commit(); settings.edit().putInt("setAlarmMinute", minuteSet).commit(); settings.edit().putInt("setRepeating", 1).commit(); }else if(repeatBool==false){ PendingIntent pendingIntent; Intent myIntent = new Intent(AyatRuqyaActivity.this, MyReceiver.class); pendingIntent = PendingIntent.getBroadcast(AyatRuqyaActivity.this, RQS_1, myIntent,PendingIntent.FLAG_UPDATE_CURRENT); AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE); alarmManager.set(AlarmManager.RTC_WAKEUP, gc.getTimeInMillis(), pendingIntent); settings = getSharedPreferences(PREFS_NAME, 0); settings.edit().putBoolean("setAlarm", true).commit(); settings.edit().putInt("setAlarmHour", hourSet).commit(); settings.edit().putInt("setAlarmMinute", minuteSet).commit(); settings.edit().putInt("setRepeating", 0).commit(); } }

<strong>In my Receiver Class I am calling a service</strong>

public void onReceive(Context context, Intent intent) { playIntent = new Intent(context, MusicService.class); playIntent.putExtra("width", 0); playIntent.putExtra("height", 0); playIntent.putExtra("densitydpi", 0); playIntent.putExtra("fromMyReceiver", "true"); context.startService(playIntent); }

<strong>Manifest</strong>

<service android:name=".MusicService" android:enabled="true" /> <receiver android:name=".MyReceiver" android:process="remote" />

Answer1:

call at every day at night : 12 AM

Calendar currentCalendar = Calendar.getInstance(); Calendar todaysCalendar = Calendar.getInstance(); todaysCalendar.setTime(getDate()); Calendar nextDayCalandar = Calendar.getInstance(); nextDayCalandar.setTimeInMillis(todaysCalendar.getTimeInMillis() + (3600000 * 24)); long time = Calendar.getInstance().getTimeInMillis() + (nextDayCalandar.getTimeInMillis() - currentCalendar.getTimeInMillis()); Intent myIntent = new Intent(HomeActivity.this, MyReceiver.class); pendingIntent = PendingIntent.getBroadcast(HomeActivity.this, 0, myIntent, 0); AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE); if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) { alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, time, 3600000 * 24, pendingIntent); } else { alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, time, 3600000 * 24, pendingIntent); }

supportive method

@SuppressLint("SimpleDateFormat") private Date getDate() { SimpleDateFormat format = new SimpleDateFormat("dd-MM-yyyy"); try { return format.parse(format.format(new Date())); } catch (ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); } return new Date(); }

Recommend

  • How to implement Firebase Cloud messaging with Foreground application?
  • Unwanted duplicated activity
  • Android - How to create Multiple instances of an Activity?
  • android activity comes to foreground after alarm manager
  • schedule two tasks subsequently in android
  • Android activity transition on list view item click
  • how to get values from previous activity
  • Determining if time is between two other times regardless of date [duplicate]
  • How to create fragments in Android studios from existing activities
  • getTag = null pointer
  • Programmatically setting up a vpn on Android
  • Android Store Listview data in SQlite database and retrieve data oncreate of activity in Listview
  • Multiple Alarms Restart After Boot
  • Set AlarmManager from DatePicker and TimePicker
  • How to pass data from Activity to Service without starting a new instance?
  • set notification on alarm receiver android
  • AlarmManager to fire notification at every 24 hours
  • How to set phone's alarm from program code in Android
  • Android - Firebase Notification open an Activity when clicked
  • How to Create Forground Service Never Stop when Application is Inactive
  • Create reminder in android app
  • how to set date and month wise notification list using database in android
  • alarmmanager 2 times
  • What is the Context passed into onReceive() of a BroadcastReceiver?
  • Launch activity when user taps on a notification from the lockscreen
  • How to trigger an alarm only once in Android?
  • Android Notification Without R.java
  • AlarmManager is blocking main thread
  • Why is my AlarmManager firing off instantly?
  • Android NFC read Tags issue. Activity starts each time on data received
  • Send data from edittext to listview
  • How can go in last activity with resume button?
  • Android application: how to use the camera and grab the image bytes?
  • Dialing with Intent.ACTION_CALL stopps at # in phone number
  • Cannot Parse HTML Data Using Android / JSOUP
  • Java static initializers and reflection
  • unknown Exception android
  • Observable and ngFor in Angular 2
  • Unable to use reactive element in my shiny app