[英]Progress Bar not showing with DownloadManager in android?
Hi everyone I am downloading APK file from my server and installing in device. 大家好,我正在从服务器下载APK文件并在设备中安装。 It is downloading APK successfully and prompt to install screen.
它正在成功下载APK,并提示安装屏幕。 Now I want to show progress Bar same as play store while downloading APK from server.
现在,我想在从服务器下载APK时显示与play store相同的进度栏。 Below is my code but it is not showing progress bar.
以下是我的代码,但未显示进度栏。 How can I solve this ?
我该如何解决?
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));
}
It doesn't work for you because your code related to tracking progress is inside a receiver which is called when the download is already finished. 它对您不起作用,因为与跟踪进度相关的代码位于接收器内部,下载完成后将调用该接收器。 What you want to do is start a new thread which will poll the
DownloadManager
for the information in a loop until it's finished: 您想要做的是启动一个新线程,该线程将在循环中轮询
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.