我使用下载管理器api下载了apk文件,我不知道该文件存储在哪里。现在我必须找到apk文件并安装它 给出一些线索。提前致谢
答案 0 :(得分:6)
要安装apk,请使用以下代码:
Intent promptInstall = new Intent(Intent.ACTION_VIEW)
.setData(Uri.parse("file:///path/to/your.apk"))
.setType("application/vnd.android.package-archive");
startActivity(promptInstall);
答案 1 :(得分:3)
要在下载后自动安装文件,您需要在下载后通过广播此消息声明告诉设备执行此操作:
registerReceiver(onComplete, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));
并在处理程序上:
BroadcastReceiver onComplete=new BroadcastReceiver() {
public void onReceive(Context ctxt, Intent intent) {
long id = intent.getExtras().getLong(DownloadManager.EXTRA_DOWNLOAD_ID);
DownloadManager dm =(DownloadManager)getSystemService(DOWNLOAD_SERVICE);
intent = new Intent(Intent.ACTION_VIEW);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.setDataAndType(dm.getUriForDownloadedFile(id),
dm.getMimeTypeForDownloadedFile(id));
startActivity(intent);
}
};
答案 2 :(得分:0)
@Uday,@ Adb El-Rahman的代码有效。初始化下载时,需要设置目标uri。
String sAndroidUrl = "http://somesite.com/Install.apk";
dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
Request request = new Request(Uri.parse(sAndroidUrl));
String sDest = "file://" + android.os.Environment.getExternalStorageDirectory().toString() + "/Download/Install.apk";
request.setDestinationUri(Uri.parse(sDest));
enqueue = dm.enqueue(request);