[英]Which object use in Android instead of NotificationCenter in IOS?
IOS use NotificationCenter likes below: iOS使用NotificationCenter的方式如下:
let failureObserver = NSNotificationCenter.defaultCenter().addObserverForName(downloadEndFailureNotificationName, object: nil, queue: nil) { (notification) in
//Process failed result
self._processFailureResultData(forID: connectionID)
}
So, what is using instead of NotificationCenter in Android? 那么,在Android中使用什么来代替NotificationCenter?
You use Notification.Builder to build Notification instance. 您使用Notification.Builder来构建Notification实例。 If you are looking for Observer similar to ios, look into Broadcast Receiver
如果您正在寻找类似于ios的Observer,请查看Broadcast Receiver
LocalBroadcastManager
is a very heavyweight solution which reflects a misunderstanding of what NSNotificationCenter
does. LocalBroadcastManager
是一个非常重量级的解决方案,它反映了对NSNotificationCenter
所做的误解。
The best tool for the job is very likely something like Guava's EventBus . 工作的最佳工具很可能是Guava的EventBus之类的东西 。 Here's a simple way to use it.
这是使用它的一种简单方法。
[NSNotificationCenter defaultCenter]
equivalent. [NSNotificationCenter defaultCenter]
等效项。 In the Guava library and the description below, Apple's notifications become events , and an observer becomes a consumer . EventBus eventBus = new EventBus();
public class MyCustomEvent {
public MyCustomEvent() {}
}
MyCustomEvent
. MyCustomEvent
上注册事件通知。 You need to use the event bus instance we created in step 1. The process of registration involves registering an instance of a class with the event bus. @Subscribe
annotation. @Subscribe
批注的方法。 Such a method must take as the only argument the event class you defined above. public class MyConsumerClass {
public MyConsumerClass(EventBus eventBus) {
eventBus.register(new Object() {
@Subscribe
public void receivedCustomEvent(MyCustomEvent event) {
// Do something with the received notification/event.
}
});
}
...
}
eventBus.post(new MyCustomEvent());
For more information read up on the Guava library. 有关更多信息,请阅读Guava库。
Use LocalBroadcastManager 使用LocalBroadcastManager
Like NotificationCenter you can register and unregister object as observers. 像NotificationCenter一样,您可以注册和注销对象为观察者。 You can send broadcast messages to simulate iOS notifications behaviour.
您可以发送广播消息来模拟iOS通知行为。
For the usage refer to this StackOverflow question 有关用法,请参阅此 StackOverflow问题
Create a Notification.Builder : 创建一个Notification.Builder :
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.notification_icon)
.setContentTitle("My notification")
.setContentText("Hello World!");
Then issue the notification: 然后发出通知:
// Sets an ID for the notification
int mNotificationId = 001;
// Gets an instance of the NotificationManager service
NotificationManager mNotifyMgr =
(NotificationManager) getSystemService(NOTIFICATION_SERVICE);
// Builds the notification and issues it.
mNotifyMgr.notify(mNotificationId, mBuilder.build());
Add permissions into AndroidManifest.xml 向AndroidManifest.xml添加权限
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE"/>
<uses-permission android:name="${applicationId}.permission.C2D_MESSAGE"/>
public class GCMBroadcastReceiver extends BroadcastReceiver { 公共类GCMBroadcastReceiver扩展了BroadcastReceiver {
private static final String TAG = GCMBroadcastReceiver.class.getSimpleName();
@Override
public void onReceive(Context context, Intent intent) {
if ("com.google.android.c2dm.intent.REGISTRATION".equals(intent.getAction())) {
// Register Localytics (this will call Localytics.handleRegistration(intent))
new PushReceiver().onReceive(context, intent);
} else if ("com.google.android.c2dm.intent.RECEIVE".equals(intent.getAction())) {
// DO SOMETHING
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.