Android - Loading, please wait

前端 未结 3 1075
轮回少年
轮回少年 2021-01-30 13:51

Is there a standard \"Loading, please wait\" dialog I can use in Android development, when I invoke some AsyncTask (downloading some data from remote service for example)?

3条回答
  •  春和景丽
    2021-01-30 14:48

    If you implement runnable as well as extending Activity then you could handle the code like this...

    private ProgressDialog pDialog;
    
    public void downloadData() {
        pDialog = ProgressDialog.show(this, "Downloading Data..", "Please wait", true,false);
        Thread thread = new Thread(this);
        thread.start();
    }
    
    public void run() {
    // add downloading code here
        handler.sendEmptyMessage(0);
     }
    
    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            pDialog().dismiss();
            // handle the result here
        }
    };
    

    It's worth mentioning that you can set the content view of the progress dialog so you can display a custom message / image:)

    pDialog.setContentView(R.layout.X); 
    

提交回复
热议问题