2017-03-06 31 views
0

我想在我的android設備中啓動內部日誌,而不必直接進入adb shell。我必須編寫這個過程的腳本,以便我可以從程序中運行它。在內部存儲器上啓動adb日誌而不進入shell

我知道,你可以採取以下步驟在Android設備上啓動內部日誌:

  1. 打開一個命令提示符
  2. 進入「亞殼」
  3. 輸入「logcat的-v時間-f /sdcard/LogFile.txt &'

以上將啓動實際設備中的logcat進程。我現在可以從電腦上拔下手機,然後四處走動,然後返回並在測試完成後收集日誌。關鍵是我能夠啓動此過程,並能夠在日誌仍在運行的情況下拔出設備。

據我所知,在任何命令前面運行'adb shell'會像運行在shell中一樣運行。因此,通過這個邏輯我試圖運行:

方法1:

'亞行外殼的logcat -v時間-f /sdcard/LogFile.txt &'

該命令沒有正確的啓動日誌該設備是偉大的。但是,一旦我從計算機上拔下logcat進程就會停止。

方法2:

'亞行殼 「的logcat -v時間-f /sdcard/LogFile.txt &」'

這似乎並沒有在手機上做任何事情和我不知道爲什麼。

方法3

我試圖腳本方法以及其中I運行包含一個批處理文件只: 「ADB殼< commands.txt中」

凡命令具有單行:

'的logcat -v時間-f /sdcard/LogFile.txt &'

這似乎並沒有做任何事情。一旦窗口出現,它似乎發送命令,但實際上並沒有執行該操作。

任何有關這個主題的幫助將不勝感激。謝謝。

回答

0

您可以在應用程序類中使用此代碼。所以當應用程序將啓動此代碼將開始執行。它會根據當前時間創建新的日誌文件。

Thread t = new Thread(new Runnable() { 
    public void run() { 
     while (collectLog == true) { 
      try { 
       Thread.sleep(1000 * 60 * 6); 
       StringBuilder log = null; 
       Date now = new Date(); 
       String fileName = formatter.format(now); 
       File file = new File(dir, fileName + "_logcat2.txt"); 
       try { 

        Process process = Runtime.getRuntime().exec("logcat -d");// d will dump logs 
        //Process process = Runtime.getRuntime().exec("logcat -c"); c will clear logs 
        // process = Runtime.getRuntime().exec("logcat -f " + file); 
        BufferedReader bufferedReader = new BufferedReader(
          new InputStreamReader(process.getInputStream())); 
        log = new StringBuilder(); 
        String line; 
        while ((line = bufferedReader.readLine()) != null) { 
         log.append(line); 
         log.append("\n"); 
        } 
       } catch (IOException e) { 
       } 
       try { 
        //to write logcat in text file 
        FileOutputStream fOut = new FileOutputStream(file); 
        OutputStreamWriter osw = new OutputStreamWriter(fOut) 
        // Write the string to the file 
        osw.append(log.toString()); 
        osw.flush(); 
        osw.close(); 
       } catch (FileNotFoundException e) { 
        e.printStackTrace(); 
       } catch (IOException e) { 
        e.printStackTrace(); 
       } 

      } catch (InterruptedException e) { 
       e.printStackTrace(); 
      } 
     } 
    } 
}); 

t.start(); 
+0

這是用C#還是java編寫的? – Jim

+0

Java(安卓版) – Parth

+0

所以我實際上並不是在編寫一個android應用程序我正在編寫一個C#GUI,用戶可以在其中收集日誌並在連接到PC的設備上啓動內部日誌。這段代碼很不幸沒有幫助我... – Jim