【问题标题】:Send Update to Repeating BroadcastReceiver向重复广播接收器发送更新
【发布时间】:2014-11-19 07:06:13
【问题描述】:

我通过重复调用BroadcastReceiver 来启动IntentService 以轮询服务器更新,有如下类似的方法:

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 获取服务器更新方面,上述方法效果很好。但是,我需要向轮询服务提供一些反馈,以便更改我的更新查询。

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

【问题讨论】:

  • 您的问题不清楚.. 什么是“提供反馈”?什么是轮询服务器?
  • 究竟有什么不清楚的地方?轮询服务是由指定的BroadcastReceiver 调用的IntentService。反馈就是反馈。它是更新轮询服务使用的查询的反馈,它取决于重复轮询服务之外的执行。

标签: android broadcastreceiver repeatingalarm android-intentservice


【解决方案1】:

我认为您的问题...我认为您想在上面的警报管理器设置之后更改项目中某处“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);

【讨论】:

  • 我不认为 getApplicationContext() 是必要的; getActivity() 作为上下文工作得很好。此外,这对我来说似乎与之前所说的取消 Intents 没有什么不同,这就是我所做的。无论如何,这种方法似乎有效,所以我想这毕竟是答案。
猜你喜欢
  • 1970-01-01
  • 2019-07-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多