Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 将消息或数据从活套传递到主线程_Java_Android_Handlers_Looper - Fatal编程技术网

Java 将消息或数据从活套传递到主线程

Java 将消息或数据从活套传递到主线程,java,android,handlers,looper,Java,Android,Handlers,Looper,我的应用程序使用了一个广播接收器,它产生了两个线程,其中包含Looper ReceiverAccLooper和GetListingsLooper。这两个循环器然后调用一些其他任务来运行,然后将结果返回给循环器。我不知道如何将活套的结果返回到主任务以进一步处理数据 我尝试过回调和处理程序,唯一的成功就是使用pool.com,但这似乎有些草率。我的理解是,最好的方法是使用处理程序,但我不知道如何让处理程序从活套发送到主任务。任何关于实现这一点的最佳方法的见解都将非常有用 主线程 public voi

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

我尝试过回调和处理程序,唯一的成功就是使用pool.com,但这似乎有些草率。我的理解是,最好的方法是使用处理程序,但我不知道如何让处理程序从活套发送到主任务。任何关于实现这一点的最佳方法的见解都将非常有用

主线程

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

}
public void onReceive(上下文、意图){
Log.d(“Plog”,“接收帐户收到警报”);
DatabaseHandler db=新的DatabaseHandler(上下文);
List contacts=db.getAllContacts();
日志d(Plog,“联系人”+联系人);
用于(联系人cn:联系人){
username=cn.getName();
password=cn.getPassword();
acb=cn.getBalance();
strategykey=Integer.parseInt(cn.getStrategy());
maxamountperautoloan=cn.getMaxperloan();
int n=2;
//构建固定数量的线程池
ExecutorService池=Executors.newFixedThreadPool(n);
提交(新的ReceiverAccLooper(用户名、密码、acb、maxamountperautoloan、strategykey));
提交(新的GetListingsLooper(用户名、密码、策略键));
pool.shutdown();
//这就是我一直试图放置一个处理程序以从上面的两个循环器获取输出的地方
}
其中一个活套

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

}   
公共类ReceiverAccLooper实现可运行{
私有字符串用户名;
私有字符串密码;
私人字符串maxamountperloan;
私人字符串acb;
私有整数策略;
列出结果;
公共ReceiverAccLooper(字符串\u用户名、字符串\u密码、字符串\u acb、字符串\u maxamountperloan、整数策略){
this.username=\u username;
this.password=\u password;
this.maxamountperloan=\u maxamountperloan;
this.strategy=\u策略;
this.acb=_acb;
}
@凌驾
公开募捐{
Looper.prepare();
Log.d(“Plog”、“In-Looper”+用户名+“”+密码+“”+maxamountperloan+“”+strategy+“”+acb);
GetLoansReturn glr=新的GetLoansReturn();
glr.getLoanRunner(用户名、密码、acb、maxamountperloan);
结果=glr.planetsList;
Log.d(“Plog”,用户名+“Looper Result Owned Listings”+结果);
对象msg=结果;
已发送消息=(消息)消息;
//我想从这里发信息。。。。
Looper myLooper=Looper.myLooper();
loop.loop();
myLooper.quit();
}   

}

在主线程上使用一个新的处理程序,或者指定主线程的循环器来创建处理程序。将消息或runnable发布到该处理程序应该可以做到这一点。

感谢您的回复。我已经尝试过这样做,但我认为无法产生实际工作的代码。如何在循环器中创建一个“知道”的处理程序主线程中的处理程序,反之亦然。理想情况下,我希望循环器在完成后发布一条消息,然后主线程继续。您不在循环器中创建处理程序。在活动的onCreate中创建一个处理程序,并将其传递给循环器的构造函数。这是确保处理程序工作的一种方法。Bonus,那么您只需要创建一个(您可以将相同的onus传递给所有的循环器)。