【问题标题】:Percentage Progress bar not running in java百分比进度条未在 java 中运行
【发布时间】:2020-10-28 08:48:45
【问题描述】:

我使用此代码进度条显示,但百分比条未运行

我对 android 开发相当陌生,所以我想做的是可以从 url 显示 pdf 的应用程序,

我使用此代码进度条显示,但百分比条未运行

我正在使用com.github.barteksc.pdfviewer.PDFView 显示pdf

这是我的 pdf 节目活动

public class test2 extends AppCompatActivity  {

PDFView pdfView; //pdfView object
String URL;
String fileName;
File directory; //path of created File

// Container for all parameters of DownloadAsync
private static class AsyncParameters {
    String URL;
    File directory;
    AsyncParameters(String URL, File directory) {
        this.URL = URL;
        this.directory = directory;
    }
}
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Intent intent = getIntent(); //whatever calls this activity, gather the intent
    URL = intent.getStringExtra("File URL"); // in this case, get the file name of the "extra" passed through
    fileName = intent.getStringExtra("File Name");

    setContentView(R.layout.activity_test2);

    File intDirectory = getFilesDir();
    File folder = new File(intDirectory, "pdf");
    boolean isDirectoryCreated = folder.exists();
    //setDownloadButtonListener();
    if (!isDirectoryCreated) {
        isDirectoryCreated= folder.mkdir();
    }
    if(isDirectoryCreated) {
        directory = new File(folder, fileName);
        try {
            directory.createNewFile();
        } catch (IOException e1) {
            e1.printStackTrace();
        }

        //See if file already exists (reduces wait time)
        boolean empty = directory.length() == 0;
        if (empty) {
            /**Call class to create parameter container **/
            AsyncParameters param = new AsyncParameters(URL, directory);
            DownloadAsync Downloader = new DownloadAsync();
            Downloader.execute(param);
        }
        showPdf();
    }


}
public void showPdf()
{
    pdfView = (PDFView) findViewById(R.id.pdfViewPager);
    pdfView.fromFile(directory).load();
}

public class DownloadAsync extends AsyncTask<AsyncParameters, Void, Void> {

    // Container for all parameters of DownloadAsync
    ProgressDialog pDialog;

    @Override
    protected void onPreExecute() {
        //Create a progress bar that details the program downloading
        super.onPreExecute();
        pDialog = new ProgressDialog(test2.this);
        pDialog.setMessage("Downloading ");
        String message= "please wait don't push back";

        SpannableString ss2 =  new SpannableString(message);
        ss2.setSpan(new RelativeSizeSpan(1f), 0, ss2.length(), 0);
        ss2.setSpan(new ForegroundColorSpan(Color.BLACK), 0, ss2.length(), 0);

        pDialog.setMessage(ss2);
        pDialog.setCancelable(false);
        pDialog.setIndeterminate(false);
        pDialog.setMax(100);
        pDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        pDialog.show();
    }

    @Override
    protected Void doInBackground(AsyncParameters... params) {
        int count;
        String fileURL = params[0].URL;
        File directory = params[0].directory;
        try {
            FileOutputStream f = new FileOutputStream(directory);
            java.net.URL u = new URL(fileURL);
            HttpURLConnection c = (HttpURLConnection) u.openConnection();
            c.connect();
            InputStream in = c.getInputStream();
            int length=c.getContentLength();

            byte[] data;
            data = new byte[1024];

            long total = 0;

            while ((count = in.read(data)) != -1) {
                total += count;
                f.write(data, 0, count);
            }

            f.flush();
            in.close();
            in.close();

        } catch (Exception e) {
            e.printStackTrace();
            pDialog.setMessage(new SpannableString("ERROR DOWNLOADING"));
        }
        onPostExecute();
        return null;
    }

    private void onPostExecute() {
        pDialog.dismiss();
        showPdf();
    }
}
}

   

【问题讨论】:

    标签: android


    【解决方案1】:

    我没有看到您在后台调用 publishProgress 来调用 OnProgressUpdate。我没有看到您在 onProgressUpdate 中设置百分比。我没有看到您在任何地方都过度使用了 onProgressUpdate。 OnPostExecute() 会在后台执行完成时自动调用。您不需要在 doInBackGround 中显式调用,也不应显式调用它。

    注意:API 级别 30 不推荐使用 AsyncTask。

    【讨论】:

      猜你喜欢
      • 2012-12-23
      • 1970-01-01
      • 1970-01-01
      • 2015-06-03
      • 2010-11-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多