VirtualApp中静默安装App

最近学习看到android沙箱VirtualApp,小编资历尚浅,只是从VA运用的角度记录了下来如何在VA中静默安装app

VirtualApp github地址https://github.com/asLody/VirtualApp

1、将需要安装的app包放置在assets目录下

2、构建app安装需要的方法如下

    private void installApp(String path) {
        if (checkRequiredPermission()) {
            try {
                File localFile = extractApp(path);
                //com.app.test是测试安装的app包名
                AppInfoLite localAppInfoLite = new AppInfoLite("com.app.test", localFile.getAbsolutePath(), true);
                mPresenter.addApp(localAppInfoLite);
            } catch (Exception localException) {
                Toast.makeText(this, "安装失败", Toast.LENGTH_SHORT).show();
            }
        } else {
            ActivityCompat.requestPermissions(this, new String[]{"android.permission.WRITE_EXTERNAL_STORAGE"}, 200);
        }
    }
    private File extractApp(String path) {
        try {
            File localFile = new File(path);
            if (localFile.exists()) {
                localFile.delete();
            }
            //提取的app文件,testApp是我测试的app名称
            FileUtils.writeToFile(getAssets().open("testApp.apk"), localFile);
            return localFile;
        } catch (IOException localIOException) {
        }
        return null;
    }
    private boolean checkRequiredPermission() {
        return (Build.VERSION.SDK_INT < 23) || (ContextCompat.checkSelfPermission(this, "android.permission.WRITE_EXTERNAL_STORAGE") == 0);
    }

 3、在需要安装App代码的地方调用installApp方法即可

String apkPath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/testApp.apk";
            //判断app是否已经安装
            if (!VirtualCore.get().isAppInstalled("com.app.test")) {
                installApp(apkPath);
            }else{
                Toast.makeText(this, "当前APP已经安装,请勿重复安装", Toast.LENGTH_SHORT).show();
            }

 

你可能感兴趣的:(Android)