我有一个线程,我想进行一些计算,然后将其发送到程序的主要 Activity 中。我该怎么做?

线程是

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发送到主要 Activity 。从这里的 Activity 中调用此计时器
Timer timer = new Timer();
                       timer.schedule(new RemindTask(), 0,30000);

然后,我需要将IntegerResult应用于主要 Activity
主要如此
主要方法是
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]);
    }

最佳答案

有两种方法:

  • 如前所述,在ASyncTask上使用它。
  • 创建一个处理程序以接收响应。

  • 就像是:
    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方法,它将像那样。

    关于java - 如何将数据从线程发送到主要 Activity ,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/16609956/

    10-10 07:06