在我的一个用例中,应用程序在开始时联系服务器并下载同一应用程序的更新.
下载我正在使用Android的DownloadManager类.然后使用下面的代码,它尝试创建应用程序安装程序意图,onReceive()即成功下载APK后.
@Override
public void onReceive(Context context, Intent intent) {
//check if the broadcast message is for our Enqueued download
long referenceId = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, -1);
if (downloadReference == referenceId) {
try {
Log.v("", "Downloading of the new app version complete");
String filepath = Environment.getExternalStorageDirectory() + "/" +
Environment.DIRECTORY_DOWNLOADS + "/" + mLatestVersionFileName;
//start the installation of the latest version
Uri uri = downloadManager.getUriForDownloadedFile(downloadReference);
//downloadManager.getUriForDownloadedFile(downloadReference)
Uri fileLoc = Uri.fromFile(new File(filepath));
Intent promptInstall = new Intent(Intent.ACTION_VIEW);
promptInstall.setDataAndType(uri, "application/vnd.android.package-archive");
promptInstall.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(promptInstall); …Run Code Online (Sandbox Code Playgroud)