Android 静默卸载指定包名APP

前提:**设备已经root,未root设备,无法实现静默卸载
方式很多,我采用”uninstall + 应用程序包名 “方式卸载,以下是核心代码**。
代码下载:
http://download.csdn.net/detail/aiwusheng/7582049
升级版代码下载:
http://download.csdn.net/detail/aiwusheng/7586057

/**
     * 静默卸载apk到Data/app目录
     * 
     * @param packageName
     * @return 卸载成功为true
     */
    public static boolean uninstallDataAPPBySilent(String packageName) {
        Log.d(TAG, "-------uninstallDataAPPBySilent------");

        // 参数检测
        if (TextUtils.isEmpty(packageName)) {
            return false;
        }
        Log.d(TAG, "packageName: " + packageName);

        StringBuilder cmd = new StringBuilder();
        cmd.append("pm uninstall " + packageName).append("\n");
        // 部分手机Root之后Library path 丢失,导入library path可解决该问题
        // cmd.append("export LD_LIBRARY_PATH=/vendor/lib:/system/lib");
        if (execRootCmd(cmd.toString()) == 0) {
            Log.e(TAG, "uninstall: " + packageName + "success");
            return true;
        }
        Log.e(TAG, "uninstall: " + packageName + " failed");

        return false;
    }

    /**
     * root权限下执行命令
     * 
     * @param cmd
     *            多条命令需用换行分隔
     * @return 执行结果码 0代表成功
     */
    private static int execRootCmd(String cmd) {
        Log.d(TAG, "execRootCmd: " + cmd);

        Process process = null;
        DataOutputStream dos = null;
        try {
            process = Runtime.getRuntime().exec("su");
            dos = new DataOutputStream(process.getOutputStream());
            dos.writeBytes(cmd + "\n");
            dos.flush();
            dos.writeBytes("exit\n");
            dos.flush();
            process.waitFor();
            Log.e(TAG, "process.exitValue(): " + process.exitValue());

            return process.exitValue();
        } catch (Exception e) {
            Log.e(TAG, "exception: " + e.getMessage());
            return -1;
        } finally {
            try {
                if (dos != null) {
                    dos.close();
                }
                process.destroy();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

bjxiaxueliang

您的鼓励是我创作的最大动力!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值