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();
}
}