android静默安装与静默卸载

本文详细介绍了如何在Android 5.0和6.0系统上实现应用的静默安装与卸载。针对5.0版本,通过shell命令直接操作;而对于6.0版本,静默安装仍使用shell命令,但静默卸载需借助反射实现。同时,文中提到了添加系统权限和设置sharedUserId以确保静默操作的成功。
摘要由CSDN通过智能技术生成

android5.0和6.0静默安装与卸载


android5.0使用shell命令实现静默安装和卸载

废话不多说直接上代码

//静默安装
 private void installSpecific(String path) {
  //apk存储路径
        ShellUtils.execCommand("chmod 777 " + path, false);
        ShellUtils.CommandResult res = ShellUtils.execCommand("pm install -r " + pkg.getFilePath(), false);//静默安装的shell命令
        ShellUtils.CommandResult result = ShellUtils.execCommand("pm uninstall " + "包名", false);//静默卸载的shell命令
        if (res.result == 0 && !TextUtils.isEmpty(res.successMsg) && res.successMsg.equals("Success")) {
           //安装成功
        } else {
           //安装失败 
        }
    }

ShellUtils工具类代码

public class ShellUtils {
   

    public static final String COMMAND_SU = "can";
    public static final String COMMAND_SH = "sh";
    public static final String COMMAND_EXIT = "exit\n";
    public static final String COMMAND_LINE_END = "\n";

    private ShellUtils() {
        throw new AssertionError();
    }

    /**
     * check whether has root permission
     * 
     * @return
     */
    public static boolean checkRootPermission() {
        return execCommand("echo root", true, false).result == 0;
    }

    /**
     * execute shell command, default return result msg
     * 
     * @param command
     *            command
     * @param isRoot
     *            whether need to run with root
     * @return
     * @see ShellUtils#execCommand(String[], boolean, boolean)
     */
    public static
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值