[英]how to download apk and install on android 6
如何在Android編程中下載並安裝apk文件。 我們在Android 6及更高版本時遇到問題。
dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(file_url));
enqueue = dm.enqueue(request);
receiver3 = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (DownloadManager.ACTION_DOWNLOAD_COMPLETE.equals(action)) {
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);
if (DownloadManager.STATUS_SUCCESSFUL == c
.getInt(columnIndex)) {
String uriString = c.getString(c.getColumnIndex(DownloadManager.COLUMN_TITLE));
String uriString2 = c.getString(c.getColumnIndex(DownloadManager.COLUMN_URI));
Intent intent2 = new Intent(Intent.ACTION_VIEW);
intent2.setDataAndType(Uri.fromFile(new File(uriString2)), "application/vnd.android.package-archive");
intent2.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent2);
}
}
}
}
};
StartActivity.this.registerReceiver(receiver3, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));
當下載完成並安裝error:parse
錯誤解析package
堆棧時出現問題
dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE); DownloadManager.Request request = new DownloadManager.Request(Uri.parse(file_url)); enqueue = dm.enqueue(request); receiver3 = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { String action = intent.getAction(); if (DownloadManager.ACTION_DOWNLOAD_COMPLETE.equals(action)) { 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); if (DownloadManager.STATUS_SUCCESSFUL == c .getInt(columnIndex)) { String uriString = c.getString(c.getColumnIndex(DownloadManager.COLUMN_TITLE)); String uriString2 = c.getString(c.getColumnIndex(DownloadManager.COLUMN_URI)); File file = new File(dir, "App.apk"); Intent intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive"); startActivity(intent); } } } } }; StartActivity.this.registerReceiver(receiver3, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));
可能這個鏈接對你有用
https://stackoverflow.com/questions/4604239/install-application-programmatically-on-android
同時檢查以下影響安裝的因素。
文件可能未完全下載。 應用程序可能不適合您的設備配置。 安全問題 - 但您可以通過設置選項啟用。 APK文件損壞。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.