引入第三方库下载安装包
implementation 'com.ixuea:android-downloader:3.0.0'
下载安装包
private static boolean isdownloadingPack = false; //是否正在下载安装包
if (isdownloadingPack) {
return;
}
File file = new File(Constants.APK_PATH);
//如果文件已存在就先删除
if (file.exists()) {
file.delete();
}
DownloadManager downloadManager = DownloadService.getDownloadManager(getContext().getApplicationContext());
DownloadInfo downloadInfo = new DownloadInfo.Builder().setUrl(安装包网络地址)
.setPath(Constants.APK_PATH)
.build();
downloadInfo.setDownloadListener(new DownloadListener() {
@Override
public void onStart() {
LogUtils.d("开始下载安装包");
isdownloadingPack = true;
}
@Override
public void onWaited() {
}
@Override
public void onPaused() {
}
@Override
public void onDownloading(long progress, long size) {
long l = progress * 100 / size;
LogUtils.d("正在下载安装包:" + l + "% - 总大小: " + size / 1024 / 1024 + "MB");
}
@Override
public void onRemoved() {
}
@Override
public void onDownloadSuccess() {
LogUtils.d("下载成功");
DeviceUtils.getInstance().installPackage(Constants.APK_PATH, true);
LogUtils.d("安装成功");
isdownloadingPack = false;
}
@Override
public void onDownloadFailed(DownloadException e) {
LogUtils.d("下载失败" + e);
isdownloadingPack = false;
}
});
downloadManager.download(downloadInfo);
manifest文件注册静态广播
<uses-permission android:name= "android.permission.RESTART_PACKAGES" />
<uses-permission android:name="android.permission.INSTALL_PACKAGES"
tools:ignore="ProtectedPermissions" />
<receiver android:name=".receiver.UpdateRestartReceiver">
<intent-filter>
<action android:name="android.intent.action.PACKAGE_ADDED" />
<action android:name="android.intent.action.PACKAGE_REPLACED" />
<action android:name="android.intent.action.PACKAGE_REMOVED" />
<data android:scheme="package" />
</intent-filter>
</receiver>
UpdateRestartReceiver.java
public class UpdateRestartReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
String localPkgName = context.getPackageName();
//取得MyReceiver所在的App的包名
Uri data = intent.getData();
String installedPkgName = data.getSchemeSpecificPart();
//取得安装的Apk的包名,只在该app覆盖安装后自启动
if ((action.equals(Intent.ACTION_PACKAGE_ADDED) || action.equals(Intent.ACTION_PACKAGE_REPLACED)) && installedPkgName.equals(localPkgName)) {
/**
* 启动activity
*/
Intent mIntent = new Intent( );
mIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
ComponentName comp = new ComponentName("com.xxx.xxx", "com.xxx.xxx.ui.common.activity");
mIntent.setComponent(comp);
mIntent.setAction("android.intent.action.VIEW");
context.startActivity(mIntent);
}
}
}
安装方法
public String installSilently(String path) {
Log.d("lozie", "path" + path);
// 通过命令行来安装APK
String[] args = {"pm", "install", "-r", path};
String result = "";
// 创建一个操作系统进程并执行命令行操作
ProcessBuilder processBuilder = new ProcessBuilder(args);
Process process = null;
InputStream errIs = null;
InputStream inIs = null;
try {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
int read = -1;
process = processBuilder.start();
errIs = process.getErrorStream();
while ((read = errIs.read()) != -1) {
baos.write(read);
}
baos.write('\n');
inIs = process.getInputStream();
while ((read = inIs.read()) != -1) {
baos.write(read);
}
byte[] data = baos.toByteArray();
result = new String(data);
Log.d("lozie", "result" + result);
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (errIs != null) {
errIs.close();
}
if (inIs != null) {
inIs.close();
}
} catch (IOException e) {
e.printStackTrace();
}
if (process != null) {
process.destroy();
}
}
return result;
}
参考文档
https://blog.csdn.net/weixin_42602900/article/details/119674889