zoukankan      html  css  js  c++  java
  • android开发实现静默安装(root权限)

    方式是将应用设置为内置的系统应用,注意事system/app目录下面,采用copy2SystemApp()方法就可以,注意chmod 777的权限,若是直接将apk拷贝到system/app目录,没有这个权限还是不能静默安装的。

    直接贴出工具类:

    public class ApkController {
        /**
         * 描述: 安装
         */
        public static boolean install(String apkPath,Context context){
            // 先判断手机是否有root权限
            if(hasRootPerssion()){
                // 有root权限,利用静默安装实现
                return clientInstall(apkPath);
            }else{
                // 没有root权限,利用意图进行安装
                File file = new File(apkPath);
                if(!file.exists())
                    return false; 
                Intent intent = new Intent();
                intent.setAction("android.intent.action.VIEW");
                intent.addCategory("android.intent.category.DEFAULT");
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                intent.setDataAndType(Uri.fromFile(file),"application/vnd.android.package-archive");
                context.startActivity(intent);
                return true;
            }
        }
         
        /**
         * 描述: 卸载
         */
        public static boolean uninstall(String packageName,Context context){
            if(hasRootPerssion()){
                // 有root权限,利用静默卸载实现
                return clientUninstall(packageName);
            }else{
                Uri packageURI = Uri.parse("package:" + packageName);
                Intent uninstallIntent = new Intent(Intent.ACTION_DELETE,packageURI);
                uninstallIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                context.startActivity(uninstallIntent);
                return true;
            }
        }
         
        /**
         * 判断手机是否有root权限
         */
        public static boolean hasRootPerssion(){
            PrintWriter PrintWriter = null;
            Process process = null;
            try {
                process = Runtime.getRuntime().exec("su");
                PrintWriter = new PrintWriter(process.getOutputStream());
                PrintWriter.flush();
                PrintWriter.close();
                int value = process.waitFor();  
                return returnResult(value);
            } catch (Exception e) {
                e.printStackTrace();
            }finally{
                if(process!=null){
                    process.destroy();
                }
            }
            return false;
        }
         
        /**
         * 静默安装
         */
        public static boolean clientInstall(String apkPath){
            PrintWriter PrintWriter = null;
            Process process = null;
            try {
                process = Runtime.getRuntime().exec("su");
                PrintWriter = new PrintWriter(process.getOutputStream());
                PrintWriter.println("chmod 777 "+apkPath);
                PrintWriter.println("export LD_LIBRARY_PATH=/vendor/lib:/system/lib");
                PrintWriter.println("pm install -r "+apkPath);
    //          PrintWriter.println("exit");
                PrintWriter.flush();
                PrintWriter.close();
                int value = process.waitFor();
                return returnResult(value);
            } catch (Exception e) {
                e.printStackTrace();
            }finally{
                if(process!=null){
                    process.destroy();
                }
            }
            return false;
        }
         
        /**
         * 静默卸载
         */
        public static boolean clientUninstall(String packageName){
            PrintWriter PrintWriter = null;
            Process process = null;
            try {
                process = Runtime.getRuntime().exec("su");
                PrintWriter = new PrintWriter(process.getOutputStream());
                PrintWriter.println("LD_LIBRARY_PATH=/vendor/lib:/system/lib ");
                PrintWriter.println("pm uninstall "+packageName);
                PrintWriter.flush();
                PrintWriter.close();
                int value = process.waitFor();  
                return returnResult(value); 
            } catch (Exception e) {
                e.printStackTrace();
            }finally{
                if(process!=null){
                    process.destroy();
                }
            }
            return false;
        }
         
        /**
         * 启动app
         * com.exmaple.client/.MainActivity
         * com.exmaple.client/com.exmaple.client.MainActivity
         */
        public static boolean startApp(String packageName,String activityName){
            boolean isSuccess = false;
            String cmd = "am start -n " + packageName + "/" + activityName + " 
    ";
            Process process = null;
            try {
               process = Runtime.getRuntime().exec(cmd);
               int value = process.waitFor();  
               return returnResult(value);
            } catch (Exception e) {
              e.printStackTrace();
            } finally{
                if(process!=null){
                    process.destroy();
                }
            }
            return isSuccess;
        }
        /**
         * 将文件复制到system/app 目录
         * @param apkPath 特别注意格式:该路径不能是:/storage/emulated/0/app/QDemoTest4.apk 需要是:/sdcard/app/QDemoTest4.apk
         * @return
         */
        public static boolean copy2SystemApp(String apkPath){
            PrintWriter PrintWriter = null;
            Process process = null;
            String appName = "chetou.apk",cmd;
            
            try {
                process = Runtime.getRuntime().exec("su");
                PrintWriter = new PrintWriter(process.getOutputStream());
                cmd = "mount -o remount,rw -t yaffs2 /dev/block/mtdblock3 /system";
                Log.e("copy2SystemApp", cmd);
                PrintWriter.println(cmd);
                
                cmd = "cat "+apkPath+" > /system/app/"+appName;
                Log.e("copy2SystemApp", cmd);
                PrintWriter.println(cmd);
                
                cmd = "chmod 777 /system/app/"+appName +" -R";
                Log.e("copy2SystemApp", cmd);
                PrintWriter.println(cmd);
                
                cmd = "mount -o remount,ro -t yaffs2 /dev/block/mtdblock3 /system";
                Log.e("copy2SystemApp", cmd);
                PrintWriter.println(cmd);
                PrintWriter.println("reboot");  //重启
                PrintWriter.println("exit");
                PrintWriter.flush();
                PrintWriter.close();
                int value = process.waitFor();
                return returnResult(value);
            } catch (Exception e) {
                e.printStackTrace();
            }finally{
                if(process!=null){
                    process.destroy();
                }
            }
            return false;
        }
        private static boolean returnResult(int value){
            // 代表成功  
            if (value == 0) {
                return true;
            } else if (value == 1) { // 失败
                return false;
            } else { // 未知情况
                return false;
            }  
        }
    }
  • 相关阅读:
    组装query,query汇总,query字段
    POJ 1276, Cash Machine
    POJ 1129, Channel Allocation
    POJ 2531, Network Saboteur
    POJ 1837, Balance
    POJ 3278, Catch That Cow
    POJ 2676, Sudoku
    POJ 3126, Prime Path
    POJ 3414, Pots
    POJ 1426, Find The Multiple
  • 原文地址:https://www.cnblogs.com/feijian/p/5201572.html
Copyright © 2011-2022 走看看