美文网首页Android技术手册
apk静默安装遇到坑的解决

apk静默安装遇到坑的解决

作者: 牵手生活 | 来源:发表于2017-09-25 21:28 被阅读0次

    linxu上批处理命令的简单介绍

    批处理命令格式:不同命令之间用“;”或“&&”隔开
    要实现静默安装其实就是adb的2个命令拼接,并用“;”或“&&”隔开

    • adb 卸载app的命令:
      pm uninstall 包名
    • adb覆盖安装命令:
      pm install -rtd apk文件(包含路径)
    • adb启动activity的命令:
      am start -n 包名/.包下面的主activity

    调用方法静默安装的方法

            String apkPath = apk文件(包含路径);
            String runApp = "am start -n 包名/.包下的启动activity >/dev/null 2>&1;";
            String install = "pm install -rtd " + apkPath + " >/dev/null 2>&1; ";
            String bat = install + runApp;
            Log.i(TAG,"linxu bat :"+bat);
            SystemManager.RootCommand(bat);
    

    普通实现办法是通过linux的批处理命令处理

    pm install -rtd /storage/emulated/0/apkpaht/apkfile;am start -n com.***.***/.activity.LaunchActivity
    

    本来通过这个方法一切正常,安装更新后可以再次启动apk
    最近在某些模拟器上这个方法尽然通不过,现象是apk已经更新安装,不过不能打开新的app

    优化linux的批处理命令处理

    经模拟器开放人员修改后的批处理代码(为批处理添加日志打印),结果莫名其妙的可以了。(具体原因他也说不清楚)

    pm install -rtd /storage/emulated/0/apkpaht/apkfile  >sdcard/test.log 2>&1; am start -n com.***.***/.activity.LaunchActivity >>sdcard/test.log 2>&1;
    

    最后使用的批处理命令(把日志打印到一个空设备上)

    经模拟器开放人员修改后的批处理代码(为批处理添加日志打印),结果莫名其妙的可以了。(具体原因他也说不清楚)

    pm install -rtd /storage/emulated/0/apkpaht/apkfile  >/dev/null 2>&1;  am start -n com.***.***/.activity.LaunchActivity >>/dev/null 2>&1; ;
    

    附:SystemManager工具类

    public class SystemManager {
    
    
        public static void RootCommand(String command) {
            Process process = null;
            DataOutputStream os = null;
            try {
                process = Runtime.getRuntime().exec("su");
                os = new DataOutputStream(process.getOutputStream());
                os.writeBytes(command + "\n");
                os.writeBytes("exit\n");
                os.flush();
                process.waitFor();
            } catch (Exception e) {
                TogglableLog.e("SystemManager", "command:" + command);
            } finally {
                try {
                    if (os != null) {
                        os.close();
                    }
                    if (process != null) {
                        process.destroy();
                    }
                } catch (Exception e) {
                    LogUtil.e("SystemManager", e);
                }
            }
            TogglableLog.d("SystemManager", "root success");
        }
    
    
        public static void Root(String command) {
            Process process = null;
            DataOutputStream os = null;
            try {
                process = getProcess();
                os = new DataOutputStream(process.getOutputStream());
                os.writeBytes(command + "\n");
                os.writeBytes("exit\n");
                os.flush();
                process.waitFor();
            } catch (Exception e) {
                Log.e("Root", "command:" + command);
            } finally {
                try {
                    if (os != null) {
                        os.close();
                    }
                    process.destroy();
                } catch (Exception e) {
                    Log.e("Root", "" + e);
                }
            }
            Log.d(" Root", "root success");
        }
    
    
        public static void Root777(String command) {
            Process process = null;
            DataOutputStream os = null;
            try {
                process = getProcess();
                os = new DataOutputStream(process.getOutputStream());
                command = "chmod 777 " + command;
                os.writeBytes(command + "\n");
                os.writeBytes("exit\n");
                os.flush();
                process.waitFor();
            } catch (Exception e) {
                Log.e("Root", "command:" + command);
            } finally {
                try {
                    if (os != null) {
                        os.close();
                    }
                    process.destroy();
                } catch (Exception e) {
                    Log.e("Root", "" + e);
                }
            }
            Log.d(" Root", "root success");
        }
    
        /**
         * root 权限
         */
        public static void suRoot(final Context context) {
            try {
                TermSessionCommandUtil.getInstance(context).exec("su");
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        authXposedFile2(context);
                    }
                }).start();
    
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    
        /**
         * 获取root 权限
         */
        public static Process getProcess() {
            Process process = null;
            try {
                process = Runtime.getRuntime().exec("su");
            } catch (IOException e) {
                e.printStackTrace();
            }
            return process;
        }
    
    
    
    
        /**
         * 手机是否root
         *
         * @return
         */
        public static boolean isRoot() {
            boolean isRoot = false;
            String sys = System.getenv("PATH");
            ArrayList<String> commands = new ArrayList<String>();
            String[] path = sys.split(":");
            for (int i = 0; i < path.length; i++) {
                String commod = "ls -l " + path[i] + "/su";
                commands.add(commod);
                System.out.println("commod : " + commod);
            }
            ArrayList<String> res = run("/system/bin/sh", commands);
            String response = "";
            for (int i = 0; i < res.size(); i++) {
                response += res.get(i);
            }
            int inavailableCount = 0;
            String root = "-rwsr-sr-x root root";
            for (int i = 0; i < res.size(); i++) {
                if (res.get(i).contains("No such file or directory")
                        || res.get(i).contains("Permission denied")) {
                    inavailableCount++;
                }
            }
            return inavailableCount < res.size();
        }
    
        // 批量运行命令行
        private static ArrayList run(String shell, ArrayList<String> commands) {
            ArrayList output = new ArrayList();
            Process process = null;
            try {
                process = Runtime.getRuntime().exec(shell);
                BufferedOutputStream shellInput = new BufferedOutputStream(
                        process.getOutputStream());
                BufferedReader shellOutput = new BufferedReader(
                        new InputStreamReader(process.getInputStream()));
    
                for (String command : commands) {
                    shellInput.write((command + " 2>&1\n").getBytes());
                }
    
                shellInput.write("exit\n".getBytes());
                shellInput.flush();
    
                String line;
                while ((line = shellOutput.readLine()) != null) {
                    output.add(line);
                }
    
                process.waitFor();
            } catch (IOException e) {
                e.printStackTrace();
            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally {
                process.destroy();
            }
    
            return output;
        }
    
    
    }
    
    

    相关文章

      网友评论

        本文标题:apk静默安装遇到坑的解决

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