如何从Runnable线程获取局部变量

时间:2015-05-20 20:39:28

标签: java android multithreading runnable

我有一个帮助器(非活动)类,它对API进行查询,该API具有一个名为run()的公共函数并在新线程上运行(根据Android规范)。我的MainActivity创建了一个新的MakeQuery对象并运行其run()函数:

MakeQuery q = new MakeQuery();
q.run();

但是,我需要从线程中访问变量。以下是一个简短的代码示例:

public class MakeQuery implements Runnable {

    private void setNewString(String localThreadString){
       //NewString comes out null...
       NewString  = localThreadString;
    }

    public void run() {
        new Thread(new Runnable() {
            public void run() {
                android.os.Process.setThreadPriority(android.os.Process.THREAD_PRIORITY_BACKGROUND);

                API Api = new API(keys and tokens);

                //queryAPI() returns string
                setNewString(queryAPI(Api, term1, term2));

                //Don't know how to send intent from here (No context), 
                //I would like:
                Intent i = new Intent(MainActivity.class, AnotherActivity.class)
        }
    }).start();
}

的AsyncTask

//This runs in background thread
@Override
protected Void doInBackground(Void... params) {
    API Api = new API(keys and tokens);

    setNewString(queryAPI(Api, string1, string2));

    return null;
}

@Override
protected void onPostExecute(Void aVoid) {
    Intent intent = new Intent(mContext, Activity2.class);
    intent.putExtra("NewString", NewString);
    Log.d(MYTAG, NewString);
}

MainActivity

public void buttonPressed(View view) {
    Intent intent = new Intent(this, Activity2.class);
    ...
    MakeQuery task = new MakeQuery(this);
    task.execute();

    startActivity(intent);
}

我试图在网上看几个小时。我曾尝试过做AsyncTask,但我不知道如何用我所拥有的方式实现这一点。此外,使用localThread<String>没有帮助。

TLDR:我想知道是否可以获取NewString,以便我可以通过Intent将其传递给另一个Activity。

解决方案 不要使用Runnable,请创建一个新的AsyncTask,如下所示。另外,请确保StartActivity位于帮助程序类中,而不是MainActivity中。这是因为在开始新活动之前我没有等待完成任务。

2 个答案:

答案 0 :(得分:4)

以下是使用异步任务的实现:

public class MakeQueryTask extends AsyncTask<Void, Void, Void> {

    private Context mContext;

    private String newString;

    public MakeQueryTask(Context context) {
        mContext = context;
    }

    //This runs on a background thread
    @Override
    protected Void doInBackground(Void... params) {


        API Api = new API(keys and tokens);

        //queryAPI() returns string
        setNewString(queryAPI(Api, term1, term2));

        //You should start your activity on main thread. Do it in onPostExecute() which will be invoked after the background thread is done
        //Intent i = new Intent(mContext, AnotherActivity.class);
        //mContext.startActivity(intent);
        return null;
    }

    private void setNewString(String localThreadString){
        newString  = localThreadString;
    }

    //This will run on UI thread
    @Override
    protected void onPostExecute(Void aVoid) {
        Intent intent = new Intent(mContext, AnotherActivity.class);
       mContext.startActivity(intent);
    }
}

你会这样执行:

 MakeQueryTask task = new MakeQueryTask(this); //Here this is an activity (context)
 task.execute();

答案 1 :(得分:2)

如果您从runnableactivity调用此service,则可以在构造函数中传入context。然后使用intent函数中的run()启动活动。

public class MakeQuery implements Runnable {

    private Context context;

    public MakeQuery(Context context) {
        this.context = context;
    }

    private void setNewString(String localThreadString){
       //NewString comes out null...
       NewString  = localThreadString;
    }

    public void run() {
        new Thread(new Runnable() {
            public void run() {
                android.os.Process.setThreadPriority(android.os.Process.THREAD_PRIORITY_BACKGROUND);

                API Api = new API(keys and tokens);

                //queryAPI() returns string
                setNewString(queryAPI(Api, term1, term2));

                Intent intent = new Intent(MainActivity.class, AnotherActivity.class)
                context.startActivity(intent);
        }
    }).start();
}