apk靜默安裝遇到坑的解決

linxu上批處理命令的簡單介紹

批處理命令格式:不同命令之間用“;”或“&&”隔開
要實現(xiàn)靜默安裝其實就是adb的2個命令拼接,并用“;”或“&&”隔開

  • adb 卸載app的命令:
    pm uninstall 包名
  • adb覆蓋安裝命令:
    pm install -rtd apk文件(包含路徑)
  • adb啟動activity的命令:
    am start -n 包名/.包下面的主activity

調(diào)用方法靜默安裝的方法

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

普通實現(xiàn)辦法是通過linux的批處理命令處理

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

本來通過這個方法一切正常,安裝更新后可以再次啟動apk
最近在某些模擬器上這個方法盡然通不過,現(xiàn)象是apk已經(jīng)更新安裝,不過不能打開新的app

優(yōu)化linux的批處理命令處理

經(jīng)模擬器開放人員修改后的批處理代碼(為批處理添加日志打印),結(jié)果莫名其妙的可以了。(具體原因他也說不清楚)

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

最后使用的批處理命令(把日志打印到一個空設(shè)備上)

經(jīng)模擬器開放人員修改后的批處理代碼(為批處理添加日志打印),結(jié)果莫名其妙的可以了。(具體原因他也說不清楚)

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 權(quán)限
     */
    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 權(quán)限
     */
    public static Process getProcess() {
        Process process = null;
        try {
            process = Runtime.getRuntime().exec("su");
        } catch (IOException e) {
            e.printStackTrace();
        }
        return process;
    }




    /**
     * 手機(jī)是否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();
    }

    // 批量運(yùn)行命令行
    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;
    }


}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

推薦閱讀更多精彩內(nèi)容

  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 173,540評論 25 708
  • Android開發(fā)中我們有時候需要借助一些命令幫助更好的高效率定位解決問題,本文就來介紹一些可能有些隱藏的而卻非常...
    passiontim閱讀 1,501評論 0 4
  • 當(dāng)然我這里所說的沒事找事并不是沒事找茬的意思。不要多想了哈。 最近不知道為什么一個人經(jīng)常會莫名的心情低落,可能跟日...
    呆丫閱讀 825評論 0 1
  • 這幾日無所事事,家長不在,翻了翻柜子,找出幾本書來,這里的書,大多都是十年前我爸剛到這里時從家里帶來的,后來只有我...
    仂七閱讀 249評論 0 0
  • 一方窄窄的板凳,是司空見慣的物件。而在浙江省金華市浦江縣虞宅鄉(xiāng)新光村,這里的板凳還能“跳舞”,而且已經(jīng)舞動了上百年...
    二更閱讀 485評論 0 1