How to trigger actions from a non-information topic?

I have an observer design pattern that uses Thread to send my own Events. Sometimes the classes that are called are Android actions, and in other cases they are ordinary classes. The code in the stream is NOT an action.

I was getting this error:

Unable to create handler inside thread that did not call Looper.prepare ()

To avoid this error at runtime, I had to add Looper.prepare()Thread inside. I know about this post: It is not possible to create a handler inside a thread that Looper.prepare () did not call , it contains better information for this kind of problem, but still I don’t know how to integrate the mjosh answer into my topic.


Problem

A call from flow to action never occurs. Separately, this code works, but not when the "Actions" are waiting for a call.

This class will send events to other classes. This works with a stream.

public class EventBus
{
    // Non essential code here.
    ...
    // Thread code.
    private class MyThread implements Runnable
    {
        @Override
        public void run()
        {
            while(true)
            {
               synchronized(list)
               {
                   for(Observer o : list)
                       // Execution transfers to Activity here.
                      o.handleEvent(event);                   
               }
            }
         } 
     }
}

Inside the action.

public class FirstActivity extends Activity implements Observer
{
    public void handleEvent()
    {
      // Never gets here.
    }
}

So how do I solve this problem? In my application there will be many actions and many ordinary classes.

To solve my problem

"". , .

+4
1

Android, , , . , , , , . - Threading Activity. . . POJO, SSCCE . , , - , EventBus.

public class Test {

    public static void main(String[] args) throws InterruptedException {
        //create the thread to demonstrate execution
        MyThread t = new MyThread();
        //add a non activity observer
        t.addObserver(new NonActivityObserver());
        //add an activity observer
        t.addObserver(new ActivityDecendent());
        //fire off the thread
        new Thread(t).start();
        //give us time to read the output
        Thread.sleep(Long.MAX_VALUE);       
    }


    public static class MyThread implements Runnable
    {
        private ArrayList<Observer> list = new ArrayList<Observer>();

        public void addObserver(Observer arg0) {
            synchronized (list) {
                list.add(arg0);
            }
        }
        @Override
        public void run()
        {
            //no loop just doing this once for example
           synchronized(list)
           {
               for(final Observer o : list) {
                   //first try to cast to an Activity
                   try {
                       Activity act = (Activity)o;
                       //if we get here, its an activity 
                       //so invoke on its UiThread
                       act.runOnUiThread(new Runnable() {

                        @Override
                        public void run() {
                            Event event = new Event("From the activity runOnUiThread method");
                            o.handleEvent(event);
                        }

                       });
                   }
                   catch (Exception e) {
                       //if we got here, the class is not an activity
                       //so it should be safe to just handle the event
                       //on this thread
                       Event event = new Event("From observers handle event method");
                       o.handleEvent(event);
                   }   
               }                      
           }
         } 
     }

    //your observer interface
    public static interface Observer {
        public void handleEvent(Event e);
    }

    //Your Event class
    public static class Event {
        private String message;
        public Event(String message) {
            this.message = message;
        }
        public void talk() {
            System.out.println(message);
        }
    }

    //An observer which isnt an activity
    public static class NonActivityObserver implements Observer {

        @Override
        public void handleEvent(Event e) {
            e.talk();
        }

    }

    //An activity which implements Observer
    public static class ActivityDecendent extends Activity implements Observer {
        @Override
        public void handleEvent(Event e) {
            e.talk();
        }
    }

    //An Activity 
    public static class Activity {
        //pretend this is the Android activity executing this runnable
        //on the UI thread
        public void runOnUiThread(Runnable r) {
            new Thread(r).start();
        }
    }
}

:

From observers handle event method
From the activity runOnUiThread method

, ... , .

+2

All Articles