AlarmManager不要作为expectly [英] AlarmManager don't run as expectly

查看:517
本文介绍了AlarmManager不要作为expectly的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

正如你建议我使用AlarmManager代替定时器,我想,程序会运行。结果
但不幸的是,事实并非如此。或者,更好,总是不...

As you suggested me to use AlarmManager instead of Timer, I thought, the program will run.
But unfortunately, it does not. Or, better, not always...

这是我的code:

long millis = 0;

this.alarmMgr = (AlarmManager)this.main.getSystemService(Context.ALARM_SERVICE);

this.checkPendingIntent = PendingIntent.getBroadcast(this.main, 0,
              new Intent(this.main, AlarmReceiver.class), 0);

if(frequency.compareTo("1HOUR") == 0)
  millis = 3600 * 1000;
if(frequency.compareTo("12HOUR") == 0)
  millis = 12 * 3600 * 1000;
if(frequency.compareTo("1DAY") == 0)
  millis = 24 * 3600 * 1000;
if(frequency.compareTo("1WEEK") == 0)
  millis = 7 * 24 * 3600 * 1000;

this.alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP,
        System.currentTimeMillis(), millis, this.checkPendingIntent);

我期待那悬而未决的意图(AlarmReceiver)将会被调用每X毫秒,但事实并非如此。结果
我可以在我的手机日志中看到,它不会被调用,并在日志我的服务器中(接收器发送一个HTTP请求),没有收到请求。

I'm expecting that the pending intent (AlarmReceiver) will be called every X milliseconds, but it does not.
I can see in the logs of my phone, that it will not be called, and in the log of my server (the receiver sends an HTTP-Request), that no requests are received.

很奇怪的是,它运行一段时间,但我不能重现的情况。

VERY strange is, that sometime it runs, but I can't reproduce the situation.

有人会说我什么我做错了?

Can someone say me what am I doing wrong?

多谢结果
卢卡Bertoncello

Thanks a lot
Luca Bertoncello

推荐答案

这是如何使用的闹钟一个简单的例子

This is a simple example of how to use the Alarm

Calendar now = Calendar.getInstance();
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, yourHour);
calendar.set(Calendar.MINUTE, yourMin);
calendar.set(Calendar.SECOND, youSec);

if (calendar.before(now)) { //if time passed
    calendar.add(Calendar.DATE,1);
}
Intent intent = new Intent(Context.this, DestinationActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity
               (Settings.this,123456, intent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager am = (AlarmManager)getSystemService(Activity.ALARM_SERVICE);
am.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
                                        AlarmManager.INTERVAL_DAY,pendingIntent);

使用setRepeating如果你想每天重复闹钟。结果
如果我想知道我用setAlarm(.. .. .. ..),而不是

Use setRepeating if you want your alarm to be Repeated daily.
If you don`t want use setAlarm(..,..,..,..) instead

这篇关于AlarmManager不要作为expectly的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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