Android custom progress indicator while doing AsyncTask

末鹿安然 提交于 2019-12-11 11:39:20

问题


When running an AsyncTask I want to display a progress indicator. This stock one is ugly so I made a layout with my own. There lies the problem however, it's a separate layout meaning I need a new activity to display it.

I thought I could startActivity(~progressActivity~) and then ... destroy it? But it seems impossible to stop a child activity from the parent.

I could hide the progress indicator on the AsyncTask-invoking-activity, then when processing make it display and hide the rest of the layout. Now I need two sub-layouts in my main layout and hide one of each every time. This sounds like a hack and I don't fancy it much.

Any suggestions?


回答1:


If you need more control over your ProgressBar, you can use WindowManager to add a view on top of everything. It can be done without any additional layout, activities or windows. You can control animation, touches, position and visibility just like in case of regular view. Fully working code:

final ProgressBar view = new ProgressBar(TestActivity.this);
view.setBackgroundColor(0x7f000000);
final LayoutParams windowParams = new WindowManager.LayoutParams();
windowParams.gravity = Gravity.CENTER;
windowParams.height = WindowManager.LayoutParams.WRAP_CONTENT;
windowParams.width = WindowManager.LayoutParams.WRAP_CONTENT;
windowParams.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
        | WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
        | WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN;
windowParams.format = PixelFormat.TRANSLUCENT;
windowParams.windowAnimations = 0;

new AsyncTask<Integer, Integer, Integer>() {
    public void onPreExecute() {
        // init your dialog here;
        getWindowManager().addView(view, windowParams);
    }

    public void onPostExecute(Integer result) {
        getWindowManager().removeView(view);
        // process result;
    }

    @Override
    protected Integer doInBackground(Integer... arg0) {
        // do your things here
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }
}.execute();



回答2:


new AsyncTask<Params, Progress, Result>() {
   Dialog dlg = new Dialog();
   View customProgress;
   public void onPreExecute() {
       //init your dialog here;
       View customProgress = LayoutInflater.from(CurrentActivity.this).inflate(R.layout.your_progress_layout, null, false);
       dialog.setContentView(view);
       dialog.show();
   }
   public Result doInBackground(Params... params) {
    // do something
    publishProgress(progress);  
   }
   public void onProgressUpdate(Progress progress) {
    // do something with progressView;
   }
   public void onPostExecute(Result result) {
      dlg.dissmiss();
      // process result;
   }
}.execute();

This is the possible way ( only sample, may contains errors)



来源:https://stackoverflow.com/questions/15085874/android-custom-progress-indicator-while-doing-asynctask

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!