美文网首页安卓收藏
静默安装 And 静默卸载

静默安装 And 静默卸载

作者: Exception_Cui | 来源:发表于2018-07-05 14:45 被阅读12次

一、准备工作

要实现静默安装、卸载,首先你要有root权限,能把你的静默安装、卸载程序移动到system/app目录下。

1、用RE浏览器将你的应用(一般在/data/app目录下)移动到/system/app目录下,如果你的程序有.so文件,那么请将相应的.so文件从/data/data/程序包名/lib目录下移动到/system/lib目录下

2、重启你的手机,你就会发现你的应用已经是系统级应用了,不能被卸载,也就是说你的应用现在已经八门全开,活力无限了。

二 .权限

<!-- 静默安装所需权限,如与Manifest报错,请运行Project->clean -->
   <!-- 允许程序安装应用 -->
   <uses-permission android:name="android.permission.INSTALL_PACKAGES" />
   <!-- 允许程序删除应用 -->
   <uses-permission android:name="android.permission.DELETE_PACKAGES" />
   <!-- 允许应用清除应用缓存 -->
   <uses-permission android:name="android.permission.CLEAR_APP_CACHE" />
   <!-- 允许应用清除应用的用户数据 -->
   <uses-permission android:name="android.permission.CLEAR_APP_USER_DATA" />

三 :管理类

public class ApkManager {
 15 
 16     private static final String TAG = "ApkManager";
 17     private static final String INSTALL_CMD = "install";
 18     private static final String UNINSTALL_CMD = "uninstall";
 19 
 20     /**
 21      * APK静默安装
 22      * 
 23      * @param apkPath
 24      *            APK安装包路径
 25      * @return true 静默安装成功 false 静默安装失败
 26      */
 27     public static boolean install(String apkPath) {
 28         String[] args = { "pm", INSTALL_CMD, "-r", apkPath };
 29         String result = apkProcess(args);
 30         Log.e(TAG, "install log:"+result);
 31         if (result != null
 32                 && (result.endsWith("Success") || result.endsWith("Success\n"))) {
 33             return true;
 34         }
 35         return false;
 36     }
 37 
 38     /**
 39      * APK静默安装
 40      * 
 41      * @param packageName
 42      *            需要卸载应用的包名
 43      * @return true 静默卸载成功 false 静默卸载失败
 44      */
 45     public static boolean uninstall(String packageName) {
 46         String[] args = { "pm", UNINSTALL_CMD, packageName };
 47         String result = apkProcess(args);
 48         Log.e(TAG, "uninstall log:"+result);
 49         if (result != null
 50                 && (result.endsWith("Success") || result.endsWith("Success\n"))) {
 51             return true;
 52         }
 53         return false;
 54     }
 55 
 56     /**
 57      * 应用安装、卸载处理
 58      * 
 59      * @param args
 60      *            安装、卸载参数
 61      * @return Apk安装、卸载结果
 62      */
 63     public static String apkProcess(String[] args) {
 64         String result = null;
 65         ProcessBuilder processBuilder = new ProcessBuilder(args);
 66         Process process = null;
 67         InputStream errIs = null;
 68         InputStream inIs = null;
 69         try {
 70             ByteArrayOutputStream baos = new ByteArrayOutputStream();
 71             int read = -1;
 72             process = processBuilder.start();
 73             errIs = process.getErrorStream();
 74             while ((read = errIs.read()) != -1) {
 75                 baos.write(read);
 76             }
 77             baos.write('\n');
 78             inIs = process.getInputStream();
 79             while ((read = inIs.read()) != -1) {
 80                 baos.write(read);
 81             }
 82             byte[] data = baos.toByteArray();
 83             result = new String(data);
 84         } catch (Exception e) {
 85             e.printStackTrace();
 86         } finally {
 87             try {
 88                 if (errIs != null) {
 89                     errIs.close();
 90                 }
 91                 if (inIs != null) {
 92                     inIs.close();
 93                 }
 94             } catch (Exception e) {
 95                 e.printStackTrace();
 96             }
 97             if (process != null) {
 98                 process.destroy();
 99             }
100         }
101         return result;
102     }
103 }

本文转自:http://www.cnblogs.com/travellife/

相关文章

网友评论

    本文标题:静默安装 And 静默卸载

    本文链接:https://www.haomeiwen.com/subject/ncdpuftx.html