Android 等同于 NSNotificationCenter

在将 iPhone 应用程序移植到 android 的过程中,我正在寻找在应用程序中进行通信的最佳方式。意图似乎是走的方式,这是最好的(唯一的)选择吗?在性能和编码方面,NSUserDefault 看起来比意图轻量级得多。

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

27637 次浏览

You could use this: http://developer.android.com/reference/android/content/BroadcastReceiver.html, which gives a similar behavior.

You can register receivers programmatically through Context.registerReceiver(BroadcastReceiver, IntentFilter) and it will capture intents sent through Context.sendBroadcast(Intent).

Note, though, that a receiver will not get notifications if its activity (context) has been paused.

The best equivalent I found is LocalBroadcastManager which is part of the Android Support Package.

From the LocalBroadcastManager documentation:

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.

When using this, you can say that an Intent is an equivalent to an NSNotification. Here is an example:

ReceiverActivity.java

An activity that watches for notifications for the event named "custom-event-name".

@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

The second activity that sends/broadcasts notifications.

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

With the code above, every time the button R.id.button_send is clicked, an Intent is broadcasted and is received by mMessageReceiver in ReceiverActivity.

The debug output should look like this:

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!

I found that the usage of EventBus of Guava lib is the simplest way for publish-subscribe-style communication between components without requiring the components to explicitly register with one another

see their sample on https://code.google.com/p/guava-libraries/wiki/EventBusExplained

// Class is typically registered by the container.
class EventBusChangeRecorder {
@Subscribe public void recordCustomerChange(ChangeEvent e) {
recordChange(e.getChange());
}


// somewhere during initialization
eventBus.register(this);


}


// much later
public void changeCustomer() {
eventBus.post(new ChangeEvent("bla bla") );
}

you can add this lib simply on Android Studio by adding a dependency to your build.gradle:

compile 'com.google.guava:guava:17.0'

You could use weak references.

This way you could manage the memory yourself and add and remove observers as you please.

When you addObserver add these parameters - cast that context from the activity you are adding it in to the empty interface, add a notification name, and call the method to run interface.

The method to run interface would have a function that is called run to return the data that you are passing something like this

public static interface Themethodtorun {
void run(String notification_name, Object additional_data);
}

Create a observation class that invokes a reference with a empty interface. Also construct your Themethodtorun interface from the context being passed in the addobserver.

Add the observation to a data structure.

To call it would be the same method however all you need to do is find the specific notification name in the data structure, use the Themethodtorun.run(notification_name, data).

This will send a callback to where ever you created an observer with a specific notification name. Dont forget to remove them when your done!

This is good reference for weak references.

http://learningviacode.blogspot.co.nz/2014/02/weak-references-in-java.html

I am in the process of uploading this code to github. Keep eyes open!

Here is something similar to @Shiki answer, but from the angle of iOS developers and Notification center.

First create some kind of NotificationCenter service:

public class NotificationCenter {


public static void addObserver(Context context, NotificationType notification, BroadcastReceiver responseHandler) {
LocalBroadcastManager.getInstance(context).registerReceiver(responseHandler, new IntentFilter(notification.name()));
}


public static void removeObserver(Context context, BroadcastReceiver responseHandler) {
LocalBroadcastManager.getInstance(context).unregisterReceiver(responseHandler);
}


public static void postNotification(Context context, NotificationType notification, HashMap<String, String> params) {
Intent intent = new Intent(notification.name());
// insert parameters if needed
for(Map.Entry<String, String> entry : params.entrySet()) {
String key = entry.getKey();
String value = entry.getValue();
intent.putExtra(key, value);
}
LocalBroadcastManager.getInstance(context).sendBroadcast(intent);
}
}

Then, you will also need some enum type to be secure of mistakes in coding with strings - (NotificationType):

public enum NotificationType {


LoginResponse;
// Others


}

Here is usage(add/remove observers) for example in activities:

public class LoginActivity extends AppCompatActivity{


private BroadcastReceiver loginResponseReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
// do what you need to do with parameters that you sent with notification


//here is example how to get parameter "isSuccess" that is sent with notification
Boolean result = Boolean.valueOf(intent.getStringExtra("isSuccess"));
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);


//subscribe to notifications listener in onCreate of activity
NotificationCenter.addObserver(this, NotificationType.LoginResponse, loginResponseReceiver);
}


@Override
protected void onDestroy() {
// Don't forget to unsubscribe from notifications listener
NotificationCenter.removeObserver(this, loginResponseReceiver);
super.onDestroy();
}
}

and here is finally how we post notification to NotificationCenter from some callback or rest service or whatever:

public void loginService(final Context context, String username, String password) {
//do some async work, or rest call etc.
//...


//on response, when we want to trigger and send notification that our job is finished
HashMap<String,String> params = new HashMap<String, String>();
params.put("isSuccess", String.valueOf(false));
NotificationCenter.postNotification(context, NotificationType.LoginResponse, params);
}

that's it, cheers!

Kotlin: Here's a @Shiki's version in Kotlin with a little bit refactor in a fragment.

  1. Register the observer in Fragment.

Fragment.kt

