zoukankan      html  css  js  c++  java
  • Android采用pm命令静默卸载应用

    卸载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<AppInfoModel> 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      * 静默卸载App
    31      *
    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             List<PackageInfo> packageInfoList = 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     }
  • 相关阅读:
    day03—JavaScript中DOM的Event事件方法
    day02-Javascript之document.write()方法
    day01-JavaScript中"Uncaught TypeError: Cannot set property 'innerHTML' of null"错误
    Linux安装Tomcat8
    CentOS7安装jdk8及环境变量配置
    Linux命令之lsof
    java如何停止一个运行的线程?
    大数据技术之Hadoop(HDFS)
    大数据技术之Hadoop入门
    用word2013 把word 文档发送到博客园
  • 原文地址:https://www.cnblogs.com/ken9527just/p/11586593.html
Copyright © 2011-2022 走看看