[英]My AsyncTask in android not executing do in background , Why?
我的 AsyncTask 沒有執行 doInBackground , preExecute 正在運行,我用 Toast 對其進行了測試。
我也在運行另一個異步任務,但是當我開始這個任務時,我取消了那個任務,它仍然無法工作。
它沒有拋出任何錯誤仍然發生在我身上。
這是我的代碼,任何幫助將不勝感激。
有些代碼被截斷了。 我只是展示重要的部分。
對不起,如果我的英語有任何錯誤。
// AsyncTask
package com.test.testing;
//imports
public class Update_Score extends AsyncTask<Void, Void, Void> {
String result="vvv",
name,
score,
lastTest,
maximum;
ProgressBar loading;
TextView text;
Context context;
@Override
protected Void doInBackground(Void... voids) {
String strUrl="http://test.tk/updatesomething.php";
while (result=="vvv") {
try {
URL url = new URL(strUrl);
HttpURLConnection httpConnection = (HttpURLConnection) url.openConnection();
httpConnection.setRequestMethod("POST");
httpConnection.setDoOutput(true);
httpConnection.setDoInput(true);
//output
OutputStream output = httpConnection.getOutputStream();
BufferedWriter writer=new BufferedWriter(new OutputStreamWriter(output,"UTF-8"));
String post_data= URLEncoder.encode("name","UTF-8")+"="+URLEncoder.encode(name,"UTF-8")+"&"
+URLEncoder.encode("score","UTF-8")+"="+URLEncoder.encode(score,"UTF-8")+"&"
+URLEncoder.encode("lastTest","UTF-8")+"="+URLEncoder.encode(lastTest,"UTF-8");
writer.write(post_data);
writer.flush();
writer.close();
output.close();
//input
InputStream input = httpConnection.getInputStream();
BufferedReader reader=new BufferedReader(new InputStreamReader(input,"iso-8859-1"));
result="";
String line="";
while ((line=reader.readLine())!=null){
result += line;
}
reader.close();
input.close();
httpConnection.disconnect();
} catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) {
e.printStackTrace();
}
}
new AlertDialog.Builder(context).setMessage("updateEnd").create().show();
return null;
}
}
// Calling AsyncTask
package com.test.testing;
//imports
public class Final_Score extends AppCompatActivity {
public TextView score;
public ProgressBar loading;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_final__score);
initVar();
String totalMark =getIntent().getStringExtra("TOTAL_MARKS"),
name =getIntent().getStringExtra("NAME") ,
lastTest =getIntent().getStringExtra("TESTNUM"),
maximum =getIntent().getStringExtra("MAX");
boolean forced=getIntent().getBooleanExtra("FORCED",false);
if(forced){
new AlertDialog.Builder(this).setMessage("Time Up").setTitle("").create().show();
}
score.setText(totalMark);
Update_Score update=new Update_Score(name,totalMark,lastTest,loading,this,score,maximum);
update.execute();
}
}
提前致謝。
我遇到了我所說的正在運行的任務也沒有取消的問題,即使我調用AsyncTask.cancel(true)
它是一個循環直到特定時間(如 11:17)到達的任務。
while(!mainActivity.timeNotEnds){
//some stuff here
if (time==previouslySpecifiedTime){
break;
}
}
所以我在主活動中創建了一個布爾值,並在該任務中設置代碼,如while(!mainActivity.timeNotEnds)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.