如何在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
堆栈时出现问题
答案 0 :(得分:0)
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));
答案 1 :(得分:0)
可能此链接对您有用
https://stackoverflow.com/questions/4604239/install-application-programmatically-on-android
同时检查以下影响安装的因素。
文件可能未完全下载。 应用程序可能不适合您的设备配置。 安全问题 - 但您可以通过设置选项启用。 APK文件损坏。