Android 静默安装/后台安装

Android实现静默安装其实很简单,今天在网上找资料找半天都说的很复杂,什么需要系统安装权限、调用系统隐藏的api、需要系统环境下编译、需要跟systemUI同进程什么的。我不知道他们真的实现了静默安装没有,反正我按照他们的方式统统都失败了。

    下面我来说说我的静默安装实现方式,亲测效果跟豌豆荚一样,并且实现起来非常简单:

    

    1.支持静默安装的机器必须Root,这个不需要我多讲了。

    2.使用pm指令安装即可。

    3.特别注意 PM指令不支持中文,也就说路径中有中文会导致安装失败!


    关键代码如下:


execRootCmdSilent("pm install -r " + Environment.getExternalStorageDirectory().getPath()+"/xxx.apk")


  1. public int execRootCmdSilent(String cmd) {  
  2.        int result = -1;  
  3.        DataOutputStream dos = null;  
  4.   
  5.        try {  
  6.            Process p = Runtime.getRuntime().exec("su");  
  7.            dos = new DataOutputStream(p.getOutputStream());  
  8.   
  9.            Log.i(TAG, cmd);  
  10.            dos.writeBytes(cmd + "\n");  
  11.            dos.flush();  
  12.            dos.writeBytes("exit\n");  
  13.            dos.flush();  
  14.            p.waitFor();  
  15.            result = p.exitValue();  
  16.        } catch (Exception e) {  
  17.            e.printStackTrace();  
  18.        } finally {  
  19.            if (dos != null) {  
  20.                try {  
  21.                    dos.close();  
  22.                } catch (IOException e) {  
  23.                    e.printStackTrace();  
  24.                }  
  25.            }  
  26.        }  
  27.        return result;  
  28.    }  

    不需要在Manifest中声明任何权限
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值