Android采用pm命令静默卸载应用
16lz
2021-01-26
卸载app的方式有多种,可以直接调用android系统的卸载程序,但是这样会调出android卸载提示框,问题就是真的不好看。
所以采用静默卸载的方式,避免弹出系统提示框。
方法一(调用系统卸载程序):
1 //卸载应用2 Uri packageURI = Uri.parse("package:" + pkgName);3 Intent uninstallIntent = new Intent(Intent.ACTION_DELETE, packageURI);4 startActivity(uninstallIntent);
方法二(静默安装):
1 /** 2 * 卸载应用入口提示 3 * 4 * @param position app在列表中的位置 5 * @param mData app列表 6 */ 7 public void startUninstall(int position, List mData) { 8 9 //获取应用程序包名10 String pkg = mData.get(position).packageName;11 //判断此应用程序是否存在12 Boolean pkgExist;13 pkgExist = appExist(mContext,pkg);14 if (!pkgExist) {15 Toast.makeText(mContext, "程序未安装", Toast.LENGTH_SHORT).show();16 } else {17 //静默卸载18 Boolean uninstallSuccess = uninstall(pkg);19 if (uninstallSuccess){20 Toast.makeText(mContext, "卸载成功", Toast.LENGTH_SHORT).show();21 } else {22 Toast.makeText(mContext, "卸载失败", Toast.LENGTH_SHORT).show();23 }24 }25 26 }27 28 29 /**30 * 静默卸载App31 *32 * @param packageName 包名33 * @return 是否卸载成功34 */35 private static boolean uninstall(String packageName) {36 Process process = null;37 BufferedReader successResult = null;38 BufferedReader errorResult = null;39 StringBuilder successMsg = new StringBuilder();40 StringBuilder errorMsg = new StringBuilder();41 try {42 process = new ProcessBuilder("pm", "uninstall", packageName).start();43 successResult = new BufferedReader(new InputStreamReader(process.getInputStream()));44 errorResult = new BufferedReader(new InputStreamReader(process.getErrorStream()));45 String s;46 while ((s = successResult.readLine()) != null) {47 successMsg.append(s);48 }49 while ((s = errorResult.readLine()) != null) {50 errorMsg.append(s);51 }52 } catch (Exception e) {53 KLog.d("e = " + e.toString());54 } finally {55 try {56 if (successResult != null) {57 successResult.close();58 }59 if (errorResult != null) {60 errorResult.close();61 }62 } catch (Exception e) {63 KLog.d("Exception : " + e.toString());64 }65 if (process != null) {66 process.destroy();67 }68 }69 //如果含有"success"单词则认为卸载成功70 return successMsg.toString().equalsIgnoreCase("success");71 }72 73 /**74 * 判断应用是否存在75 *76 * @param context 上下文77 * @param packageName 包名78 * @return 是否存在79 */80 private boolean appExist(Context context, String packageName) {81 try {82 ListpackageInfoList = context.getPackageManager().getInstalledPackages(0);83 for (PackageInfo packageInfo : packageInfoList) {84 if (packageInfo.packageName.equalsIgnoreCase(packageName)) {85 return true;86 }87 }88 } catch (Exception e) {89 KLog.d(e.toString());90 }91 return false;92 }
更多相关文章
- ubuntu kylin 下的 android 编译
- Android仿微信发送语音消息动态提示,支持上滑取消发送
- Android(安卓)4.0 源码 下 载
- android studio ,gradle 导入项目 常见错误 错误提示:Error:(2, 0)
- android studio 自用快捷键方案
- Android(安卓)Studio 我的快捷键
- [Android4.4]电池低电量告警提示
- linux下nessus的安装和使用【原创】
- Android中的三种提示方式