温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Android 静默安装实现方法

发布时间:2020-09-23 11:22:52 来源:脚本之家 阅读:192 作者:lqh 栏目:移动开发

Android静默安装的方法,静默安装就是绕过安装程序时的提示窗口,直接在后台安装。

注意:静默安装的前提是设备有ROOT权限。

代码如下:

/** 
 * 静默安装 
 * @param file 
 * @return 
 */ 
public boolean slientInstall(File file) { 
  boolean result = false; 
  Process process = null; 
  OutputStream out = null; 
  try { 
    process = Runtime.getRuntime().exec("su"); 
    out = process.getOutputStream(); 
    DataOutputStream dataOutputStream = new DataOutputStream(out); 
    dataOutputStream.writeBytes("chmod 777 " + file.getPath() + "\n"); 
    dataOutputStream.writeBytes("LD_LIBRARY_PATH=/vendor/lib:/system/lib pm install -r " + 
        file.getPath()); 
    // 提交命令 
    dataOutputStream.flush(); 
    // 关闭流操作 
    dataOutputStream.close(); 
    out.close(); 
    int value = process.waitFor(); 
     
    // 代表成功 
    if (value == 0) { 
      result = true; 
    } else if (value == 1) { // 失败 
      result = false; 
    } else { // 未知情况 
      result = false; 
    } 
  } catch (IOException e) { 
    e.printStackTrace(); 
  } catch (InterruptedException e) { 
    e.printStackTrace(); 
  } 
   
  return result; 
} 

注意代码中pm install -r 前面的LD_LIBRARY_PATH=/vendor/lib:/system/lib,在4.0版本以上必须加上,不然会无法安装。

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI