在Android界面上显示和获取Logcat日志输出
一、首先我們要獲取Logcat中的日志
如何獲取呢?
首先我們要先定義一個(gè)String[]數(shù)組,里面的代碼是
//第一個(gè)是Logcat ,也就是我們想要獲取的log日志 //第二個(gè)是 -s 也就是表示過濾的意思 //第三個(gè)就是 我們要過濾的類型 W表示warm ,我們也可以換成 D :debug, I:info,E:error等等 String[] running = new String[]{"logcat","-s","adb logcat *: W"}; 復(fù)制代碼當(dāng)我們?cè)O(shè)置好之后,我們還需要一個(gè)process類,作用通俗來講就是用Java代碼來進(jìn)行adb命令行操作代碼是:
Process exec = Runtime.getRuntime().exec(running); 復(fù)制代碼通過以上的方法我們就可以獲得和過濾Logcat中的方法。
二、接下來開始使用IO流進(jìn)行字符操作,把數(shù)據(jù)保存在Android SDCard中
首先:我們定義一個(gè)InputStream,
final InputStream is = exec.getInputStream 復(fù)制代碼接下來開啟一個(gè)線程,線程中的方法就是通過IO流先讀取Logcat中的數(shù)據(jù),然后再把數(shù)據(jù)通過OutPutStream方法寫入到SDCard中。
new Thread() {@Overridepublic void run() {FileOutputStream os = null;try {//新建一個(gè)路徑信息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());}} 復(fù)制代碼當(dāng)我們這個(gè)類寫完之后,我們?cè)侔褭?quán)限添加進(jìn)去就可以了。
<!-- 讀取Log權(quán)限 --><uses-permission android:name="android.permission.READ_LOGS" /><!-- 在SDCard中創(chuàng)建與刪除文件權(quán)限 --><uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" /><!-- 往SDCard寫入數(shù)據(jù)權(quán)限 --><uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /><!-- 從SDCard讀出數(shù)據(jù)權(quán)限 --><uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> 復(fù)制代碼添加完權(quán)限,我們運(yùn)行試試。
然后我們?cè)俅蜷_我們的SDCard中的文件目錄:
這樣我們就已經(jīng)獲取到了Logcat中的日志(可以和控制臺(tái)的對(duì)比一下):
由于我開啟了兩次所以打印出了兩次的log.
三、之后我們先創(chuàng)建頁面,然后在按行讀取Txt文本中的內(nèi)容
首先我們開始編寫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>復(fù)制代碼編寫完成后,我們開始在MainActivity里面初始化我們的類
private ListView listView;private Button btn;listView = (ListView) findViewById(R.id.ListLog);btn = (Button) findViewById(R.id.BtnLog);復(fù)制代碼之后,我們開始編寫我們的讀取TXT文件的方法
/** * 根據(jù)行讀取內(nèi)容 * @return */ public List<String> Txt() { //將讀出來的一行行數(shù)據(jù)使用List存儲(chǔ) 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 正則表達(dá)式 newList.add(count, reds); count++; } } isr.close(); br.close(); }else { Log.e("tag", "can not find file");} } catch (Exception e) { e.printStackTrace(); } return newList; } 復(fù)制代碼我們看d的代碼,其實(shí)也就是IO讀寫操作
if (file.isFile() && file.exists()) //這一行是判斷是否有文件存在 復(fù)制代碼然后我們用InputStreamReader讀取我們SDCard中的文件;
使用BufferedReader方法讀取我們獲取的字符流;
最后我們用While循環(huán)和正則表達(dá)式來把每一行都給放入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 正則表達(dá)式 newList.add(count, reds); count++; } } 復(fù)制代碼還有一個(gè)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"/> 復(fù)制代碼接下來就是把List放入ListView中:
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.log_list_item,Txt());listView.setAdapter(adapter); 復(fù)制代碼好讓我們運(yùn)行一下看看效果:
好了,我們的顯示日志也已經(jīng)成功了。接下來就是要可以清空日志;
最后、清空日志
如何清空日志呢?
其實(shí)非常簡(jiǎn)單
/*** 刪除Log文件* @param fileName 文件路徑和名稱*/public static void delFile(String fileName){ File file = new File(fileName); if(file.isFile()){ file.delete(); } file.exists(); } 復(fù)制代碼我們只需要把路徑傳過去,進(jìn)行判斷,如果有就直接刪除。
然后我們對(duì)ListView進(jìn)行刷新就可以了。
轉(zhuǎn)載于:https://juejin.im/post/5aaa1421518825555c1d5fde
總結(jié)
以上是生活随笔為你收集整理的在Android界面上显示和获取Logcat日志输出的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Linux 软件安装与卸载之获取程序包的
- 下一篇: 【60岁老人年审】老来网app养老保险年