Android中的iOS NSNotificationCenter等效?

use*_*291 15 iphone android ios

Android中是否有类似的iOS类NSNotificationCenter?我可以使用任何库或有用的代码吗?

Beh*_*çar 20

在Android中,没有像ios那样的中央通知中心.但是你基本上可以使用ObservableObserver对象来完成你的任务.

你可以在下面定义一个类,只需修改它以便单独使用,并为同时使用添加synchronized,但想法是一样的:

public class ObservingService {
    HashMap<String, Observable> observables;

    public ObservingService() {
        observables = new HashMap<String, Observable>();
    }

    public void addObserver(String notification, Observer observer) {
        Observable observable = observables.get(notification);
        if (observable==null) {
            observable = new Observable();
            observables.put(notification, observable);
        }
        observable.addObserver(observer);
    }

    public void removeObserver(String notification, Observer observer) {
        Observable observable = observables.get(notification);
        if (observable!=null) {         
            observable.deleteObserver(observer);
        }
    }       

    public void postNotification(String notification, Object object) {
        Observable observable = observables.get(notification);
        if (observable!=null) {
            observable.setChanged();
            observable.notifyObservers(object);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)


Mil*_*gan 9

从Square看看奥托活动巴士:

http://square.github.com/otto/

它具有与NSNotificationCenter基本相同的功能,但由于注释和静态类型,更容易遵循事件所遵循的组件和路径的依赖关系.它比使用股票Android广播API,IMO简单得多.


eir*_*ran 7

我有同样的想法..所以我写了这个:

public class NotificationCenter {

    //static reference for singleton
    private static NotificationCenter _instance;

    private HashMap<String, ArrayList<Runnable>> registredObjects;

    //default c'tor for singleton
    private NotificationCenter(){
        registredObjects = new HashMap<String, ArrayList<Runnable>>();
    }

    //returning the reference
    public static synchronized NotificationCenter defaultCenter(){
        if(_instance == null)
            _instance = new NotificationCenter();
        return _instance;
    }

    public synchronized void addFucntionForNotification(String notificationName, Runnable r){
        ArrayList<Runnable> list = registredObjects.get(notificationName);
        if(list == null) {
            list = new ArrayList<Runnable>();
            registredObjects.put(notificationName, list);
        }
        list.add(r);
    }

    public synchronized void removeFucntionForNotification(String notificationName, Runnable r){
        ArrayList<Runnable> list = registredObjects.get(notificationName);
        if(list != null) {
            list.remove(r);
        }
    }

    public synchronized void postNotification(String notificationName){
        ArrayList<Runnable> list = registredObjects.get(notificationName);
        if(list != null) {
            for(Runnable r: list)
                r.run();
        }
    }

}
Run Code Online (Sandbox Code Playgroud)

这个用法将是:

  NotificationCenter.defaultCenter().addFucntionForNotification("buttonClick", new Runnable() {
        @Override
        public void run() {
            Toast.makeText(MainActivity.this, "Hello There", Toast.LENGTH_LONG).show();
        }
    });
Run Code Online (Sandbox Code Playgroud)

试图使界面尽可能类似于IOS,但更简单(不需要对象注册).

希望有帮助:)


归档时间:

查看次数:

8329 次

最近记录:

10 年,6 月 前