2

我有一个线程,我想进行一些计算,然后将其发送到我程序的主要活动。我怎么能这样做?

线程是

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]);
    }
4

3 回答 3

5

有两种方法:

  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 方法,它会是这样的。

于 2013-05-17T13:55:18.303 回答
1

更新

使用自定义BroadcastReceiver

在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"));   
于 2013-05-17T13:15:12.673 回答
0

似乎您需要的是AsyncTask

您可以按照 Rstar 的建议使用 Service,但这似乎有点矫枉过正。

于 2013-05-17T13:22:21.087 回答