如何将数据从线程发送到主要活动java android

时间:2013-05-17 13:11:37

标签: java android multithreading

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

线程是

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

3 个答案:

答案 0 :(得分:4)

有两种方法:

  1. 如前所述,在ASyncTask上使用它。
  2. 创建一个处理程序以接收响应。
  3. 类似的东西:

    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方法的话就会有类似的东西。

答案 1 :(得分: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"));   

答案 2 :(得分:0)

看起来像你需要的是AsyncTask

你可以使用Rstar提出的服务,但似乎有点矫枉过正。