Android廣告機自動重啓處理實錄

背景:項目採用安卓廣告機,適配原內容管理控制播放系統,系統運行穩定。換裝咱們的APP,設備會出現不定時的批量的設備重啓。java

分析:APP在測試環境一切OK,在生產環境依舊會出現小批量的不按期重啓git

發現:關閉設備中的「看門狗」選項,設備一切使用正常,無重啓現象。聯繫廠家,廠家反饋---看門狗爲設備異常的底層守護程序,關閉可能致使系統或者硬件損壞。不建議關閉。shell

解決辦法:生產環境,安裝日誌收集工具APP,收集系統異常日誌,進行分析處理。網絡

  1.因爲設備運行於內網環境,因此沒法使用TeamView等遠程軟件,何況前提是安裝TeamView QuickSupport,因爲牆的存在,安裝估計要花些力氣。app

       2.採用ADB 網絡鏈接的方式,遠程控制廣告機,安裝異常日誌收集APP,等待重啓,而後整理logless

       3.ADB操做方法ide

  Windows環境下,下載ADBTools,一共1M不到,解壓相應文件到指定文件夾。工具

cmd環境下:adb connect ip:port測試

1. 顯示當前運行的所有模擬器:
    adb devices
ui

2. 安裝應用程序:
    adb install -r 123.apk

3. 獲取模擬器中的文件:
    adb pull <remote> <local>

4. 向模擬器中寫文件:
    adb push <local> <remote>

5. 進入模擬器的shell模式:
    adb shell

6. 缷載apk包:
      adb shell
      cd data/app
      rm 123.apk
      exit
      adb uninstall 123.apk
      adb install -r 123.apk

7. 查看adb命令幫助信息:
      adb help
8. 刪除系統應用:
      adb remount (從新掛載系統分區,使系統分區從新可寫)。
      adb shell
      cd system/app
      rm 123.apk

9. 獲取管理員權限:
      adb root

十、複製文件: 
     複製一個文件或目錄到設備: 
      adb push <source> <destination></destination></source> 
      如:adb push update.zip /sdcard/ 
     從設備上覆制一個文件或目錄: 
     adb pull <source> <destination></destination></source> 
     如:adb pull /sdcard/update.zip.

十一、取得當前運行設備的實例的列表及每一個實例的狀態: 
    adb devices

十二、 #cd system/sd/data //進入系統內指定文件夾 
           cd .. 返回上一級目錄 
           cd ../.. 返回上兩級目錄 
           cd 進入我的的主目錄 
          cd ~user1 進入我的的主目錄 
          cd - 返回上次所在的目錄


#ls //列表顯示當前文件夾內容 
#rm -r xxx //刪除名字爲xxx的文件夾及其裏面的全部文件 
#rm xxx //刪除文件xxx 
#rmdir xxx //刪除xxx的文件夾 

#mkdir -p xxx //遞歸建立xxx的文件夾
#cp [選項] [來源文件] [目的文件],-d 複製一個快捷方式/-r 複製一個目錄/-i 對一個存在的文件,詢問是否覆蓋
#mv [選項] [來源文件] [目標文件],-u 目標文件存在時纔會生效,若是源文件比目標文件新纔會移動/-i 對一個存在的文件,詢問是否覆蓋;


#cat 經常使用命令,即查看一個文件的內容並顯示在屏幕上。
     more 也是用來查看一個文件的內容。當文件內容太多,一屏幕不能佔下,使用more當看完一屏後按空格鍵繼續看下一屏。看完全部內容後就會退出。若是你想提早退出,只需按q鍵便可。
     less 做用跟more同樣,但比more好在能夠上翻,下翻。空格鍵一樣能夠翻頁,而按」j」鍵能夠向下移動(按一下就向下移動一行),按」k」鍵向上移動。在使用more和less查看某個文件時,你能夠按一下」/」 鍵,而後輸入一個word回車,這樣就能夠查找這個word了。若是是多個該word能夠按」n」鍵顯示下一個。另外你也能夠不按」/」而是按」?」後邊一樣跟word來搜索這個word,惟一不一樣的是,」/」是在當前行向下搜索,而」?」是在當前行向上搜索。

解決方案:使用程序批量運行ADB,以達到批量操做,下面貼出源碼

import java.io.IOException;
import java.util.Arrays;

/**
 * @Description:
 * @User: Administrator
 * @Date: 2018/7/30 0030
 */
public class Adb {

    String adbPath = "adb";
    String packageName = "cn.firegod.study.myapplication";
    String activityName = "MainActivity";
    String app = "/Users/yangyan/git/MyApplication/app/build/outputs/apk/debug/app-debug-ckp.apk.apk";

    enum Command {
        CONNECT, INSTALL_APP, UNINSTALL_APP, REBOOT, START_APP, STOP_APP, CLEAN_APP, WAIT
    }

