ProgressDialog 的應用

      試想下 當你用android手機下一個應用程序 然後你的手機屏幕就好像死機一樣的,什麼反應都沒得,那個用戶體驗就差到極點了,

那麼android就給我們提供了一個選擇,它大概有幾種場景會被用到:

 

1、只是單純的等待即可,圓形的等待條 就可以解決

2、還有一種就是可以看出進度

3、也可以用自己的樣式,比如:加一個或兩個按鈕,寫些事件

4、如果用於下載 可以做顯示文件下載進度的功能

 

——————————————————————————————

 

1、單純的等待

    ProgressDialog progressDialog = ProgressDialog.show

     (

        ProgressBarDemo.this,

        "Loading...",

        "Please wait...",

        true,

        false

    );

 

2、可以看出進度

  ProgressDialog pb;

 

  int counter  = 0;

 

  pb = new ProgressDialog(this);

 

  // 設置已有進度

  pb.setProgress(0);

  

  // 設置進度的最大值

  pb.setMax(100);

 

 //關閉ProgressDialog

 pb.dismiss();

 

 //設置ProgressDialog進度條是否不明確

 pd.setIndeterminate(false);

 

 // 設置下載進度條的樣式

 pb.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);

 

// 設置ProgressDialog是否可以按退回鍵取消
pd.setCancelable(true);

 

pb.show();

 

//進度的顯現

new Thread() {
            public void run() {
                    try {
                            while (counter <= 100) {
                                    // 由線程來控制進度
                                    pd.setProgress(counter++);

                                    Thread.sleep(1000);
                            }
                            pd.cancel();
                    } catch (InterruptedException e) {
                            pd.cancel();
                    }
            }
    }.start();

 

3、加按鈕

  ProgressDialog pb;

 

  pb = new ProgressDialog(this);

 

  pb.setButton("確定"new Bt1DialogListener());  

 

  class Bt1DialogListener implements DialogInterface.OnClickListener {  
           @Override  
           public void onClick(DialogInterface dialog, int which) {  
           // 點擊“確定”按鈕取消對話框  
                   pb.cancel();  
                  }  
            }  

 

 4、顯現下載的進度

   和第二點大同小異 主要賦的值是用文件的大小。

   int downLoadFileSize = 0;

   URL myURL = new URL(url);
   URLConnection conn = myURL.openConnection();
   conn.connect();
   InputStream is = conn.getInputStream();

  

   FileOutputStream fos = new FileOutputStream(path + filename);

 

   // 把數據存入路徑+文件名
        byte buf[] = new byte[1024];
        downLoadFileSize = 0;
        sendMsg(0);
        do
        {
            // 循環讀取
            int numread = is.read(buf);
            if (numread == -1)
            {
                break;
            }
            fos.write(buf, 0, numread);
            downLoadFileSize += numread;

            // 更新進度條
            sendMsg(1);
        }
        while (true);

        // 通知下載完成
        sendMsg(2);
        try
        {
            is.close();
        }
        catch (Exception ex)
        {
            Log.e("tag", "error: " + ex.getMessage(), ex);
        }

 

 

 private void sendMsg(int flag)
 {
      Message msg = new Message();
      msg.what = flag;
      downloadHandler.sendMessage(msg);
  }

 

 /**
     * 一個下載文件的handler
     */
    private Handler downloadHandler = new Handler()
    {
        @Override
        public void handleMessage(Message msg)
        {

            // 定義一個Handler,用於處理下載線程與UI間通訊
            if (!Thread.currentThread().isInterrupted())
            {
                pb.show();
                switch (msg.what)
                {
                    case 0:
                        // 設置數字顯示爲文件的大小
                        pb.setMax(fileSize);

                    case 1:
                        // 設置顯示已下載的文件大小
                        pb.setProgress(downLoadFileSize);
                        break;
                    case 2:
                        // 退出,用戶選擇升級
                        // android.os.Process.killProcess(android.os.Process.myPid());
                        String str = "/sdcard/ChinaFaceSNS.apk";
                        File file = new File(str);
                        if (file.isFile())
                        {
                            Intent intent = new Intent(Intent.ACTION_VIEW);
                            intent.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive");

                            // 跳轉到程序安裝界面
                            startActivityForResult(intent, CANCEL_REQUEST_CODE);
                            pb.dismiss();
                            Toast.makeText(LoadingActivity.this, "文件下載完成", 1).show();
                        }
                        else
                        {
                            Toast.makeText(LoadingActivity.this, "下載錯誤", 1).show();
                        }

                        break;

                    case -1:
                        String error = msg.getData().getString("error");
                        Toast.makeText(LoadingActivity.this, error, 1).show();
                        break;
                }
            }
            super.handleMessage(msg);
        }
    };

 

這邊只是部分代碼 只供參考

 

還有這個CSDN的“插入代碼”功能怎麼使的啊 我鬱悶啊

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章