更新的活动从一个BroadcastReceiver [英] Updating an Activity from a BroadcastReceiver

查看:114
本文介绍了更新的活动从一个BroadcastReceiver的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<一个href="http://stackoverflow.com/questions/10216151/updating-google-maps-marker-from-a-broadcast-sms-receiver/10216546">This 的问题提出了一个有趣的问题。

This question brought up an interesting issue.

的OP具有一个应用程序,显示地图,并且此映射需要与经由SMS消息接收的位置标记进行更新。各个步骤相当简单:在SMS​​消息可以通过的BroadcastReceiver 标记可以通过 ItemizedOverlay 在图形页面的顶部。最棘手的部分是有一个与应用程序的主要部分的接收部分进行通信。

The OP has an app that displays a map, and this map needs to be updated with location markers that are received via SMS messages. The individual steps are fairly straightforward: the SMS messages can be received by a BroadcastReceiver, the markers can be displayed by an ItemizedOverlay on top of a MapView. The tricky part is to have the receiving part communicate with the main part of the app.

  • 如果该应用程序有会发生什么积极的 MapActivity ,然后将其的BroadcastReceiver 被援引作为响应传入​​的短信?是 MapActivity ,而的BroadcastReceiver code是在同一过程中执行的暂停?如果是这样,是不是安全的的BroadcastReceiver 访问 MapActivity 通过静态引用(这是由活动的设置的onCreate 的方法?

  • What happens if the app has an active MapActivity, and then its BroadcastReceiver is invoked as a response to an incoming SMS? Is the MapActivity suspended while the BroadcastReceiver code is executed in the same process? If so, is it safe for BroadcastReceiver to access the MapActivity via a static reference (which is set by the activity's onCreate method?

相反,是一个应用程序的的BroadcastReceiver 在一个单独的进程中执行,因此需要与应用程序的activty沟通一些其他的方式?

Conversely, is an app's BroadcastReceiver executed in a separate process, and would therefore need some other way to communicate with the app's activty?

推荐答案

阅读文档,它看起来像一个BroadcastReceiver是在不同的进程中执行,尽管我不是100%肯定(<一href="http://developer.android.com/reference/android/content/BroadcastReceiver.html#ReceiverLifecycle"相对=nofollow> BroadcastReceiver的生命周期)

Reading the docs, it looks like a BroadcastReceiver is executed on a different process, tho I'm not 100% sure (BroadcastReceiver lifecycle)

当前正在执行一个BroadcastReceiver(也就是,当前正在运行的code在它的onR​​eceive(上下文,意图)方法)被认为是一个过程,是一个前台进程

这说,我不会考虑安全从的onReceive访问活动,因为它是一个不同的过程,它可能会崩溃。

This said, I wouldn't consider safe to access the activity from the onReceive, as it's a different process and it will probably crash.

要考虑到的活动也可以作为一个BroadcastReceiver,寿你要控制时在其生命周期也积极地监听事件。这样一来,就可以在onResume订阅(code从ZXing项目中提取)

Take into account that the Activity can also act as a BroadcastReceiver, tho you have to control when in its lifecycle it actively listens for events. That way, you can subscribe in onResume (code extracted from ZXing project)

 public void onResume(){
    activity.registerReceiver(powerStatusReceiver, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
    [...]
  }

  public void onPause() {
    [...]
    activity.unregisterReceiver(powerStatusReceiver);
  }

和你定义的BroadcastReceiver作为公共类中的私有类

And you define the BroadcastReceiver as a private class inside the public class

final class InactivityTimer {

[onResume, onPause, rest of the stuff ...]

    private final class PowerStatusReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent){
          if (Intent.ACTION_BATTERY_CHANGED.equals(intent.getAction())) {
            // 0 indicates that we're on battery
            // In Android 2.0+, use BatteryManager.EXTRA_PLUGGED
            int batteryPlugged = intent.getIntExtra("plugged", -1);
            if (batteryPlugged > 0) {
              InactivityTimer.this.cancel();
            }
          }
        }
      }
}

所以,的BroadcastReceiver应始终坚持的新的标志物(通过服务,从未在的onReceive 的)和它应通知潜在活性MapActivity新标记已被添加,这将在如果听这是积极的。

So, the BroadcastReceiver should always persist the new markers (through a Service, never inside the onReceive) AND it should notify a potentially active MapActivity that new markers have been added, which will be listening if it's active.

或者,更简单,活动和的BroadcastReceiver监听同样的短信意图。而后者则坚持它,第一次更新的地图,尽管我只是猜测什么,我会尽力。

Or, even easier, the Activity and the BroadcastReceiver listen for the same SMS Intent. While the latter persists it, the first updates the map, tho I'm just guessing what I would try.

这篇关于更新的活动从一个BroadcastReceiver的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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