0

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. Here is my code:

 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();

    }



}

In my Receiver Class I am calling a service

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);


}

Manifest

    <service
            android:name=".MusicService"
            android:enabled="true" />
       <receiver android:name=".MyReceiver"
android:process="remote" />
User42590
  • 2,473
  • 12
  • 44
  • 85
  • Do you switch off device before comes next time(day)? – Palak May 28 '15 at 06:50
  • What is the Max SDK you use? Please post the Manifest entry, also you can check [this](http://stackoverflow.com/questions/21461191/alarmmanager-fires-alarms-at-wrong-time/21461246#21461246) – Skynet May 28 '15 at 06:51
  • @Palak no I did not switch off my device for 3 to 4 days for checking the alarm functionality. – User42590 May 28 '15 at 06:53
  • @Skynet I have set it to 19 level – User42590 May 28 '15 at 06:54
  • Check the link I posted in the above comment, Alarms work different in API 19. – Skynet May 28 '15 at 06:55
  • @Skynet Yes I checked both with setInexactRepeating and setRepeating but the problem is same. – User42590 May 28 '15 at 06:56
  • You need to use setExact(). Then you need to call this code each time you want to set the Alarm. So the cycle would be First Run - setExact - Call a module or method (Do processing) - on processing finished call the setALarm function again so that it sets the alarm as setExact() for the next round. – Skynet May 28 '15 at 06:57
  • @Skynet Means if the alarm call first time then in my onreceive I should again set the setExactalarm to next day so it could repeat. Am I write? – User42590 May 28 '15 at 07:11
  • As far as I know yes, but I havnt coded an Alarm since a long time, so I am not sure if things has changed. I suggest that you go with this method, but also do some research and see if there is a better way. – Skynet May 28 '15 at 07:19
  • I am searching from 4 to 5 days.And I tried all solutions on the net but I am still unable to understand why this is happening . – User42590 May 28 '15 at 07:23
  • This is by design you dont need to think why it is happening, the reason for this is that all Alarms are bundled together from API 19 and broadcast at a feasible time, so that battery consumption and resource contention is less. – Skynet May 28 '15 at 07:29

1 Answers1

0

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();
}
krunal shah
  • 364
  • 2
  • 14
  • This is not an answer to the question, just some jumbled code without any proof of working. The OP clearly asked that he wants an exact alarm everyday. – Skynet May 28 '15 at 07:07
  • its working in my app pefectly, what is issue in this code ? please suggest me. – krunal shah May 28 '15 at 07:08
  • It must be working in your app, please support your answer with more explaination. Plus read the question. There is a `huge difference` between `setExact()` and `setInexactRepeating()`. – Skynet May 28 '15 at 07:10
  • with more explaination, means now what to remain to add or support in code ? – krunal shah May 28 '15 at 07:15