简体   繁体   English

为什么 AsyncTask doInBackground 不运行但第二次运行

[英]How come in AsyncTask doInBackground doesn't run but on second time it does runs

So I am new with this and I need to call function that would run AsyncTask from separate file.所以我是新来的,我需要调用从单独的文件运行 AsyncTask 的函数。

MainActivity.java code MainActivity.java 代码

public class MainActivity extends AppCompatActivity {
String res;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Asynctasktest newAsy = new Asynctasktest();



    res = newAsy.ValidateUser();

    Toast.makeText(getBaseContext(), "'"+res+"'", Toast.LENGTH_LONG).show();
}

}

and AsyncTaskTest.java code和 AsyncTaskTest.java 代码

public class Asynctasktest extends MainActivity {

String Res;

private class GetFWork extends AsyncTask<Void,Void,String> {

    @Override
    protected void onPreExecute() {
        Res = "onPreExecute";
    }

    @Override
    protected String doInBackground(Void... param) {

        return "am i here";
    }

    @Override
    protected void onPostExecute(String Result) {
        super.onPostExecute(Result);
        Res = Result;
    }
}

public String ValidateUser(){

    final GetFWork Fl = new GetFWork();

    Fl.execute();

    return Res;
}


}

So I need to get text "am i here", but I get "onPreExecute", which shows that on first call it doesnt do doInBackground.所以我需要得到文本“我在这里”,但我得到“onPreExecute”,这表明在第一次调用时它不做 doInBackground。

This is how AsyncTask works.这就是 AsyncTask 的工作原理。

res = newAsy.ValidateUser();
Toast.makeText(getBaseContext(), "'"+res+"'", Toast.LENGTH_LONG).show();

are both executed at the same time, and thus - since AsyncTask is asynchronous - the value of res is still "onPreExecute".两者同时执行,因此 - 由于 AsyncTask 是异步的 - res 的值仍然是“onPreExecute”。

Good point made by Wukash.乌卡什提出的好观点。 Just to elaborate.只是为了详细说明。 When ValidateUser() is returning Res , there is no guarantee that the AsyncTask has completed running, since, its asynchronous, and runs off the main thread.当 ValidateUser() 返回Res ,不能保证AsyncTask已经完成运行,因为它是异步的,并且在主线程之外运行。 Hence, in your case, it returns null , since Res never got updated.因此,在您的情况下,它返回null ,因为Res从未更新过。

If you require returning the value result to update UI or something, send a handler function reference as callback, and make that do this work.如果您需要返回值result以更新 UI 或其他内容,请将处理程序函数引用作为回调发送,并使其执行此操作。

Please let me know if you need some further explanation.如果您需要进一步解释,请告诉我。 I recall having had a similar question when I was implementing an AysncTask for the first time.我记得在我第一次实施AysncTask时遇到过类似的问题。

Try calling the super in this method尝试在此方法中调用 super

@Override
protected void onPreExecute() {
    super.onPreExecute();
    Res = "onPreExecute";
}

Also remember that you cannot call the same AsyncTask object twice or you'll get this error:还要记住,你不能两次调用同一个 AsyncTask 对象,否则你会得到这个错误:

Cannot execute task: the task has already been executed (a task can be executed only once)

So the approach you have in your ValidateUser() method creating a new GetFWork() is good.因此,您在 ValidateUser() 方法中创建新 GetFWork() 的方法很好。

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

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