    private void connect(String host) {
        String cmd = adbPath + " connect " + host;
        try {
            Process exec = Runtime.getRuntime().exec(cmd);
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    private void install(String host, String apkCkpFile) {
        String cmd = adbPath + " -s " + host + " install " + apkCkpFile;
        try {
            Process exec = Runtime.getRuntime().exec(cmd);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void uninstall(String host, String packageName) {
        String cmd = adbPath + " -s " + host + " uninstall " + packageName;
        try {
            Process exec = Runtime.getRuntime().exec(cmd);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void start(String host, String packageName, String activity) {
        String cmd = adbPath + " -s " + host + " shell am start  " + packageName + "/." + activity;
        try {
            Process exec = Runtime.getRuntime().exec(cmd);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void stop(String host, String packageName) {
        String cmd = adbPath + " -s " + host + " shell am force-stop  " + packageName;
        try {
            Process exec = Runtime.getRuntime().exec(cmd);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void clearData(String host, String packageName) {
        String cmd = adbPath + " -s " + host + " shell pm clear " + packageName;
        try {
            Process exec = Runtime.getRuntime().exec(cmd);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void reboot(String host) {
        String cmd = adbPath + " -s " + host + " reboot";
        try {
            Process exec = Runtime.getRuntime().exec(cmd);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 一個 host 執行多個 cmd
     *
     * @param host
     * @param cmds
     */
    private void batchCmd(String host, Command... cmds) {
        for (Command cmd : cmds) {
            executeCmd(cmd, host);
        }
    }

    /**
     * 多個 host 執行一個 cmd
     *
     * @param cmd
     * @param hosts
     */
    private void batchHost(Command cmd, String... hosts) {
        for (String host : hosts) {
            executeCmd(cmd, host);
        }
    }

    private void executeCmd(Command cmd, String host) {
        switch (cmd) {
            case CONNECT:
                //鏈接
                this.connect(host);
                break;
            case STOP_APP:
                this.stop(host, packageName);
                break;
            case CLEAN_APP:
                this.clearData(host, packageName);
                break;
            case UNINSTALL_APP:
                this.uninstall(host, packageName);
                break;
            case INSTALL_APP:
                this.install(host, app);
                break;
            case START_APP:
                this.start(host, packageName, activityName);
                break;
            case REBOOT:
                this.reboot(host);
                break;
            case WAIT:
                this.sleep(2000);
                break;
            default:
                System.out.println("命令不認識");
        }
    }

    private void sleep(int millis) {
        try {
            Thread.sleep(millis);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }


    public static void main(String[] args) {

        Adb adb = new Adb();

//        一樓柱子屏 6.10 - 6.20
        String[] layer_1_zhuzi = new String[]{
                "172.16.6.10",
                "172.16.6.11",
                "172.16.6.12",
                "172.16.6.13",
                "172.16.6.14",
                "172.16.6.15",
                "172.16.6.16",
                "172.16.6.17",
                "172.16.6.18",
                "172.16.6.19",
                "172.16.6.20"
        };
//        一樓窗口屏 7.10 - 7.42
        String[] layer_1_ck = new String[]{
                "172.16.7.10",
                "172.16.7.11",
                "172.16.7.12",
                "172.16.7.13",
                "172.16.7.14",
                "172.16.7.15",
                "172.16.7.16",
                "172.16.7.17",
                "172.16.7.18",
                "172.16.7.19",
                "172.16.7.20",
                "172.16.7.21",
                "172.16.7.22",
                "172.16.7.23",
                "172.16.7.24",
                "172.16.7.25",
                "172.16.7.26",
                "172.16.7.27",
                "172.16.7.28",
                "172.16.7.29",
                "172.16.7.30",
                "172.16.7.31",
                "172.16.7.32",
                "172.16.7.33",
                "172.16.7.34",
                "172.16.7.35",
                "172.16.7.36",
                "172.16.7.37",
                "172.16.7.38",
                "172.16.7.39",
                "172.16.7.40",
                "172.16.7.41",
                "172.16.7.42"
        };
//        三樓窗口屏 7.119 - 7.150
        String[] layer_3_ck = new String[]{
                "172.16.7.119",
                "172.16.7.120",
                "172.16.7.121",
                "172.16.7.122",
                "172.16.7.123",
                "172.16.7.124",
                "172.16.7.125",
                "172.16.7.126",
                "172.16.7.127",
                "172.16.7.128",
                "172.16.7.129",
                "172.16.7.130",
                "172.16.7.131",
                "172.16.7.132",
                "172.16.7.133",
                "172.16.7.134",
                "172.16.7.135",
                "172.16.7.136",
                "172.16.7.137",
                "172.16.7.138",
                "172.16.7.139",
                "172.16.7.140",
                "172.16.7.141",
                "172.16.7.142",
                "172.16.7.143",
                "172.16.7.144",
                "172.16.7.145",
                "172.16.7.146",
                "172.16.7.147",
                "172.16.7.148",
                "172.16.7.149",
                "172.16.7.150"
        };
//        三樓柱子屏 6.39 - 6.45
        String[] layer_3_zhuzi = new String[]{
                "172.16.6.39",
                "172.16.6.40",
                "172.16.6.41",
                "172.16.6.42",
                "172.16.6.43",
                "172.16.6.44",
                "172.16.6.45"
        };

        String port = "55555";

//        String ipPrefix = "172.16.7";
//        int startIP = 10;
//        int stopIP = 42;
//        for (int j = startIP; j < stopIP + 1; j++) {
//            System.out.println(ipPrefix + "." + j + ":" + port);
//        }

        String executeHost[] = layer_1_zhuzi;
        for (String host : executeHost) {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    adb.batchCmd(host + ":" + port, Command.CONNECT, Command.WAIT, Command.REBOOT);
                }
            }).start();
        }
        try {
            Thread.sleep(10000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
相關文章
相關標籤/搜索