class MyFragment : Fragment() {


private var mContext: Context? = null


private val mMessageReceiver = object: BroadcastReceiver() {
override fun onReceive(context: Context?, intent: Intent?) {
//Do something here after you get the notification
myViewModel.reloadData()
}
}


override fun onAttach(context: Context) {
super.onAttach(context)


mContext = context
}


override fun onStart() {
super.onStart()
registerSomeUpdate()
}


override fun onDestroy() {
LocalBroadcastManager.getInstance(mContext!!).unregisterReceiver(mMessageReceiver)
super.onDestroy()
}


private fun registerSomeUpdate() {
LocalBroadcastManager.getInstance(mContext!!).registerReceiver(mMessageReceiver, IntentFilter(Constant.NOTIFICATION_SOMETHING_HAPPEN))
}


}
  1. Post notification anywhere. Only you need the context.

    LocalBroadcastManager.getInstance(context).sendBroadcast(Intent(Constant.NOTIFICATION_SOMETHING_HAPPEN))```
    

PS:

  1. you can add a Constant.kt like me for well organize the notifications. Constant.kt
object Constant {
const val NOTIFICATION_SOMETHING_HAPPEN = "notification_something_happened_locally"
}
  1. For the context in a fragment, you can use activity (sometimes null) or conext like what I used.

I wrote a wrapper that can do this same job, equivalent to iOS using LiveData

Wrapper:

class ObserverNotify {
private val liveData = MutableLiveData<Nothing>()




fun postNotification() {
GlobalScope.launch {
withContext(Dispatchers.Main) {
liveData.value = liveData.value
}
}
}


fun observeForever(observer: () -> Unit) {
liveData.observeForever { observer() }
}


fun observe(owner: LifecycleOwner, observer: () -> Unit) {
liveData.observe(owner) { observer()}
}


}


class ObserverNotifyWithData<T> {
private val liveData = MutableLiveData<T>()




fun postNotification(data: T) {
GlobalScope.launch {
withContext(Dispatchers.Main) {
liveData.value = data
}
}
}


fun observeForever(observer: (T) -> Unit) {
liveData.observeForever { observer(it) }
}


fun observe(owner: LifecycleOwner, observer: (T) -> Unit) {
liveData.observe(owner) { observer(it) }
}


}

Declaring observer types:

object ObserverCenter {
val moveMusicToBeTheNextOne: ObserverNotifyWithData<Music> by lazy { ObserverNotifyWithData() }
val playNextMusic: ObserverNotify by lazy { ObserverNotify() }
val newFCMTokenDidHandle: ObserverNotifyWithData<String?> by lazy { ObserverNotifyWithData() }
}

In the activity to observe:

ObserverCenter.newFCMTokenDidHandle.observe(this) {
// Do stuff
}

To notify:

ObserverCenter.playNextMusic.postNotification()
ObserverCenter.newFCMTokenDidHandle.postNotification("MyData")

Answer of @Shiki could be right in June 2020, but in January 2022, LocalBroadcastManager happened to be deprecated.

After two days of research, I ended up finding that SharedFlow was indicated by Android to "send ticks to the rest of the app so that all the content refreshes periodically at the same time".

Meaning, more or less, what we could expect from the NSNotificationCenter of Swift.

And here is the way I implemented the Shared Flow in my app:

First, you need to create an InAppNotif Singleton, which is actually a shared ViewModel for your activity (be caution to this last point: shared for your activity, not your all app^^)

enum class InAppNotifName {
NotifNameNumber1,
NotifNameNumber2,
NotifNameNumber3
}


object InAppNotif: ViewModel() {
    

private val _sharedNotif = MutableSharedFlow<InAppNotifName>(0)
val sharedNotif: SharedFlow<InAppNotifName> = _sharedNotif.asSharedFlow()


private fun sendNotif(name: InAppNotifName) {
CoroutineScope(Default).launch {
_sharedNotif.emit(name)
}
}


public fun notifyNotif1() {
sendNotif(InAppNotifName.NotifNameNumber1)
}


public fun notifyNotif2() {
sendNotif(InAppNotifName.NotifNameNumber1)
}


public fun notifyNotif3() {
sendNotif(InAppNotifName.NotifNameNumber1)
}


}

Second Step, only required if you have many Fragments receiving in app notifications, and you don't want to repeat yourself, would be to create an "Receiving Notif" interface

fun AnyReceivingNotif.observeInAppNotif() {
CoroutineScope(Default).launch {
InAppNotif.sharedNotif.collect {
onReceivingInAppNotif(it)
}
}
}


interface AnyReceivingNotif {
suspend fun onReceivingInAppNotif(value: InAppNotifName)
}

By the way, the "suspend" word is useful only if you need to update the UI upon receiving the notification.

Finally, from any object which is to receive InAppNotif, all you would need to do is get it be conform to your AnyReceivingNotif interface, and then complete the onReceivingInAppNotif function

class MyFragment: Fragment(), AnyReceivingNotif {


override suspend fun onReceivingInAppNotif(value: InAppNotifName) {
when (value) {
InAppNotifName.NotifNameNumber1 -> { /* Do complicated things */ }
InAppNotifName.NotifNameNumber2 -> { /* Do some stuff */ }
InAppNotifName.NotifNameNumber3 -> {
withContext(Default){
/* Update the UI */
}
}
}
}


}