简体   繁体   English

AlarmManager在错误的时间响应

[英]AlarmManager responding at wrong time

Sorry if answered before, but i looked everywhere but didn't get the proper solution 对不起,如果以前回答过,但是我到处都是,但是没有找到合适的解决方案

I am using AlarmManager to automatically fire a notification at 9am everyday, but when i try to run it on emulator it executes immediately, and every half hour (31-32min to be precise) after that instead of only once at 9am everyday. 我正在使用AlarmManager每天早上9点自动触发通知,但是当我尝试在模拟器上运行它时,它会立即执行,然后每半小时(准确地说是31-32分钟)执行一次,而不是每天早上9点才执行一次。

Any ideas why? 有什么想法吗? help appreciated. 帮助赞赏。

code is as below: 代码如下:

public class Home extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_bsheet);

    notificationAlert(savedInstanceState);

}

    private void notificationAlert(Bundle savedInstanceState) {

    AlarmManager manager;
    PendingIntent pendingIntent;
    Intent intent=new Intent(Home.this, Notify.class);
    manager=(AlarmManager)getSystemService(ALARM_SERVICE);
    pendingIntent=PendingIntent.getService(Home.this,
            0, intent, 0);
    GregorianCalendar gcal = new GregorianCalendar();
    gcal.set(Calendar.HOUR_OF_DAY, 9);
    gcal.set(Calendar.MINUTE, 0);
    gcal.set(Calendar.SECOND, 0);
    gcal.set(Calendar.MILLISECOND, 0);

    long initTime = gcal.getTimeInMillis();

    manager.setRepeating(AlarmManager.RTC_WAKEUP, initTime,
            24*60*60*1000, pendingIntent);
}

}

cheers, 干杯,

EDIT: my intention is that, once the app is installed, it fires this alarm at 9am. 编辑:我的意图是,一旦安装了该应用程序,它将在上午9点触发此警报。 i have put the alarm in the onCreate, so im not sure if the alarm is only being created everytime i start the app and something weird is happening when i hide the app... again insight would be appreciated! 我已将警报放在onCreate中,所以我不确定是否仅在每次启动应用程序时才创建警报,而当我隐藏应用程序时发生了一些奇怪的事情……再次感谢您的见解!

try this : 尝试这个 :

    Calendar currentTime = Calendar.getInstance();
    Calendar alarmTime = Calendar.getInstance();
    currentTime.set(Calendar.HOUR_OF_DAY, 9;
    currentTime.set(Calendar.MINUTE, 0);
    currentTime.set(Calendar.SECOND, 0);
    currentTime.set(Calendar.MILLISECOND, 0);
    if (alarmTime.compareTo(currentTime) <= 0) {
// check for time
alarmTime.add(Calendar.DATE, 1);
}

Intent intent = new Intent(getBaseContext(), Receiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(
    getBaseContext(), 1, intent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
am.setRepeating(AlarmManager.RTC_WAKEUP, alarmTime.getTimeInMillis(),
                        AlarmManager.INTERVAL_DAY, pendingIntent);
manager.setRepeating(AlarmManager.RTC_WAKEUP, initTime, 24*60*60*1000, pendingIntent);

Alarm manager will fire immidiatly if initTime < System.currentTimeMillis() 如果initTime < System.currentTimeMillis()警报管理器将立即触发

from docs : 来自docs

If the time occurs in the past, the alarm will be triggered immediately, with an alarm count depending on how far in the past the trigger time is relative to the repeat interval. 如果时间是过去的时间,则将立即触发警报,并根据过去的触发时间相对于重复间隔的时间来计算警报数。

According to the code you've provided, gcal.getTimeInMillis() will return millisecods corresponding to Today 9.00 . 根据您提供的代码, gcal.getTimeInMillis()将返回对应于Today 9.00的 millisecods。 So if you'll try to run your code after this time, initTime will be less then current system time which triggers immidiate run of AlarmManager. 因此,如果您在这段时间之后尝试运行代码,则initTime将小于当前系统时间,这将触发AlarmManager的立即运行。

To fix this, for example, you can add one day to your calendar before passing its gcal.getTimeInMillis() if it is already in past so it will point to Tomorrow 9.00 to let it run tomorrows morning 例如,要解决此问题,您可以在日历中添加一天,然后再通过gcal.getTimeInMillis()如果已经过去),那么它将指向明天9.00,以便明天早上运行

Update1 更新1

Tried code like this and it worked as expected for me - fired service every 10 seconds: 尝试过这样的代码,它对我来说像预期的那样工作-每10秒触发一次服务:

    @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    initManager();
}
private void initManager() {
    AlarmManager manager;
    PendingIntent pendingIntent;
    Intent intent = new Intent(this, NotifyService.class);
    manager=(AlarmManager)getSystemService(ALARM_SERVICE);
    pendingIntent=PendingIntent.getService(this, 0, intent, 0);

    long initTime = System.currentTimeMillis() + 5000;

    manager.setRepeating(AlarmManager.RTC_WAKEUP, initTime,
            10*1000, pendingIntent);        
}

However you can use another option: there is AlarmManager.set() method You can fire it like this 但是,您可以使用另一个选项:有AlarmManager.set()方法,您可以像这样触发它

long runTime = /* your calendar time + 24 hours in millis*/
manager.set(AlarmManager.RTC_WAKEUP, runTime, pendingIntent);       

so it will fire your service at runTime . 因此它将在runTime触发您的服务。 And then in the service invoke the same method with recalculated runTime for another day so it could look something like that: 然后在该服务中,另一天用重新计算的runTime调用相同的方法,因此它看起来像这样:

public MainActivity extends Activity{

    protected onCreate(Bundle bundle){
        ....
        initManager();
    }

    private initManager(){
        ...
        long runTime = /* time of your first alarm/notification/whatever you develope */

        Intent intent = new Intent(this, NotifyService.class);
        pendingIntent=PendingIntent.getService(this, 0, intent, 0);
        manager.set(AlarmManager.RTC_WAKEUP, runTime, pendingIntent);
    }
}

And the service: 和服务:

public NotifyService extends Service{
    ...
    public int onStartCommand(...){
         ...
        /* do stuff */

        Intent intent = new Intent(getApplicationContext(), NotifyService.class);
        pendingIntent=PendingIntent.getService(this, 0, intent, 0);
        long runTime = /* recalculate it according to the next time of firing this service*/
        manager.set(AlarmManager.RTC_WAKEUP, runTime, pendingIntent);

    }

} }

So your service will register an intent in AlarmManager itself everytime the service fires. 因此,每次服务触发时,您的服务都会在AlarmManager本身中注册一个意图。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM