如何使用Asynctask get方法进度?

时间:2016-07-15 11:47:38

标签: android android-asynctask

我想使用进度条...但是当我搜索时,进度条不能用于Asynctask.get.But我必须在Asynctask中使用.get和progress。

我做了非常简单的来源。 如何更改为在主线程中显示进度条?

我想同时使用get方法和ui进度。

pom.xml

请帮帮我。 谢谢!!

4 个答案:

答案 0 :(得分:1)

在致电AsyncTask

之前执行此操作
private ProgressDialog progDailog = null;

public void onCreate(Bundle savedInstanceState) {
    btn.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            AAA asyncTask = new AAA();
            try {
                progDailog = new ProgressDialog(ViewTestActivity.this);
                progDailog.setMessage("Loading...");
                progDailog.setIndeterminate(false);
                progDailog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
                progDailog.setCancelable(true);
                progDailog.show();
                ((AAA) asyncTask).execute(null, null, null,null);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (ExecutionException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    });
}
public class AAA extends AsyncTask<Object, String, Object> {



    @Override
    protected Object doInBackground(Object... params) {
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return params;
    }

    @Override
    protected void onPostExecute(Object result) {
        progDailog.dismiss();
    }
}

答案 1 :(得分:0)

我不知道是否有标准答案,但我刚刚通过在主线程上设置监听器,并从异步任务向监听器发送进度消息而做了类似的事情 - 实际上在我的情况下它是从数据库异步加载。对我来说很好。

答案 2 :(得分:0)

您应该在onProgressUpdate

中更新进度条百分比

请参阅此示例 AsyncTask with progress bar

答案 3 :(得分:0)

试试这个:

private ProgressDialog progDailog;

public void onCreate(Bundle savedInstanceState) {
        btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
        progDailog = new ProgressDialog(ViewTestActivity.this);
        progDailog.setMessage("Loading...");
        progDailog.setIndeterminate(false);
        progDailog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        progDailog.setCancelable(true);
        progDailog.show();
        AAA asyncTask = new AAA(progDialog);
        try {
        ((AAA) asyncTask).execute(null, null, null,null).get();
        } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        } catch (ExecutionException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        }
        }
        });
        }
public class AAA extends AsyncTask<Object, String, Object> {

    private ProgressDialog progressDialog;

    public AAA (ProgressDialog progressDialog) {
        this.progressDialog = progressDialog;
    }

    @Override
    protected Object doInBackground(Object... params) {
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return params;
    }

    @Override
    protected void onPostExecute(Object result) {
        progressDialog.dismiss();
    }
}