Android eventbus開源架構(模仿)

來源:互聯網
上載者:User

Android eventbus開源架構(模仿)

 

SubscriberMethodFinder 發現註冊方法類 SubscriberMethod 使用者註冊的方法組合 Subscription 使用者和方法索引值對應類 AsyncPoster 非同步發起類 HandlerPoster 主線程發起類 PostBeen 訊息類(發起類根據訊息執行回調) EventBus 訪問類(建築者模式)

**

注意!!!!!在onCreate 方法裡面開啟新線程一般預設為主線程,只有主線程閒置時候才可以發起。Looper.myQueue().addIdleHandler

**

MainActivity
package com.lvshujun.customeventbus;import com.lvshujun.event.EventBus;import android.app.Activity;import android.os.Bundle;import android.os.Looper;import android.os.MessageQueue.IdleHandler;public class MainActivity extends Activity {    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        EventBus.getDefault().register(this);        //EventBus.getDefault().unregister(this);        Looper.myQueue().addIdleHandler(new IdleHandler() {            @Override            public boolean queueIdle() {                // TODO Auto-generated method stub                EventBus.getDefault().post(new ResponseEvent());                return false;            }        });    }    public void onEventMainThread(ResponseEvent event)    {        System.out.println(Thread.currentThread().getName());    }}
SubscriberMethodFinder
package com.lvshujun.event;import java.lang.reflect.Method;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;public class SubscriberMethodFinder {    private static final Map> methodCache = new HashMap>();    List findSubscriberMethods(Class subscriberClass) {        String cName = subscriberClass.getName();        List subscriberMethods = null;        synchronized (methodCache) {             subscriberMethods = methodCache.get(cName);            }        if(subscriberMethods != null)            return subscriberMethods;        subscriberMethods = new ArrayList();        if(subscriberClass != null)        {            Method[] methods = subscriberClass.getDeclaredMethods();            for(Method method:methods)            {                String methodName = method.getName();                if(methodName.startsWith(onEvent))                {                    Class[] parameterTypes = method.getParameterTypes();                    if(parameterTypes.length == 1)                    {                        SubscriberMethod sm = new SubscriberMethod(method, parameterTypes[0]);                        subscriberMethods.add(sm);                    }                }            }        }        methodCache.put(cName, subscriberMethods);        return subscriberMethods;    }}
Subscription
package com.lvshujun.event;public final class Subscription {    public final Object subscriber;    public final SubscriberMethod subscriberMethod;    Subscription(Object subscriber, SubscriberMethod subscriberMethod) {        this.subscriber = subscriber;        this.subscriberMethod = subscriberMethod;    }}
SubscriberMethod
package com.lvshujun.event;import java.lang.reflect.Method;public final class SubscriberMethod {    final public Method method;    final public Class eventType;    SubscriberMethod(Method method, Class eventType) {        this.method = method;        this.eventType = eventType;    }}
HandlerPoster
package com.lvshujun.event;import java.util.LinkedList;import java.util.Queue;import android.os.Handler;import android.os.Looper;import android.os.Message;final class HandlerPoster extends Handler {    private Queue queue;    private final EventBus eventBus;    HandlerPoster(EventBus eventBus, Looper looper) {        super(looper);        this.eventBus = eventBus;        queue = new LinkedList();    }    @Override    public void handleMessage(Message msg) {        try {            PostBeen pendingPost = queue.poll();            if (pendingPost == null) {            } else {                eventBus.invokeSubscriber(pendingPost);            }        } catch (Exception e) {        } finally {        }    }    void enqueue(Subscription subscription, Object event) {        queue.add(new PostBeen(event, subscription));        sendMessage(obtainMessage());    }}
AsyncPoster
package com.lvshujun.event;import java.util.LinkedList;import java.util.Queue;public class AsyncPoster implements Runnable {    private Queue queue;    private EventBus eventBus;    public AsyncPoster(EventBus eventBus) {        // super(looper);        this.eventBus = eventBus;        queue = new LinkedList();    }    @Override    public void run() {        // TODO Auto-generated method stub        try {            PostBeen pendingPost = queue.poll();            if (pendingPost == null) {            } else {                eventBus.invokeSubscriber(pendingPost);            }        } catch (Exception e) {        } finally {        }    }    void enqueue(Subscription subscription, Object event) {        queue.add(new PostBeen(event, subscription));        eventBus.executorService.execute(this);    }}
PostBeen
package com.lvshujun.event;public class PostBeen {    public Object event;    public Subscription subscription;    public PostBeen(Object obj,Subscription sub)    {        this.event = obj;        this.subscription = sub;    }}
EventBus
package com.lvshujun.event;import java.lang.reflect.InvocationTargetException;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;import java.util.concurrent.CopyOnWriteArrayList;import java.util.concurrent.ExecutorService;import java.util.concurrent.Executors;import android.os.Looper;public class EventBus {    private SubscriberMethodFinder methodFinder;    ExecutorService executorService = Executors.newCachedThreadPool();    private HandlerPoster handlerPoster;    private AsyncPoster asyncPoster;    private final Map, CopyOnWriteArrayList> subscriptionsByEventType;    private final Map>> typesBySubscriber;    public static EventBus single;    public static EventBus getDefault() {        if (single == null) {            synchronized (EventBus.class) {                if (single == null) {                    single = new EventBus();                }            }        }        return single;    }    public synchronized void register(Object subscriber) {        List subscriberMethods = methodFinder.findSubscriberMethods(subscriber.getClass());        for (SubscriberMethod method : subscriberMethods) {            Class eventType = method.eventType;            CopyOnWriteArrayList subscriptions = subscriptionsByEventType.get(eventType);            Subscription newSubscription = new Subscription(subscriber, method);            if (subscriptions == null) {                subscriptions = new CopyOnWriteArrayList();                subscriptionsByEventType.put(eventType, subscriptions);            }            subscriptions.add(newSubscription);            List> subscribedEvents = typesBySubscriber.get(subscriber);            if (subscribedEvents == null) {                subscribedEvents = new ArrayList>();                typesBySubscriber.put(subscriber, subscribedEvents);            }            subscribedEvents.add(eventType);        }    }    public synchronized void unregister(Object subscriber) {        List> subscribedTypes = typesBySubscriber.get(subscriber);        if (subscribedTypes != null) {            for (Class eventType : subscribedTypes) {                List subscriptions = subscriptionsByEventType.get(eventType);                if (subscriptions != null) {                    int size = subscriptions.size();                    for (int i = 0; i < size; i++) {                        Subscription subscription = subscriptions.get(i);                        if (subscription.subscriber == subscriber) {                            System.out.println(subscription.subscriberMethod.method.getName());                            subscriptions.remove(i);                            i--;                            size--;                        }                    }                }            }            typesBySubscriber.remove(subscriber);        }    }    public EventBus() {        methodFinder = new SubscriberMethodFinder();        subscriptionsByEventType = new HashMap, CopyOnWriteArrayList>();        typesBySubscriber = new HashMap>>();        handlerPoster = new HandlerPoster(this, Looper.getMainLooper());        asyncPoster = new AsyncPoster(this);    }    public void post(Object obj) {        List subscriptions = subscriptionsByEventType.get(obj.getClass());        if (subscriptions != null) {            for (Subscription subscription : subscriptions)            {                System.out.println(subscription.subscriberMethod.method.getName());                //handlerPoster.enqueue(subscription, obj);                asyncPoster.enqueue(subscription, obj);            }        }    }    public void invokeSubscriber(PostBeen been)            throws IllegalArgumentException, IllegalAccessException, InvocationTargetException {        Subscription sub = been.subscription;        sub.subscriberMethod.method.invoke(sub.subscriber, been.event);    }}

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.