哪个对象在Android中使用而不是在IOS中使用NotificationCenter?

时间:2016-06-01 07:18:38

标签: android ios nsnotificationcenter

IOS使用NotificationCenter,如下所示:

    let failureObserver =    NSNotificationCenter.defaultCenter().addObserverForName(downloadEndFailureNotificationName, object: nil, queue: nil) { (notification) in

        //Process failed result
        self._processFailureResultData(forID: connectionID)

    }

那么,在Android中使用什么代替NotificationCenter?

4 个答案:

答案 0 :(得分:1)

您使用Notification.Builder构建Notification实例。如果您正在寻找类似于ios的观察者,请查看广播接收器

答案 1 :(得分:1)

LocalBroadcastManager是一个非常重要的解决方案,它反映了对NSNotificationCenter所做的事情的误解。

这项工作的最佳工具非常可能像Guava's EventBus。这是一种使用它的简单方法。

  1. 创建EventBus实例。这是[NSNotificationCenter defaultCenter]等价物。在Guava库和下面的描述中,Apple的通知成为事件观察者成为消费者。发布事件的对象称为生成器
  2. EventBus eventBus = new EventBus();
    
    1. 创建事件类型。这是您自己设计的Java类。您可以拥有从生产者传递给消费者所需的任何信息。
    2. public class MyCustomEvent {
        public MyCustomEvent() {}
      }
      
      1. 在您的消费者类中,注册MyCustomEvent上的事件通知。您需要使用我们在步骤1中创建的事件总线实例。注册过程涉及使用事件总线注册类的实例。此类需要使用@Subscribe注释注释一个或多个方法。这样的方法必须作为您在上面定义的事件类的唯一参数。该方法的名称无关紧要。
      2. public class MyConsumerClass {
          public MyConsumerClass(EventBus eventBus) {
            eventBus.register(new Object() {
              @Subscribe
              public void receivedCustomEvent(MyCustomEvent event) {
                // Do something with the received notification/event.
              }
            });
          }
          ...
        }
        
        1. 在制作人方面,您现在可以发布已注册消费者收到的通知(事件)。确保使用步骤1中定义的相同事件总线实例,该实例已由您的消费者注册。
        2. eventBus.post(new MyCustomEvent());
          

          欲了解更多信息,请阅读Guava图书馆。

答案 2 :(得分:0)

使用LocalBroadcastManager

与NotificationCenter一样,您可以注册和取消注册对象作为观察者。您可以发送广播消息来模拟iOS通知行为。

有关用法,请参阅this StackOverflow问题

答案 3 :(得分:-2)

  1. 创建通知
  2. 创建 Notification.Builder

    NotificationCompat.Builder mBuilder =
        new NotificationCompat.Builder(this)
        .setSmallIcon(R.drawable.notification_icon)
        .setContentTitle("My notification")
        .setContentText("Hello World!");
    

    然后发出通知:

    // 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());
    
    1. 收听通知(使用GCM权限)
    2. 将权限添加到 AndroidManifest.xml

      <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE"/>
      <uses-permission android:name="${applicationId}.permission.C2D_MESSAGE"/>
      

      公共类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
          }
        }
      }