Android 卸载应用的两种方式

1、调用系统的卸载:

Intent intent = new Intent(Intent.ACTION_DELETE, Uri.parse("package:" + pkg_name));
startActivity(intent);

2、调用adb卸载:

runCmd("adb uninstall " + pkg_name);


/**
* 通过adb卸载应用

* @param cmd
* @return
*/
public String runCmd(String cmd) {


try {
System.out.println(cmd);
Runtime runtime = Runtime.getRuntime();
Process process = runtime.exec(cmd);
int result = process.waitFor();
BufferedReader successResult = new BufferedReader(new InputStreamReader(process.getInputStream()));
BufferedReader errorResult = new BufferedReader(new InputStreamReader(process.getErrorStream()));
String s;
StringBuilder successMsg = new StringBuilder();
while ((s = successResult.readLine()) != null) {
successMsg.append(s).append('-');
}
StringBuilder errorMsg = new StringBuilder();
while ((s = errorResult.readLine()) != null) {
errorMsg.append(s);
}
System.out.println(successMsg.toString() + ":" + errorMsg.toString());
return successMsg.toString() + ":" + errorMsg.toString();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}

注意:1、这两种种卸载的弊端就是只能卸载第三方安装的应用,要是卸载系统的需要root权限

             2、adb卸载的方式可以简化下,不需要那么多代码,自己去看吧

            3、最好在卸载的时候使用try{}catch{}判断一下,,,

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值