apk静默安装遇到坑的解决

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;
    }


}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 176,251评论 25 709
  • Android开发中我们有时候需要借助一些命令帮助更好的高效率定位解决问题,本文就来介绍一些可能有些隐藏的而却非常...
    passiontim阅读 5,653评论 0 4
  • 当然我这里所说的没事找事并不是没事找茬的意思。不要多想了哈。 最近不知道为什么一个人经常会莫名的心情低落,可能跟日...
    呆丫阅读 4,251评论 0 1
  • 这几日无所事事,家长不在,翻了翻柜子,找出几本书来,这里的书,大多都是十年前我爸刚到这里时从家里带来的,后来只有我...
    仂七阅读 1,720评论 0 0
  • 一方窄窄的板凳,是司空见惯的物件。而在浙江省金华市浦江县虞宅乡新光村,这里的板凳还能“跳舞”,而且已经舞动了上百年...
    二更阅读 3,349评论 0 1

友情链接更多精彩内容