[英]How to get a local variable from a Runnable Thread
我有一個幫助器(非活動)類,它對API進行查詢,該API具有一個名為run()
的公共函數,並在一個新線程上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);
}
主要活動
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
。 這是因為我沒有在開始新活動之前等待完成任務。
以下是使用異步任務的實現:
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();
如果從activity
或service
調用此runnable
,則可以在構造函數中傳入context
。 然后在run()
函數中啟動intent
。
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();
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.