简体   繁体   English

将值从线程传递到主要活动

[英]Pass value from thread to main activity

I've been searching this online for a long time. 我已经在网上搜索很长时间了。 Maybe what I'm doing here is wrong. 也许我在这里做的是错误的。

I have written a thread class in a separate file from MainActivity.java. 我已经在与MainActivity.java分开的文件中编写了一个线程类。 Because both the thread and the main activity are relatively long, I decided to separate them into different files. 因为线程和主活动都比较长,所以我决定将它们分为不同的文件。

I wanted to pass some value generated from the thread class to the main activity. 我想将线程类生成的一些值传递给main活动。 Initially I want to use handlers. 最初,我想使用处理程序。 But because the thread is in a different class to the main activity. 但是因为线程与主要活动不在同一个类中。 It has no idea the handler I defined in the main activity. 不知道我在主活动中定义的处理程序。

public class mythread implements Runnable{
    @Override
    public void run(){
        result = result_from_some_task();
    }
}

This is the basic structure of my thread class and I want to pass result back to the main activity. 这是我的线程类的基本结构,我想将结果传递回main活动。 I've looked at many examples, most of them the thread is within the main activity class and the handlers defined can be easily referred to. 我看过许多示例,其中大多数线程都位于主活动类中,并且可以轻松引用定义的处理程序。

Intent doesn't seems to be applicable. 意图似乎不适用。 Does anyone have any idea on how such operations can be done? 是否有人对如何进行此类操作有任何想法?

Thanks in advance. 提前致谢。

Make parameterized constructor of AnotherClass and when you make of object of AnotherClass then simply pass object of MainActivity into that constructor and inside AnotherClass class where you want to call MainActivity's method then simply call that method from that Object. 创建AnotherClass的参数化构造函数,当您构造AnotherClass的对象时,只需将MainActivity的对象传递到该构造函数中,然后在要调用MainActivity的方法的AnotherClass类内部,然后从该Object调用该方法即可。

check following code : 检查以下代码:

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    AnotherClass object= new AnotherClass (this);
    object.start();
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

public void makeSomeCalculation() {
    //logic to change some UI 
}
}

and check Another class : 并检查另一个类:

public class AnotherClass extends Thread {

MainActivity mainActivity;

public AnotherClass (MainActivity mainActivity) {
    // TODO Auto-generated constructor stub

    this.mainActivity = mainActivity;
}

public void run() {
   //write other logic
        mainActivity.makeSomeCalculation();
   //write other logic
 }
}

you need a handler in your activity. 您的活动中需要一个处理程序。 and when your thread finishes , you then dispatch a message to handler , notifying that thread execution finished. 当线程完成时,然后向处理程序调度消息,通知该线程执行完成。 see here for example. 例如看这里 you can also use interface for this. 您也可以为此使用界面。 see here for example. 例如看这里 In answer, he use interface to notify from asyntask. 作为回答,他使用接口从asyntask进行通知。 you can do same for thread. 您可以对线程执行相同操作。

This may not be what you are looking for so consider it as suggestion to avoid long term headaches.Try EventBus . 这可能不是您要查找的内容,因此应将其视为避免长期头痛的建议尝试使用EventBus This a library to communicate easily between various components in Android. 这个库可轻松在Android中的各个组件之间进行通信。

Your need to run the activity function within runOnUiThread. 您需要在runOnUiThread中运行活动功能。

mainActivity.runOnUiThread(new Runnable() {
    @Override
    public void run() {
        mainActivity.makeSomeCalculation();
    }
});                      

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM