【问题标题】:Pass message or data from looper to main thread将消息或数据从 Looper 传递到主线程
【发布时间】:2014-06-10 11:48:37
【问题描述】:

我的应用程序使用一个广播接收器,它产生两个线程,其中包含 Loopers ReceiverAccLooper 和 GetListingsLooper。这两个 Looper 然后调用其他一些任务来运行,然后将结果返回给 Looper。我无法弄清楚如何将循环器的结果返回到主要任务以进一步处理数据。

我已经尝试过回调和处理程序,我唯一的成功是使用 pool.awaitTermination 但这似乎很草率。我的理解是最好的方法是使用处理程序,但我无法弄清楚如何让处理程序从循环器发送到主任务。任何有关执行此操作的最佳方法的见解都会非常有帮助。

主线程...

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

}

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

}   

}

【问题讨论】:

    标签: java android handlers looper


    【解决方案1】:

    在主线程上使用新的处理程序,或创建指定主线程的 Looper。将消息或可运行消息发布到该处理程序应该这样做。

    【讨论】:

    • 感谢您的回复。我已经尝试过这样做,或者我认为并且无法提出实际有效的代码。如何在 Looper 中创建一个“知道”主线程中的处理程序的处理程序,反之亦然。理想情况下,我希望循环器在完成后发布一条消息,然后让主线程恢复。
    • 您不会在 looper 中创建处理程序。在您的活动的 onCreate 中创建一个,并将其传递给构造函数中的 Loopers。这是确保处理程序正常工作的一种方法。奖金,那么您只需要创建一个(您可以将相同的一个传递给所有的活套)。
    猜你喜欢
    • 1970-01-01
    • 2011-01-18
    • 2014-08-04
    • 2018-01-30
    • 1970-01-01
    • 1970-01-01
    • 2019-09-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多