0

我有类似以下内容的内容,用于IntentService通过重复调用 aBroadcastReceiver来轮询服务器更新:

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);

在轮询服务器和使用 aResultReceiver获取服务器更新方面,上述方法效果很好。但是,我需要向轮询服务提供一些反馈,以便更改我的更新查询。

我应该如何向投票服务提供反馈?如果需要更新查询,我是否只需要取消当前警报并再次设置意图?有没有比取消更好的方法?

4

1 回答 1

0

我认为您的问题...我认为您想在上面的警报管理器设置之后更改项目中某处的“updatingThing”值,如果这是您的问题,请在上面的代码中检查此解决方案以设置警报只需替换此行

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);
于 2014-12-04T09:29:18.687 回答