在Android界面上显示和获取Logcat日志输出
一、首先我們要獲取Logcat中的日志
如何獲取呢?
首先我們要先定義一個String[]數組,里面的代碼是
//第一個是Logcat ,也就是我們想要獲取的log日志 //第二個是 -s 也就是表示過濾的意思 //第三個就是 我們要過濾的類型 W表示warm ,我們也可以換成 D :debug, I:info,E:error等等 String[] running = new String[]{"logcat","-s","adb logcat *: W"}; 復制代碼當我們設置好之后,我們還需要一個process類,作用通俗來講就是用Java代碼來進行adb命令行操作代碼是:
Process exec = Runtime.getRuntime().exec(running); 復制代碼通過以上的方法我們就可以獲得和過濾Logcat中的方法。
二、接下來開始使用IO流進行字符操作,把數據保存在Android SDCard中
首先:我們定義一個InputStream,
final InputStream is = exec.getInputStream 復制代碼接下來開啟一個線程,線程中的方法就是通過IO流先讀取Logcat中的數據,然后再把數據通過OutPutStream方法寫入到SDCard中。
new Thread() {@Overridepublic void run() {FileOutputStream os = null;try {//新建一個路徑信息os = new FileOutputStream("/sdcard/Log/Log.txt");int len = 0;byte[] buf = new byte[1024];while (-1 != (len = is.read(buf))) {os.write(buf, 0, len);os.flush();}} catch (Exception e) {Log.d("writelog","read logcat process failed. message: "+ e.getMessage());} finally {if (null != os) {try {os.close();os = null;} catch (IOException e) {// Do nothing}}}}}.start();} catch (Exception e) {Log.d("writelog","open logcat process failed. message: " + e.getMessage());}} 復制代碼當我們這個類寫完之后,我們再把權限添加進去就可以了。
<!-- 讀取Log權限 --><uses-permission android:name="android.permission.READ_LOGS" /><!-- 在SDCard中創建與刪除文件權限 --><uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" /><!-- 往SDCard寫入數據權限 --><uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /><!-- 從SDCard讀出數據權限 --><uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> 復制代碼添加完權限,我們運行試試。
然后我們再打開我們的SDCard中的文件目錄:
這樣我們就已經獲取到了Logcat中的日志(可以和控制臺的對比一下):
由于我開啟了兩次所以打印出了兩次的log.
三、之后我們先創建頁面,然后在按行讀取Txt文本中的內容
首先我們開始編寫XMl視圖文件:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"tools:context=".MainActivity" ><LinearLayout android:layout_width="match_parent"android:layout_weight="7"android:orientation="vertical"><ListView android:id="@+id/ListLog"android:layout_width="match_parent"android:layout_height="match_parent"></ListView></LinearLayout><LinearLayoutandroid:layout_width="match_parent"android:layout_weight="1"android:gravity="center"android:orientation="horizontal" ><Button android:layout_gravity="center"android:gravity="center"android:id="@+id/BtnLog"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="清空日志"/></LinearLayout></LinearLayout>復制代碼編寫完成后,我們開始在MainActivity里面初始化我們的類
private ListView listView;private Button btn;listView = (ListView) findViewById(R.id.ListLog);btn = (Button) findViewById(R.id.BtnLog);復制代碼之后,我們開始編寫我們的讀取TXT文件的方法
/** * 根據行讀取內容 * @return */ public List<String> Txt() { //將讀出來的一行行數據使用List存儲 String filePath = "/sdcard/Log.txt"; List newList=new ArrayList<String>(); try { File file = new File(filePath); int count = 0;//初始化 key值 if (file.isFile() && file.exists()) {//文件存在 InputStreamReader isr = new InputStreamReader(new FileInputStream(file)); BufferedReader br = new BufferedReader(isr); String lineTxt = null; while ((lineTxt = br.readLine()) != null) { if (!"".equals(lineTxt)) { String reds = lineTxt.split("\\+")[0]; //java 正則表達式 newList.add(count, reds); count++; } } isr.close(); br.close(); }else { Log.e("tag", "can not find file");} } catch (Exception e) { e.printStackTrace(); } return newList; } 復制代碼我們看d的代碼,其實也就是IO讀寫操作
if (file.isFile() && file.exists()) //這一行是判斷是否有文件存在 復制代碼然后我們用InputStreamReader讀取我們SDCard中的文件;
使用BufferedReader方法讀取我們獲取的字符流;
最后我們用While循環和正則表達式來把每一行都給放入List中;
最后我們返回List;
InputStreamReader isr = new InputStreamReader(new FileInputStream(file)); BufferedReader br = new BufferedReader(isr); String lineTxt = null; while ((lineTxt = br.readLine()) != null) { if (!"".equals(lineTxt)) { String reds = lineTxt.split("\\+")[0]; //java 正則表達式 newList.add(count, reds); count++; } } 復制代碼還有一個XML視圖文件,名稱log_list_item.xml
<TextView xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent" android:textColor="#000000"android:gravity="left"android:paddingLeft="20dp"android:textSize="20sp"android:singleLine="true"/> 復制代碼接下來就是把List放入ListView中:
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.log_list_item,Txt());listView.setAdapter(adapter); 復制代碼好讓我們運行一下看看效果:
好了,我們的顯示日志也已經成功了。接下來就是要可以清空日志;
最后、清空日志
如何清空日志呢?
其實非常簡單
/*** 刪除Log文件* @param fileName 文件路徑和名稱*/public static void delFile(String fileName){ File file = new File(fileName); if(file.isFile()){ file.delete(); } file.exists(); } 復制代碼我們只需要把路徑傳過去,進行判斷,如果有就直接刪除。
然后我們對ListView進行刷新就可以了。
轉載于:https://juejin.im/post/5aaa1421518825555c1d5fde
總結
以上是生活随笔為你收集整理的在Android界面上显示和获取Logcat日志输出的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Linux 软件安装与卸载之获取程序包的
- 下一篇: android--------内存泄露分