为什么repeatingAlarm只运行了1次只 [英] Why repeatingAlarm only runs for 1 time only

查看:163
本文介绍了为什么repeatingAlarm只运行了1次只的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我工作的重复报警。问题是,我重复闹钟只运行只有在设定的时间一次,但在那个时候,第二天再重复。如果是2015年5月28日和时间为1:40 PM和我设置闹钟早上8:30与重复模式,那么就应该每天重复在上午8时30分,但问题是,它运行在2015年5月29日的上午08点半但不会在上午8:30于2015年5月30日运行,并进一步上。 这是我的code:

 如果(AM_PMSet!= NULL)
{    日历gcClone = Calendar.getInstance();
    日历GC =(日历)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);
    如果(gc.compareTo(gcClone)下; = 0){
        //今天设置时间过去了,算到明天
        gc.add(Calendar.DATE,1);
    }
    如果(repeatBool ==真){        长timeToAlarm = gc.getTimeInMillis();        的PendingIntent的PendingIntent;
        意图myIntent =新意图(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);        设置= getShared preferences(preFS_NAME,0);
        。settings.edit()putBoolean(setAlarm,真).commit();
        。settings.edit()putInt(setAlarmHour,hourSet).commit();
        。settings.edit()putInt(setAlarmMinute,minuteSet).commit();
        。settings.edit()putInt(setRepeating,1).commit();
    }否则如果(repeatBool == FALSE){
        的PendingIntent的PendingIntent;
        意图myIntent =新意图(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);
        设置= getShared preferences(preFS_NAME,0);
        。settings.edit()putBoolean(setAlarm,真).commit();
        。settings.edit()putInt(setAlarmHour,hourSet).commit();
        。settings.edit()putInt(setAlarmMinute,minuteSet).commit();
        。settings.edit()putInt(setRepeating,0).commit();    }}

在我的接收器类我打电话服务

 公共无效的onReceive(上下文的背景下,意图意图)
{
    playIntent =新意图(背景下,MusicService.class);
    playIntent.putExtra(宽,0);
    playIntent.putExtra(高度,0);
    playIntent.putExtra(densitydpi,0);
    playIntent.putExtra(fromMyReceiver,真);    context.startService(playIntent);
}

清单

 <服务
            机器人:名字=。MusicService
            机器人:启用=真/>
       <接收机器人:名字=。MyReceiver
机器人:工艺=远程/>


解决方案

在每天晚上电话:上午12点

 日历currentCalendar = Calendar.getInstance();
    日历todaysCalendar = Calendar.getInstance();
    todaysCalendar.setTime(GETDATE());
    日历nextDayCalandar = Calendar.getInstance();
    nextDayCalandar.setTimeInMillis(todaysCalendar.getTimeInMillis()
            +(3600000 * 24));    很长一段时间= Calendar.getInstance()。getTimeInMillis()
            +(nextDayCalandar.getTimeInMillis() - currentCalendar.getTimeInMillis());    意图myIntent =新意图(HomeActivity.this,MyReceiver.class);
    的PendingIntent = PendingIntent.getBroadcast(HomeActivity.this,0,myIntent,0);    AlarmManager alarmManager =(AlarmManager)getSystemService(ALARM_SERVICE);    如果(Build.VERSION.SDK_INT< Build.VERSION_ codeS.KITKAT)
    {
        alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,时间,3600000 * 24的PendingIntent);
    }
    其他
    {
        alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP,时间,3600000 * 24的PendingIntent);
    }

支持方法

  @燮pressLint(的SimpleDateFormat)
私人日期GETDATE()
{
    SimpleDateFormat的格式=新的SimpleDateFormat(DD-MM-YYYY);
    尝试
    {
        返回format.parse(format.format(新的Date()));
    }
    赶上(ParseException的E)
    {
        // TODO自动生成catch块
        e.printStackTrace();
    }
    返回新的Date();
}

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

解决方案

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

这篇关于为什么repeatingAlarm只运行了1次只的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