通过重复调用IntentService
来轮询服务器更新,我有以下内容来启动BroadcastReceiver
:
AlarmManager pollManager;
Intent pollIntent;
PendingIntent pollPendingIntent;
...
pollIntent = new Intent(getActivity(), ActionUpdateReceiver.class);
pollIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
pollIntent.putExtra(RECEIVER, resultReceiver);
pollIntent.putExtra(USER, accountId);
// This is the crux of my question
pollIntent.putExtra(SOMETHING_THAT_UPDATES, updatingThing);
pollPendingIntent = PendingIntent.getBroadcast(getActivity(), ACTION_REQUEST,
pollIntent, PendingIntent.FLAG_CANCEL_CURRENT);
pollManager.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(),
POLL_INTERVAL, pollPendingIntent);
在轮询服务器和使用ResultReceiver
获取服务器更新方面,上述方法效果很好。但是,我需要向轮询服务提供一些反馈,以便更改我的更新查询。
我应该如何向投票服务提供反馈?如果要求更新查询,我是否只需要取消当前警报并再次设置意图?有没有比取消更好的方法?
答案 0 :(得分:0)
我认为你的问题......我想你想在上面的警报管理器设置后更改项目中某处“更新”的值 如果这是您的问题,请检查此解决方案 对于上面代码中的设置警报,只需替换此行
pollIntent = new Intent(getActivity(), ActionUpdateReceiver.class);
由此
pollIntent = new Intent(getActivity().getApplicationContext(), ActionUpdateReceiver.class);
和PendingIntent也是
pollPendingIntent = PendingIntent.getBroadcast(getActivity().getApplicationContext(), ACTION_REQUEST,
pollIntent, PendingIntent.FLAG_CANCEL_CURRENT);
并将此代码放在您想要反馈更新的位置
AlarmManager pollManager;
Intent pollIntent;
PendingIntent pollPendingIntent;
...
pollIntent = new Intent(getActivity().getApplicationContext(), ActionUpdateReceiver.class);
pollIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
pollIntent.putExtra(RECEIVER, resultReceiver);
pollIntent.putExtra(USER, accountId);
pollIntent.putExtra(SOMETHING_THAT_UPDATES, updatingThing); // your update here
pollPendingIntent = PendingIntent.getBroadcast(getActivity().getApplicationContext(), ACTION_REQUEST,
pollIntent, PendingIntent.FLAG_CANCEL_CURRENT);
pollManager.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(),
POLL_INTERVAL, pollPendingIntent);