为什么我的Andr​​oid报警管理器发射瞬间?我的、管理器、瞬间、oid

由网友(风卷残衣)分享简介:我在下面的示例code发送的更新通知每个10'seconds。在code以下,这是在一个 UpdateService 为 AppWidgetProvider 。如果我把一个视频下载(10 * 1000); 我可以看到我的维修环路的预期行为。我显然有某种根本性错误被立即触发。它应该是一个 PendingIntent 中,...

我在下面的示例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.

阅读全文

相关推荐

最新文章