简体   繁体   English

AlarmManager未能按预期运行

[英]AlarmManager don't run as expectly

As you suggested me to use AlarmManager instead of Timer, I thought, the program will run. 正如您建议我使用AlarmManager代替Timer一样,我认为程序将运行。
But unfortunately, it does not. 但不幸的是,事实并非如此。 Or, better, not always... 或者,更好的是,并非总是...

This is my 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);

I'm expecting that the pending intent (AlarmReceiver) will be called every X milliseconds, but it does not. 我期望等待的意图(AlarmReceiver)将每X毫秒被调用一次,但是不会。
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. 我在电话的日志中看到不会调用它,在服务器的日志中(接收者发送HTTP请求),没有收到请求。

VERY strange is, that sometime it runs, but I can't reproduce the situation. 非常奇怪的是,它有时会运行,但是我无法重现这种情况。

Can someone say me what am I doing wrong? 有人可以说我在做什么错吗?

Thanks a lot 非常感谢
Luca Bertoncello 卢卡·贝通切洛(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);

Use setRepeating if you want your alarm to be Repeated daily. 如果希望每天重复一次警报,请使用setRepeating。
If you don`t want use setAlarm(..,..,..,..) instead 如果您不想使用setAlarm(..,..,..,..)

check out this- 看看这个-

Intent intentalarm = new Intent("com.mubble.powercutsensorapp.MYTIMER");
    PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intentalarm, 0);
    AlarmManager manager = (AlarmManager) getSystemService(ALARM_SERVICE);
    long now = System.currentTimeMillis();
    long interval = 24*60 * 60 * 1000; // 24 hour
    manager.setRepeating(AlarmManager.RTC_WAKEUP, now + interval, interval,
        pendingIntent); 

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

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