簡體   English   中英

ProgressDialog:顯示后不要關閉

[英]ProgressDialog : Don't Dismissing after showing

    private void copyFile(AppDB apkfile) {
    ProgressDialog pd=new ProgressDialog(this,ProgressDialog.STYLE_SPINNER);
    pd.show(this,"Coping ...",apkfile.name,true,true);
    File f1 = new File(apkfile.location);
    try {
        String fileName = apkfile.name;
        File f2 = new File(Environment.getExternalStorageDirectory().toString() + "/" + "Easy Share");
        f2.mkdirs();
        f2 = new File(f2.getPath() + "/" + fileName + ".apk");
        f2.createNewFile();
        InputStream in = new FileInputStream(f1);
        OutputStream out = new FileOutputStream(f2);
        byte[] buf = new byte[1024];
        int len;
        while ((len = in.read(buf)) > 0) {
            out.write(buf, 0, len);
        }
        in.close();
        out.close();
    } catch (FileNotFoundException ex) {
        Toast.makeText(this, ex.getMessage() + " in the specified directory.", Toast.LENGTH_SHORT);

    } catch (IOException e) {
        Toast.makeText(this, e.getMessage(), Toast.LENGTH_SHORT);
    }
    if(pd.isShowing()) {
   pd.dismiss();}

}

大家好,當您嘗試關閉進度對話框時,它不會這樣做,並尋求許多解決方案,但我不是同一個問題。 請幫忙。

更新:運行后有進度對話框pd的類。

屏幕截圖1

finally嘗試

private void copyFile(AppDB apkfile) {
    ProgressDialog pd=new ProgressDialog(this,ProgressDialog.STYLE_SPINNER);
    pd.show(this,"Coping ...",apkfile.name,true,true);
    File f1 = new File(apkfile.location);
    try {
        String fileName = apkfile.name;
        File f2 = new File(Environment.getExternalStorageDirectory().toString() + "/" + "Easy Share");
        f2.mkdirs();
        f2 = new File(f2.getPath() + "/" + fileName + ".apk");
        f2.createNewFile();
        InputStream in = new FileInputStream(f1);
        OutputStream out = new FileOutputStream(f2);
        byte[] buf = new byte[1024];
        int len;
        while ((len = in.read(buf)) > 0) {
            out.write(buf, 0, len);
        }
        in.close();
        out.close();
    } catch (FileNotFoundException ex) {
        Toast.makeText(this, ex.getMessage() + " in the specified directory.", Toast.LENGTH_SHORT);

    } catch (IOException e) {
        Toast.makeText(this, e.getMessage(), Toast.LENGTH_SHORT);
    } finally {
      if(pd.isShowing()) {
        pd.dismiss();
      }
    }
}

復制應在線程或AsyncTask中完成。 在執行AsyncTask之前顯示對話框,並隱藏在onPOstExecute中

protected void onPostExecute(Long result) {
       if(mProgressDialog.isShowing()) {
          mProgressDialog.dismiss();}
       }  
}

運行良好,我將告訴您兩種可能性

  1. 請最后在try塊中使用它

     try { String fileName = apkfile.name; File f2 = new File(Environment.getExternalStorageDirectory().toString() + "/" + "Easy Share"); f2.mkdirs(); f2 = new File(f2.getPath() + "/" + fileName + ".apk"); f2.createNewFile(); InputStream in = new FileInputStream(f1); OutputStream out = new FileOutputStream(f2); byte[] buf = new byte[1024]; int len; while ((len = in.read(buf)) > 0) { out.write(buf, 0, len); } in.close(); out.close(); if(pd.isShowing()) { pd.dismiss();} } catch (FileNotFoundException ex) { Toast.makeText(this, ex.getMessage() + " in the specified directory.", Toast.LENGTH_SHORT); } catch (IOException e) { Toast.makeText(this, e.getMessage(), Toast.LENGTH_SHORT); } 

    }

第二是延遲使用

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM