进度百分比栏未在java中运行



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

我是一个相当新的android开发,所以我试图做的是应用程序,可以显示pdf从url,

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

我正在使用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();
}
}
}

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

注意:对于API级别30,不赞成使用AsyncTask。

最新更新