[英]Progress Bar not showing with DownloadManager in android?
大家好,我正在從服務器下載APK文件並在設備中安裝。 它正在成功下載APK,並提示安裝屏幕。 現在,我想在從服務器下載APK時顯示與play store相同的進度欄。 以下是我的代碼,但未顯示進度欄。 我該如何解決?
private void downloadAndInstall() {
DownloadManager.Request request = new DownloadManager.Request(
Uri.parse(app_url));
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "zero_defect.apk");
enqueue = dm.enqueue(request);
receiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (DownloadManager.ACTION_DOWNLOAD_COMPLETE.equals(action)) {
Toast.makeText(getApplicationContext(), "Download Completed", Toast.LENGTH_LONG).show();
long downloadId = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, 0);
DownloadManager.Query query = new DownloadManager.Query();
query.setFilterById(enqueue);
Cursor c = dm.query(query);
if (c.moveToFirst()) {
int columnIndex = c.getColumnIndex(DownloadManager.COLUMN_STATUS);
int bytes_downloaded = c.getInt(c
.getColumnIndex(DownloadManager.COLUMN_BYTES_DOWNLOADED_SO_FAR));
int bytes_total = c.getInt(c.getColumnIndex(DownloadManager.COLUMN_TOTAL_SIZE_BYTES));
final int dl_progress = (int) ((bytes_downloaded * 100l) / bytes_total);
runOnUiThread(new Runnable() {
@Override
public void run() {
downloadProgress.setProgress((int) dl_progress);
}
});
if (DownloadManager.STATUS_SUCCESSFUL == c.getInt(columnIndex)) {
String uriString = c.getString(c.getColumnIndex(DownloadManager.COLUMN_LOCAL_URI));
Log.d("ainfo", uriString);
if (downloadId == c.getInt(0)) {
Log.d("DOWNLOAD PATH:", c.getString(c.getColumnIndex("local_uri")));
Log.d("isRooted:", String.valueOf(isRooted()));
if (isRooted() == false) {
File directory = Environment.getExternalStoragePublicDirectory("Download");
File file = new File(directory, "zero_defect.apk"); // assume refers to "sdcard/myapp_folder/myapp.apk"
Uri fileUri = Uri.fromFile(file); //for Build.VERSION.SDK_INT <= 24
if (Build.VERSION.SDK_INT >= 24) {
fileUri = FileProvider.getUriForFile(context, BuildConfig.APPLICATION_ID + ".provider", file);
}
Intent intent_install = new Intent(Intent.ACTION_VIEW, fileUri);
intent_install.putExtra(Intent.EXTRA_NOT_UNKNOWN_SOURCE, true);
intent_install.setDataAndType(fileUri, "application/vnd.android.package-archive");
intent_install.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent_install.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); //dont forget add this line
startActivity(intent_install);
Toast.makeText(getApplicationContext(), "App Installing", Toast.LENGTH_LONG).show();
} else {
//if your device is rooted then you can install or update app in background directly
Toast.makeText(getApplicationContext(), "App Installing...Please Wait", Toast.LENGTH_LONG).show();
File file = new File("/mnt/sdcard/Download/zero_defect.apk");
Log.d("IN INSTALLER:", "/mnt/sdcard/Download/zero_defect.apk");
if (file.exists()) {
try {
String command;
Log.d("IN File exists:", "/mnt/sdcard/Download/zero_defect.apk");
command = "pm install -r " + "/mnt/sdcard/Download/zero_defect.apk";
Log.d("COMMAND:", command);
Process proc = Runtime.getRuntime().exec(new String[]{"su", "-c", command});
proc.waitFor();
Toast.makeText(getApplicationContext(), "App Installed Successfully", Toast.LENGTH_LONG).show();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
}
}
c.close();
}
}
};
registerReceiver(receiver, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));
}
它對您不起作用,因為與跟蹤進度相關的代碼位於接收器內部,下載完成后將調用該接收器。 您想要做的是啟動一個新線程,該線程將在循環中輪詢DownloadManager
以獲取信息,直到完成:
final long downloadId = dm.enqueue(request);
new Thread(new Runnable() {
@Override
public void run() {
while (true) {
DownloadManager.Query q = new DownloadManager.Query();
q.setFilterById(downloadId);
Cursor cursor = manager.query(q);
cursor.moveToFirst();
int bytesDownloaded = cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_BYTES_DOWNLOADED_SO_FAR));
int bytesTotal = cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_TOTAL_SIZE_BYTES));
if (cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_STATUS)) == DownloadManager.STATUS_SUCCESSFUL) {
break;
}
final int progress = (int) ((bytesDownloaded * 100l) / bytesTotal);
runOnUiThread(new Runnable() {
@Override
public void run() {
progressBar.setProgress(progress);
}
});
cursor.close();
}
}
}).start();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.