android8.0以下静默安装自启动

引入第三方库下载安装包

    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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值