Android 静默卸载指定包名APP

来源:互联网 发布:移动数据 编辑:程序博客网 时间:2024/05/20 20:20

前提:**设备已经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();            }        }    }
原创粉丝点击