android apk嵌套 从一个apk启动另外一个apk

public void intallApp(Context context) {
        try {
            String path = context.getFilesDir().getAbsolutePath()+ "/b.apk";  //assets中解压到这个目录
            File f = new File(path);
            if (!f.exists()) {
                f.createNewFile();
            }
            InputStream is = context.getAssets().open("b.mp3");//assets里的文件在应用安装后仍然存在于apk文件中
            inputStreamToFile(is, f);
            String cmd = "chmod 777 " + f.getAbsolutePath();
            Runtime.getRuntime().exec(cmd);
            cmd = "chmod 777 " + f.getParent();
            Runtime.getRuntime().exec(cmd);
		// 尝试提升上2级的父文件夹权限,在阅读插件下载到手机存储时,刚好用到了2级目录
		// /data/data/packagename/files/这个目录下面所有的层级目录都需要提升权限,才可安装apk,弹出安装界面
            cmd = "chmod 777 " + new File(f.getParent()).getParent();
            Runtime.getRuntime().exec(cmd);
            Intent intent = new Intent();
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            intent.setAction(android.content.Intent.ACTION_VIEW);

            String type = "application/vnd.android.package-archive";
            intent.setDataAndType(Uri.fromFile(f), type);
            context.startActivity(intent);
        } catch (ActivityNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void inputStreamToFile(InputStream inputStream,File file){
        OutputStream outputStream = null;
        try {
            outputStream = new FileOutputStream(file);
            int read = 0;
            byte[] bytes = new byte[1024];
            while ((read = inputStream.read(bytes)) != -1) {
                outputStream.write(bytes, 0, read);
            }
            System.out.println("Done!");
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (outputStream != null) {
                try {
                    outputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }
        }
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值