开发了一个APK后,往往后续需要进行更新升级。那么,当用户选择了 更新 之后,新的APK被下载到本地,那么怎么通过代码来进行新apk的安装呢?见下面的代码:
// 新的APK的文件名
String str = "newUpdate.apk";
// 新APK在存储卡上的位置
String fileName = Environment.getExternalStorageDirectory() + str;
// 通过启动一个Intent让系统来帮你安装新的APK
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(new File(fileName)), "application/vnd.android.package-archive");
startActivity(intent);