0

My app uses a broadcastreciever that spawns two threads which contain Loopers ReceiverAccLooper and GetListingsLooper. These two loopers then call some other tasks to run that then return a result back to the Looper. I cannot figure out how to get the result from the loopers back to the main task to further process the data.

I've tried callbacks and handlers, the only success I've had is using pool.awaitTermination but that seems sloppy. My understanding is the best way to do this is with a handler but I cannot figure out how to get a handler to send from the looper to the main task. Any insight on the best way to do this would be very helpful.

Mainthread...

public void onReceive(Context context, Intent intent) {
    Log.d("Plog", "Reciever accounts received alarm");

    DatabaseHandler db = new DatabaseHandler(context);
    List<Contact> contacts = db.getAllContacts();
    Log.d(Plog, "Contacts" + contacts);
    for (Contact cn : contacts) {
        username = cn.getName();
        password = cn.getPassword();
        acb = cn.getBalance();
        strategykey = Integer.parseInt(cn.getStrategy());
        maxamountperautoloan = cn.getMaxperloan();
        int n = 2;
        // Build a fixed number of thread pool
        ExecutorService pool = Executors.newFixedThreadPool(n);

        pool.submit(new ReceiverAccLooper(username, password, acb, maxamountperautoloan, strategykey));

        pool.submit(new GetListingsLooper(username, password, strategykey));

        pool.shutdown();

        //This is where I've been trying to place a handler to get output from the two loopers above

}

One of the Loopers....

public class ReceiverAccLooper implements Runnable{
private String username;
private String password;
private String maxamountperloan;
private String acb;
private Integer strategy;
List<Map<String, String>> result;

public ReceiverAccLooper(String _username, String _password, String _acb, String _maxamountperloan, Integer _strategy){
    this.username = _username;
    this.password = _password;
    this.maxamountperloan = _maxamountperloan;
    this.strategy = _strategy;
    this.acb = _acb;
}

@Override
public void run() {
    Looper.prepare();
    Log.d("Plog", "In Looper " + username + " " + password + " " + maxamountperloan + " " + strategy + " " + acb);

    GetLoansReturn glr = new GetLoansReturn();
    glr.GetLoansRunner(username, password, acb, maxamountperloan);
    result = glr.planetsList;
    Log.d("Plog", username + " Looper Result Owned Listings " + result);

    Object msg = result;
    Message sent = (Message) msg;

    //I've would like to send the message from here....

    Looper myLooper = Looper.myLooper();
    Looper.loop();
    myLooper.quit();

}   

}

1 Answers1

0

Use a handler new-ed on the main thread, or created specifying the Looper of the main thread. Posting the message or runnable to that handler should do it.

Gabe Sechan
  • 90,003
  • 9
  • 87
  • 127
  • Thank you for the reply. I've tried to do this or so I think and cannot come up with code that actually works. How do I create a handler in the Looper that "Knows" the handler in the main thread or vice versa. I ideally would like the looper to publish a message once it has completed and then for the main thread to resume. – user3562301 Jun 10 '14 at 04:13
  • You don't create a handler in the looper. Create one in your onCreate of your activity and pass it to the Loopers in their constructor. That's one way to assure the handler will work. Bonus, then you only need to create one (you can pass the same one to all your loopers). – Gabe Sechan Jun 10 '14 at 04:16