先尝试解析错误。但第二次尝试成功安装在Android
问题描述:
首先,写我的服务器URL 2.下载APK 3.安装APK
当我安装APK
解析错误消息。 但重试安装,成功安装apk。
总之,首先尝试安装apk。尝试后解析错误消息 。成功安装apk。
String url = "https://myserver/.apk ";
File apkFile = new File("/sdcard/Download/openapk.apk");
Uri mUri = Uri.parse(url);
DownloadManager.Request r = new DownloadManager.Request(mUri);
r.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "openapk.apk");
r.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
DownloadManager dm = (DownloadManager)getSystemService(Context.DOWNLOAD_SERVICE);
dm.enqueue(r);
Uri apkUri = Uri.fromFile(apkFile);
Intent webIntent =new Intent(Intent.ACTION_VIEW);
webIntent.setDataAndType(Uri.fromFile(apkFile), "application/vnd.android.package-archive");
webIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(webIntent);
答
下载管理器以异步方式工作,所以你不能只是尝试安装的APK马上对你叫dm.enqueue()
后因为你的代码是不是等候在那里,但正如名字告诉,入列的下载,并将控制返回到您的代码,而下载继续单独工作。
您必须等待下载管理器完成,然后尝试安装您的应用程序。下载管理器广播ACTION_DOWNLOAD_COMPLETE
下载完成时的意图操作,所以你只需要等待它,然后采取行动。
文档:https://developer.android.com/reference/android/app/DownloadManager.html
[解析错误安装的APK结果]的可能的复制(http://*.com/questions/36783585/installing-an-apk-results-in-a-parse-错误) –