简体   繁体   中英

AsyncTask block Unity(UI) thread issue

Here is my main Activity in OnCreate :

        mUnityPlayer = new UnityPlayer(this);
        setContentView(mUnityPlayer);
        mUnityPlayer.requestFocus();

        new SimuDownloadTask(this).execute();

The following code is the source code of SimuDownloadTask :

    public class SimuDownloadTask extends AsyncTask<Void, Integer, Boolean> {

    private ProgressDialog progressDialog;
    private int count=0;
    private Context mainActivityContext;

    public SimuDownloadTask(Context context) {
        mainActivityContext=context;
    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        progressDialog=new ProgressDialog(mainActivityContext,R.style.XMNewDialog);
        progressDialog.show();
    }

    @Override
    protected Boolean doInBackground(Void... arg0) {
        while(true)
        {
            try {
                Thread.sleep(200);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            int downloadPercent=doDownload();
            publishProgress(downloadPercent);
            if(downloadPercent>=500)
                break;
        }
        return true;
    }

    @Override
    protected void onProgressUpdate(Integer... values) {
        super.onProgressUpdate(values);
        progressDialog.setMessage("current progress:"+values[0]+"%");
    }

    @Override
    protected void onPostExecute(Boolean result) {
        super.onPostExecute(result);
        progressDialog.dismiss();
        if(result)
        {
            Toast.makeText(mainActivityContext, "success", Toast.LENGTH_SHORT).show();  
        }
        else
        {
            Toast.makeText(mainActivityContext, "fail", Toast.LENGTH_SHORT).show();  
        }
    }

    private int doDownload()
    {
        count+=1;
        return count;
    }

}

Here is the problem. When I start the app, the progressbar blocks the UI thread. After the progressbar finished, the Unity starts.

When I replace the SimuDownloadTask in OnCreate with the following code:

new Thread(){
@Override
public void run() {
    super.run();
    Looper.prepare();
    progressDialog=new ProgressDialog(UnityPlayerActivity.this,R.style.XMNewDialog);
    progressDialog.setTitle("test");
    progressDialog.setCancelable(true);
    progressDialog.show();
    Looper.loop();
}
}.start(); 

The unity thread is running properly(not blocked by the progressbar). So I think the problem is not relevant to Unity.

I have already checked the relevant links such as: Asynctask from non ui thread But still can't figure out the issues. Any clues will be helpful.

Instead of using an AsyncTask you may use a worker thread for the counter logic and a handler to update the progress bar. See the updated answer in this link

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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