2013-05-17 112 views
1

我有一個線程,我想進行一些計算,然後將其發送到我的程序的主要活動。我該怎麼做?如何從線程發送數據到主要活動java android

線程是

class RemindTask extends TimerTask { 
    public void run() { 
     flag = true; 
     System.out.println(" Hello World!"); 
     for (int e=0; e<2; e++){ 
      fileToReceive = fileparts.peek(); 
      System.out.println("fileToReceive "+fileToReceive); 
      fileToReceive = fileparts.remove(); 
     } 
     IntegerResult = 4; 
     flag=false; 
    } 

我想給IntegerResult發送到主要活動。這個計時器從活動從這裏

Timer timer = new Timer(); 
         timer.schedule(new RemindTask(), 0,30000); 

叫之後,我需要採取的IntegerResult的主要活動 到主讓 塔主要方法是

int i=0; 
//IntegerResult = 4; here i need to take the result of the thread 
while (!fileparts.isEmpty() ) { //&& !fileparts1.isEmpty() && (flag == false) 
    String[] myStringArray = new String[IntegerResult]; 
    for (int e=0; e<IntegerResult; e++){ 
     if (fileToReceive != null && !fileparts.isEmpty()){ 
      fileToReceive = fileparts.peek(); 
      myStringArray[e] = fileToReceive; 
      fileToReceive = fileparts.remove(); 
     } else { 
      myStringArray[e] = " "; 
     } 
     System.out.println("myStringArray["+e+"] " + myStringArray[e]); 
    } 
+2

這是一個常見問題。你應該考慮使用'BlockingQueue',比如'LinkedBlockingQueue'。它允許多個線程共享信息併爲您處理所有的同步和信號。 – Gray

回答

3

有兩種方式:

  1. 上的AsyncTask使用此,如前所述。
  2. 創建一個處理程序來接收響應。

類似:

public static int YOUR_INT_MESSAGE = 100; 

class RemindTask extends TimerTask { 
    public void run() { 
     flag = true; 
     System.out.println(" Hello World!"); 
     for (int e=0; e<2; e++){ 
      fileToReceive = fileparts.peek(); 
      System.out.println("fileToReceive "+fileToReceive); 
      fileToReceive = fileparts.remove(); 
     } 
     IntegerResult = 4; 
     flag=false; 
     Message msg = new Message(); 
     msg.what = YOUR_INT_MESSAGE; 
     msg.obj = IntegerResult; 
     YourActivity.this.handler.sendMessage(msg); 
    } 

,並在您的MainActivity實現處理

public Handler handler; 

handler = new Handler() { 
    @Override 
    public void handleMessage(android.os.Message msg) { 
     if (msg.what == YOUR_INT_MESSAGE) { 
      int i=0; 
      IntegerResult = msg.obj; 
      while (!fileparts.isEmpty() ) { //&& !fileparts1.isEmpty() && (flag == false) 
       String[] myStringArray = new String[IntegerResult]; 
       for (int e=0; e<IntegerResult; e++){ 
        if (fileToReceive != null && !fileparts.isEmpty()){ 
         fileToReceive = fileparts.peek(); 
         myStringArray[e] = fileToReceive; 
         fileToReceive = fileparts.remove(); 
        } else { 
         myStringArray[e] = " "; 
       } 
       System.out.println("myStringArray["+e+"] " + myStringArray[e]); 
      } 
     } 
    } 
} 

我無法測試上面,現在的代碼,但它的將是類似的東西,如果你想要Handler方法。

0

更新

使用自定義BroadcastReceiver

Wri TE這ActivityA.java

Intent intent = new Intent(); 
intent.putExtra("message","hi"); 
intent.setAction("com.android.activity.SEND_DATA"); 
sendBroadcast(intent); 

寫這篇文章ServiceA.java/ActivityB.java

private BroadcastReceiver mMessageReceiver = new BroadcastReceiver() { 
@Override 
public void onReceive(Context context, Intent intent) { 
    // Extract data included in the Intent 
    String message = intent.getStringExtra("message"); 
    Log.d("receiver", "Got message: " + message); 
    } 
}; 

現在註冊Receiver

LocalBroadcastManager.getInstance(mContext).registerReceiver(mMessageReceiver, 
    new IntentFilter("com.android.activity.SEND_DATA")); 
+1

如果您只在答案中包含鏈接,則應將其作爲評論發佈。 –

0

好像你需要的是AsyncTask

你可以像Rstar一樣提供服務,但它有點矯枉過正。

相關問題