为什么repeatingAlarm只运行一次

时间:2015-05-28 06:47:20

标签: android android-alarms

我正在努力重复闹钟。问题是我的重复闹钟只在设定的时间运行一次,但在第二天的那个时间不再重复。如果是2015年5月28日,时间是下午1:40,我将警报设置为上午8:30,重复模式,那么它应该每天早上8:30重复,但问题是它运行于2015年5月29日上午8:30但不会在2015年5月30日上午8:30和更晚的时间运行。 这是我的代码:

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

    }



}

在我的Receiver Class中,我正在调用服务

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


}

清单

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

1 个答案:

答案 0 :(得分:0)

每天晚上12点致电

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

支持方法

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