为什么我的Andr​​oid报警管理器发射瞬间? [英] Why is my android alarm manager firing instantly?

查看:101
本文介绍了为什么我的Andr​​oid报警管理器发射瞬间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在下面的示例code发送的更新通知每个10'seconds。在code以下,这是在一个 UpdateService AppWidgetProvider 。如果我把一个视频下载(10 * 1000); 我可以看到我的维修环路的预期行为。我显然有某种根本性错误被立即触发。它应该是一个 PendingIntent 中,将广播更新我的听众报警。

 长下次更新= 10 * 1000;
Log.d(TAG,请求在下次更新+下次更新+毫秒);

意图updateIntent =新的意图(ACTION_UPDATE_ALL);
updateIntent.setClass(这一点,UpdateService.class);

PendingIntent pendingIntent = PendingIntent.getService(此,0,updateIntent,0);

//日程报警,并强制设备清醒此更新
AlarmManager alarmManager =(AlarmManager)getBaseContext()getSystemService(Context.ALARM_SERVICE)。
alarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME,SystemClock.elapsedRealtime(),
    下次更新,pendingIntent);
 

解决方案

AlarmManager.setRepeating被定义为公共无效setRepeating(int型的,长triggerAtTime,间隔长,PendingIntent操作)的第二个参数是当它应该首先调用。你告诉它开始于 SystemClock.elapsedRealtime(),也就是现在的。

I am following sample code for sending an update notification every 10'seconds. The code follows and it is in an UpdateService for an AppWidgetProvider. If I put a Thread.sleep(10*1000); I can see the expected behavior of my servicing loop. I obviously have something fundamentally wrong that is triggering immediately. It is supposed to be a PendingIntent of an alarm that will broadcast update to my listener.

long nextUpdate = 10*1000;
Log.d(TAG, "Requesting next update in " + nextUpdate + " msec." );

Intent updateIntent = new Intent(ACTION_UPDATE_ALL);
updateIntent.setClass(this, UpdateService.class);

PendingIntent pendingIntent = PendingIntent.getService(this, 0, updateIntent, 0);

// Schedule alarm, and force the device awake for this update
AlarmManager alarmManager = (AlarmManager)getBaseContext().getSystemService(Context.ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME, SystemClock.elapsedRealtime(), 
    nextUpdate, pendingIntent);

解决方案

AlarmManager.setRepeating is defined as public void setRepeating (int type, long triggerAtTime, long interval, PendingIntent operation) The 2nd argument is when it should be first called. You're telling it to start at SystemClock.elapsedRealtime(), which is now.

这篇关于为什么我的Andr​​oid报警管理器发射瞬间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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