我在 android 中的 AsyncTask 没有在后台执行,为什么?

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;

   }       

}

所以我在主 activity 中创建了一个布尔值,并在该任务中设置了代码,例如 while(!mainActivity.timeNotEnds)