Android 相当于 NSNotificationCenter

标签 android iphone ios events nsnotificationcenter

在将 iPhone 应用程序移植到 android 的过程中,我正在寻找在应用程序内进行通信的最佳方式。 Intent 似乎是要走的路,这是最好的(唯一)选择吗? NSUserDefaults 在性能和编码方面似乎都比 Intent 轻得多。

我还应该添加我有一个状态的应用程序子类,但我需要让另一个 Activity 知道一个事件。

最佳答案

我找到的最佳等价物是 LocalBroadcastManager这是 Android Support Package 的一部分.

来自 LocalBroadcastManager 文档:

Helper to register for and send broadcasts of Intents to local objects within your process. This is has a number of advantages over sending global broadcasts with sendBroadcast(Intent):

  • You know that the data you are broadcasting won't leave your app, so don't need to worry about leaking private data.
  • It is not possible for other applications to send these broadcasts to your app, so you don't need to worry about having security holes they can exploit.
  • It is more efficient than sending a global broadcast through the system.

使用它时,您可以说 Intent相当于 NSNotification .这是一个例子:

ReceiverActivity.java

监视名为 "custom-event-name" 的事件通知的 Activity .

@Override
public void onCreate(Bundle savedInstanceState) {

  ...
  
  // Register to receive messages.
  // This is just like [[NSNotificationCenter defaultCenter] addObserver:...]
  // We are registering an observer (mMessageReceiver) to receive Intents
  // with actions named "custom-event-name".
  LocalBroadcastManager.getInstance(this).registerReceiver(mMessageReceiver,
      new IntentFilter("custom-event-name"));
}

// Our handler for received Intents. This will be called whenever an Intent
// with an action named "custom-event-name" is broadcasted.
private BroadcastReceiver mMessageReceiver = new BroadcastReceiver() {
  @Override
  public void onReceive(Context context, Intent intent) {
    // Get extra data included in the Intent
    String message = intent.getStringExtra("message");
    Log.d("receiver", "Got message: " + message);
  }
};

@Override
protected void onDestroy() {
  // Unregister since the activity is about to be closed.
  // This is somewhat like [[NSNotificationCenter defaultCenter] removeObserver:name:object:] 
  LocalBroadcastManager.getInstance(this).unregisterReceiver(mMessageReceiver);
  super.onDestroy();
}

SenderActivity.java

发送/广播通知的第二个 Activity 。

@Override
public void onCreate(Bundle savedInstanceState) {
  
  ...
  
  // Every time a button is clicked, we want to broadcast a notification.
  findViewById(R.id.button_send).setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
      sendMessage();
    }
  });
}

// Send an Intent with an action named "custom-event-name". The Intent sent should 
// be received by the ReceiverActivity.
private void sendMessage() {
  Log.d("sender", "Broadcasting message");
  Intent intent = new Intent("custom-event-name");
  // You can also include some extra data.
  intent.putExtra("message", "This is my message!");
  LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
}

使用上面的代码,每次按钮 R.id.button_send被点击,一个 Intent 被广播并被 mMessageReceiver 接收在 ReceiverActivity .

调试输出应如下所示:

01-16 10:35:42.413: D/sender(356): Broadcasting message
01-16 10:35:42.421: D/receiver(356): Got message: This is my message! 

关于Android 相当于 NSNotificationCenter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3947641/

相关文章:

java - 诊断 "RuntimeException: Unable to start activity Caused by: java.lang.NullPointerException"

android - 为什么我们在 Android Studio 中生成矢量图像时需要指定大小?

iphone - identifierForVendor 与 SecureUDID/OpenID

ios - 如何使用Swift通过PHAsset访问UIImage的元数据

ios - 以编程方式创建的 UIImageView

Android Studio 找不到我的资源

android - 在谷歌上创建更新服务

iphone - 数字布局在安全的 UITextField 中不粘?

iPhone 开发 - 编译器警告!

iOS:支持本地文件提供程序中的应用程序组文件夹