Android静默安装实现

Android静默安装的方案有很多种,如利用ProcessBuilder或者Runtime.getRuntime().exec()运行pm isntall命令,但此方法需要先su,需要root权限,或者反射获取PackageManager.installPackage(),使用起来也比较繁琐,同时需要获取系统权限,在API21以上可以是用PackageInstaller.Secssion实现静默安装,该接口也需要获取系统权限android.permission.INSTALL_PACKAGES,不多说,直接上代码。

 public void install(@NonNull Context context,@NonNull String path) throws Exception {
        File file=new File(path);
        String apkName=path.substring(path.lastIndexOf(File.separator)+1,path.lastIndexOf(".apk"));
		//获取PackageInstaller
		PackageInstaller packageInstaller = context.getPackageManager()
                .getPackageInstaller();
        PackageInstaller.SessionParams params=new PackageInstaller
                .SessionParams(PackageInstaller.SessionParams.MODE_FULL_INSTALL);
        PackageInstaller.Session session=null;
        OutputStream outputStream=null;
        FileInputStream inputStream=null;
        try {
			//创建Session
            int sessionId = packageInstaller.createSession(params);
			//开启Session
            session=packageInstaller.openSession(sessionId);
			//获取输出流,用于将apk写入session
            outputStream = session.openWrite(apkName, 0, -1);
            inputStream=new FileInputStream(file);
            byte[] buffer=new byte[4096];
            int n;
			//读取apk文件写入session
            while ((n=inputStream.read(buffer))>0){
                outputStream.write(buffer,0,n);
            }
			//写完需要关闭流,否则会抛异常“files still open”
            inputStream.close();
            inputStream=null;
            outputStream.flush();
            outputStream.close();
            outputStream=null;
			//配置安装完成后发起的intent,通常是打开activity
            Intent intent=new Intent();
            PendingIntent pendingIntent=PendingIntent.getActivity(mContext,0,intent,0);
            IntentSender intentSender = pendingIntent.getIntentSender();
			//提交启动安装
            session.commit(intentSender);

        } catch (IOException e) {
            throw new RuntimeException("Couldn't install package", e);
        } catch (RuntimeException e) {
            if (session != null) {
                session.abandon();
            }
            throw e;
        }finally {
            closeStream(inputStream);
            closeStream(outputStream);
        }

    }

注释已经解释得很清楚,就不在多讲解,如需获取安装过程的状态,可以在createSession前给PackageInataller注册SessionCallback获取

packageInstaller.registerSessionCallback(new PackageInstaller.SessionCallback() {
            @Override
            public void onCreated(int sessionId) {
                
            }

            @Override
            public void onBadgingChanged(int sessionId) {

            }

            @Override
            public void onActiveChanged(int sessionId, boolean active) {

            }

            @Override
            public void onProgressChanged(int sessionId, float progress) {

            }

            @Override
            public void onFinished(int sessionId, boolean success) {

            }
        });

 

你可能感兴趣的:(Android)