Equivalent of iOS NSNotificationCenter in Android?

前端 未结 5 796
无人共我
无人共我 2021-01-31 05:51

Is there an equivalent of the iOS class NSNotificationCenter in Android ? Are there any libraries or useful code available to me ?

5条回答
  •  别跟我提以往
    2021-01-31 06:09

    On the basis of Behlül answer, I change the code to make it closer to iOS NSNotificationCenter.

    Another thing: the notifications will fire on the main thread

    package com.oxygen.utils;
    
    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.List;
    
    import android.os.Handler;
    
    public class NotificationCenter {
    
    //---------------- event type list ---------------------
    
        public static enum NotificationID{
            IMAGES_CACHE_READY
        } 
    
    
    //---------------- singelton ---------------------------    
    
            private static NotificationCenter instance = null;
    
            private NotificationCenter() { observables = new HashMap();   }
    
            public static synchronized NotificationCenter singelton() {
                if (instance == null) {
                            instance = new NotificationCenter ();
                }
                return instance;
            }
    
    //-------------------------------------------
    
                    public class Notification {
    
                        private Object poster;  // the object that post the event
                        private Object info;    // event specific data 
                        private NotificationID id;      // event name
    
                        public Notification(Object poster, NotificationID id, Object info) {
                            super();
                            this.poster = poster;
                            this.info = info;
                            this.id = id;
                        }
    
                        public Object getPoster() {
                            return poster;
                        }
    
                        public Object getInfo() {
                            return info;
                        }
    
                        public NotificationID getId() {
                            return id;
                        }
                    }
            //-------------------------------------------
    
                    public interface Notifiable {
                        public void onNotification(Notification notify);
                    }
    
    //-------------------------------------------
    
            protected class MyObservable {
                List observers = new ArrayList();
    
                public MyObservable() {
                }
    
                public void addObserver(Notifiable observer) {
                    if (observer == null) {
                        throw new NullPointerException("observer == null");
                    }
                    synchronized (this) {
                        if (!observers.contains(observer))
                            observers.add(observer);
                    }
                }
    
                public int countObservers() {
                    return observers.size();
                }
    
                public synchronized void deleteObserver(Notifiable observer) {
                    observers.remove(observer);
                }
    
                public synchronized void deleteObservers() {
                    observers.clear();
                }
    
                public void notifyObservers(Notification notify) {
                    int size = 0;
                    Notifiable[] arrays = null;
                    synchronized (this) {
                            size = observers.size();
                            arrays = new Notifiable[size];
                            observers.toArray(arrays);
                    }
                    if (arrays != null) {
                        for (Notifiable observer : arrays) {
                            observer.onNotification(notify);
                        }
                    }
                }
            }
    
    //-------------------------------------------
    
            HashMap observables;
    
            public void addObserver(NotificationID id, Notifiable observer) {
                MyObservable observable = observables.get(id);
                if (observable==null) {
                    observable = new MyObservable ();
                    observables.put(id, observable);
                }
                observable.addObserver(observer);
            }
    
            public void removeObserver(NotificationID id, Notifiable observer) {
                MyObservable observable = observables.get(id);
                if (observable!=null) {         
                    observable.deleteObserver(observer);
                }
            }
    
            public void removeObserver(Notifiable observer) {
                 for (MyObservable observable : observables.values()) {
                     if (observable!=null) {         
                        observable.deleteObserver(observer);
                    }    
                }
            }
    
            public void postNotification(final Object notificationPoster, final NotificationID id, final Object notificationInfo) {
    
                final MyObservable  observable = observables.get(id);
                if (observable!=null) {
    
                    // notification post to the maim (UI) thread    
                    // Get a handler that can be used to post to the main thread
                    Handler mainHandler = new Handler(AppContext.get().getMainLooper());
    
                    Runnable myRunnable = new Runnable() {
    
                        @Override
                        public void run() {
                            observable.notifyObservers(new Notification(notificationPoster, id, notificationInfo) );
                        }
                    };
    
                    mainHandler.post(myRunnable);
                }
            }
    }
    

    Listener sample:

    public class CustomGridViewAdapter extends ArrayAdapter  implements Notifiable {
        int layoutResourceId;
    
        public CustomGridViewAdapter(Context context, int layoutResourceId) {
            super(context, layoutResourceId);
            this.layoutResourceId = layoutResourceId;
    
            loadCategories(false);
            NotificationCenter.singelton().addObserver(NotificationID.IMAGES_CACHE_READY, this);
        }
    
        public void onDestroy() {
            NotificationCenter.singelton().removeObserver(this);            
        }
    
        @Override
        public void onNotification(Notification notify) {
            switch (notify.getId()) {
            case IMAGES_CACHE_READY:
                loadCategories(true);
                break;
            }
        }
    ...
    }
    

提交回复
热议问题