2014-06-10 2 views
0

내 응용 프로그램은 Loopers ReceiverAccLooper 및 GetListingsLooper가 포함 된 두 개의 스레드를 생성하는 broadcastreciever를 사용합니다. 이 두 루퍼는 실행을 위해 다른 작업을 호출 한 다음 결과를 다시 루퍼로 반환합니다. 루퍼의 결과를 주요 작업으로 되돌려 데이터를 추가 처리하는 방법을 알 수 없습니다.루퍼에서 메인 스레드로 메시지 또는 데이터 전달

콜백 및 처리기를 사용해 본 결과, pool.awaitTermination을 사용하여 성공한 것은 성공하지 못했습니다. 내 이해는 이것을 처리하는 가장 좋은 방법은 핸들러를 사용하는 것이지만 핸들러가 루퍼에서 주 태스크로 보내는 방법을 알아낼 수 없다. 이 작업을 수행하는 가장 좋은 방법에 대한 통찰력은 매우 유용 할 것입니다.

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 

} 

하나 ....

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

} 

}

답변

0

주 스레드에서 핸들러 새로운 에디션을 사용하거나 지정 생성 주 스레드의 루퍼입니다. 메시지 또는 실행 파일을 해당 처리기에 게시하면 처리해야합니다.

+0

답장을 보내 주셔서 감사합니다. 나는 이것을하려고했기 때문에 실제로 작동하는 코드를 생각해 낼 수는 없다. 루퍼에서 메인 쓰레드의 핸들러를 "아는"핸들러를 만들거나 그 반대의 경우 어떻게 할 것인가? 루퍼가 메시지를 게시하고 주 스레드가 다시 시작하면 게시하는 것이 이상적입니다. – user3562301

+0

루퍼에 핸들러를 만들지 마십시오. 당신의 onCreate에 액티비티를 생성하고 생성자의 Loopers에게 넘깁니다. 이것이 핸들러가 작동 할 수있는 한 가지 방법입니다. 보너스, 그럼 당신은 하나를 만들 필요가 (당신은 모든 루퍼에 동일한 하나를 전달할 수 있습니다). –

관련 문제