2016-04-24 26 views
1

我正在使用logcat在Android應用程序中記錄消息,我希望能夠訪問這些消息並在DialogFragment的TextView中顯示它們。消息列表非常不一致,每次打開和關閉對話框時都會發生更改。它會顯示完整歷史記錄一次,然後下一次刪除,有時會顯示更多消息。每當我打開對話框時,我能做些什麼來顯示所有消息(用於運行)?我是在錯誤的時間還是在某個地方運行流程?或者緩衝區應該在其他地方處理?謝謝。如何在Android的TextView中打印完整的logcat歷史記錄?

public boolean onOptionsItemSelected(MenuItem item) { 
    switch (item.getItemId()) { 
     case R.id.action_view_logs: 
      // View Logs MenuItem tapped 
      if (logsDialogFragment == null) { 
       logsDialogFragment = new LogsDialogFragment(); 
      } 
      logsDialogFragment.show(getFragmentManager(), "dialog"); 
      return true; 
     case R.id.action_exit: 
      // Exit MenuItem tapped 
      finish(); 
      return true; 
     default: 
      return super.onOptionsItemSelected(item); 
    } 
} 

public class LogsDialogFragment extends DialogFragment { 

@Override 
public Dialog onCreateDialog(Bundle savedInstanceState) { 
    Log.i(WifiDirectHandler.LOG_TAG, "Viewing Logs"); 
    AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(getActivity()) 
     .setTitle(getString(R.string.title_logs)) 
     .setNegativeButton(getString(R.string.action_close), 
      new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog, int whichButton) { 
        dialog.dismiss(); 
       } 
      } 
     ); 

    LayoutInflater i = getActivity().getLayoutInflater(); 
    View rootView = i.inflate(R.layout.fragment_logs_dialog, null); 

    TextView logTextView = (TextView) rootView.findViewById(R.id.logTextView); 
    logTextView.setMovementMethod(new ScrollingMovementMethod()); 

    try { 
     Process process = Runtime.getRuntime().exec("logcat -d"); 
     BufferedReader bufferedReader = new BufferedReader(
       new InputStreamReader(process.getInputStream())); 

     StringBuilder log = new StringBuilder(); 
     String line; 
     while ((line = bufferedReader.readLine()) != null) { 
      if (line.contains(WifiDirectHandler.LOG_TAG)){ 
       // Removes log tag and PID from the log line 
       log.append(line.substring(line.indexOf(": ") + 2)).append("\n"); 
      } 
     } 
     logTextView.setText(log.toString()); 
    } catch (IOException e) { 
    } 

    dialogBuilder.setView(rootView); 
    return dialogBuilder.create(); 
} 
} 
+0

是'logTextView'顯示結果嗎? –

+0

是的,但每次打開和關閉對話框時結果都不相同。 – Brendan

+0

結果不一樣,它的自然,Log貓顯示不同,請看詳細,你可能不一樣。 –

回答

0

我最終將歷史記錄保存到一個成員變量,並且每次打開對話框時只添加新的日誌行。

public class LogsDialogFragment extends DialogFragment { 

    private StringBuilder log = new StringBuilder(); 

    @Override 
    public Dialog onCreateDialog(Bundle savedInstanceState) { 
     // Sets the Layout for the UI 
     LayoutInflater i = getActivity().getLayoutInflater(); 
     View rootView = i.inflate(R.layout.fragment_logs_dialog, null); 

     TextView logTextView = (TextView) rootView.findViewById(R.id.logTextView); 
     logTextView.setMovementMethod(new ScrollingMovementMethod()); 

     try { 
      Process process = Runtime.getRuntime().exec("logcat -d"); 
      BufferedReader bufferedReader = new BufferedReader(
        new InputStreamReader(process.getInputStream())); 

      StringBuilder log = new StringBuilder(); 
      String line; 
      while ((line = bufferedReader.readLine()) != null) { 
       if (line.contains(WifiDirectHandler.LOG_TAG)){ 
        // Removes log tag and PID from the log line 
        log.append(line.substring(line.indexOf(": ") + 2)).append("\n"); 
       } 
      } 

      this.log.append(log.toString().replace(this.log.toString(), "")); 
      logTextView.setText(this.log.toString()); 
     } catch (IOException e) { 
      Log.e("wifiDirectHandler", "Failure reading logcat"); 
     } 

     // Creates and returns the AlertDialog for the logs 
     AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(getActivity()) 
      .setTitle(getString(R.string.title_logs)) 
      .setNegativeButton(getString(R.string.action_close), 
       new DialogInterface.OnClickListener() { 
        public void onClick(DialogInterface dialog, int whichButton) { 
         dialog.dismiss(); 
        } 
       } 
      ).setView(rootView); 
     return dialogBuilder.create(); 
    } 
} 
1
Please add permission in manifest for logcat <uses-permission android:name="android.permission.READ_LOGS" /> 
2

的logcat的消息緩衝區不是很大,所以舊的郵件一下就從logcat中刪除,如果你需要完整的日誌 - 保存logcat的消息發送到SD卡,然後文件從它讀取信息。此方法會將所有當前消息保存到文件中:

public static void saveLogcatToFile(Context context) { 
     String fileName = "yourlogname.log"; 
     File outputFile = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + 
       "/YourAppName", fileName); 
     try { 
      @SuppressWarnings("unused") 
      Process process = Runtime.getRuntime().exec("logcat -f "+outputFile.getAbsolutePath()); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
    } 

上面的代碼只是某個時間點日誌的快照。如果你想不斷地寫日誌,可以使用Java的日誌記錄功能,然後寫入。

相關問題