AsyncTask ; doInbackground() not called after calling method onPreExecute()

前端 未结 4 1204
星月不相逢
星月不相逢 2021-02-01 03:34

I did added async library at my project and checked already, I don\'t know why code flow doesn\'t go in to asynctask

Code

public void doMysql()
{
    Log         


        
4条回答
  •  生来不讨喜
    2021-02-01 04:31

    You should execute your task with executor

    task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
    

    Because in lower versions of Android all AsyncTasks were executed at single background thread. So new tasks might be waiting, until other task working.

    In lower versions in Android (actually on pre-HONEYCOMB) you can't execute AsyncTask on executor.

    Change your code to

    public void executeAsyncTask()
    {
        AsyncTask task = new AsyncTask() {
            @Override
            protected void onPreExecute() {
                super.onPreExecute();
                Log.e("AsyncTask", "onPreExecute");
            }
    
            @Override
            protected String doInBackground(Void... params) {
                Log.v("AsyncTask", "doInBackground");
                String msg = null;
                // some calculation logic of msg variable
                return msg;
            }
            @Override
            protected void onPostExecute(String msg) {
                Log.v("AsyncTask", "onPostExecute");
            }
        };
    
        if(Build.VERSION.SDK_INT >= 11/*HONEYCOMB*/) {
            task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
        } else {
            task.execute();
        }
    }
    

提交回复
热议问题