android 静默安装apk

我说的这种方法  对开发程序环境要求比较苛刻,需要运行程序的系统必须已经root过的才可以用此方法:

代码很简单就几句话:

 public boolean silentInstall(String apkPath)
	    {
	        //第一步,改变apk的权限
	        String cmd1 = "chmod 777 " + apkPath + " \n";
	       
	        //第二步,注意,在android系统下映射pm的路径,这一条很重要,在android4.0以上需要添加系统环境变量,4.0以下就不需要
	    //如果是4.0以下,只需要运行pm install -r *.apk 即可
	        String cmd2 = "LD_LIBRARY_PATH=/vendor/lib:/system/lib pm install -r "
	                + apkPath + " \n";
	        return execWithSID(cmd1, cmd2);
	    }

	    private boolean execWithSID(String... args)
	    {
	        boolean isSuccess = true;
	        Process process = null;
	        OutputStream out = null;
	        try
	        {
	            process = Runtime.getRuntime().exec("su");
	            out = process.getOutputStream();
	            DataOutputStream dataOutputStream = new DataOutputStream(out);

	            for (String tmp : args)
	            {
	                dataOutputStream.writeBytes(tmp);
	            }

	            dataOutputStream.flush(); // 提交命令
	            dataOutputStream.close(); // 关闭流操作
	            out.close();

	         //   isSuccess = waitForProcess(process);
	        } 
	        catch (IOException e)
	        {
	            e.printStackTrace();
	            isSuccess = false;
	        }

	        return isSuccess;
	    }
经测试,可以使用

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值