Why repeatingAlarm only runs for 1 time only

谁说我不能喝 提交于 2019-12-12 01:43:58

问题


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" />

回答1:


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


来源:https://stackoverflow.com/questions/30499111/why-repeatingalarm-only-runs-for-1-time-only

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!