在Android界面上顯示和獲取Logcat日志輸出的方法
一、首先我們要獲取Logcat中的日志
如何獲取呢?
首先我們要先定義一個(gè)String[]數(shù)組,里面的代碼是
//第一個(gè)是Logcat ,也就是我們想要獲取的log日志
//第二個(gè)是 -s 也就是表示過(guò)濾的意思
//第三個(gè)就是 我們要過(guò)濾的類型 W表示warm ,我們也可以換成 D :debug, I:info,E:error等等
String[] running = new String[]{"logcat","-s","adb logcat *: W"};
當(dāng)我們?cè)O(shè)置好之后,我們還需要一個(gè)process類,作用通俗來(lái)講就是用Java代碼來(lái)進(jìn)行adb命令行操作代碼是:
Process exec = Runtime.getRuntime().exec(running);
通過(guò)以上的方法我們就可以獲得和過(guò)濾Logcat中的方法。
二、接下來(lái)開始使用IO流進(jìn)行字符操作,把數(shù)據(jù)保存在Android SDCard中
首先:我們定義一個(gè)InputStream,
final InputStream is = exec.getInputStream
接下來(lái)開啟一個(gè)線程,線程中的方法就是通過(guò)IO流先讀取Logcat中的數(shù)據(jù),然后再把數(shù)據(jù)通過(guò)OutPutStream方法寫入到SDCard中。
new Thread() {
@Override
public 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());
}
}
當(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" />
添加完權(quán)限,我們運(yùn)行試試。

然后我們?cè)俅蜷_我們的SDCard中的文件目錄:

這樣我們就已經(jīng)獲取到了Logcat中的日志(可以和控制臺(tái)的對(duì)比一下):

由于我開啟了兩次所以打印出了兩次的log.
三、之后我們先創(chuàng)建頁(yè)面,然后在按行讀取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>
<LinearLayout
android: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文件的方法
/**
* 根據(jù)行讀取內(nèi)容
* @return
*/
public List<String> Txt() {
//將讀出來(lái)的一行行數(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;
}
我們看d的代碼,其實(shí)也就是IO讀寫操作
if (file.isFile() && file.exists()) //這一行是判斷是否有文件存在
然后我們用InputStreamReader讀取我們SDCard中的文件;
使用BufferedReader方法讀取我們獲取的字符流;
最后我們用While循環(huán)和正則表達(dá)式來(lái)把每一行都給放入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++;
}
}
還有一個(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" />
接下來(lái)就是把List放入ListView中:
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.log_list_item,Txt()); listView.setAdapter(adapter);
好讓我們運(yùn)行一下看看效果:

好了,我們的顯示日志也已經(jīng)成功了。接下來(lái)就是要可以清空日志;
最后、清空日志
如何清空日志呢?
其實(shí)非常簡(jiǎn)單
/**
* 刪除Log文件
* @param fileName 文件路徑和名稱
*/
public static void delFile(String fileName){
File file = new File(fileName);
if(file.isFile()){
file.delete();
}
file.exists();
}
我們只需要把路徑傳過(guò)去,進(jìn)行判斷,如果有就直接刪除。
然后我們對(duì)ListView進(jìn)行刷新就可以了。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android adb logcat 命令查看日志詳細(xì)介紹
- Android shell命令行中過(guò)濾adb logcat輸出的幾種方法
- Android開發(fā)之在程序中時(shí)時(shí)獲取logcat日志信息的方法(附demo源碼下載)
- Android Studio使用小技巧:自定義Logcat
- logcat命令使用方法和查看android系統(tǒng)日志緩沖區(qū)內(nèi)容的方法
- android真機(jī)調(diào)試時(shí)無(wú)法顯示logcat信息的解決方法介紹
- Android開發(fā)筆記之:一分鐘學(xué)會(huì)使用Logcat調(diào)試程序的詳解
- 如何通過(guò)Android Logcat插件分析firebase崩潰問(wèn)題
相關(guān)文章
Android 多線程的實(shí)現(xiàn)方法總結(jié)
這篇文章主要介紹了Android 多線程的實(shí)現(xiàn)方法總結(jié)的相關(guān)資料,這里提供三種方法,幫助大家掌握這部分內(nèi)容,需要的朋友可以參考下2017-08-08
Android端使用Modbus協(xié)議的簡(jiǎn)單方法
Modbus協(xié)議是全球第一個(gè)用于工業(yè)現(xiàn)場(chǎng)的總線協(xié)議,與外設(shè)交互可以采用串口通信,tcp等方式,這篇文章主要給大家介紹了關(guān)于Android端使用Modbus協(xié)議的簡(jiǎn)單方法,需要的朋友可以參考下2021-11-11
Android中recyclerView底部添加透明漸變效果
這篇文章主要給大家介紹了關(guān)于Android中recyclerView如何實(shí)現(xiàn)底部添加透明漸變效果的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)各位Android開發(fā)者們具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起看看吧。2018-04-04
詳解Android?Flutter如何使用相機(jī)實(shí)現(xiàn)拍攝照片
在app中使用相機(jī)肯定是再平常不過(guò)的一項(xiàng)事情了,相機(jī)肯定涉及到了底層原生代碼的調(diào)用,那么在flutter中如何快速簡(jiǎn)單的使用上相機(jī)的功能呢?一起來(lái)看看吧2023-04-04
Android ListView實(shí)現(xiàn)下拉頂部圖片變大效果
這篇文章主要為大家詳細(xì)介紹了Android ListView實(shí)現(xiàn)下拉頂部圖片變大,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-12-12
Android實(shí)現(xiàn)從本地圖庫(kù)/相機(jī)拍照后裁剪圖片并設(shè)置頭像
玩qq或者是微信的盆友都知道,這些聊天工具里都要設(shè)置頭像,一般情況下大家的解決辦法是從本地圖庫(kù)選擇圖片或是從相機(jī)拍照,然后根據(jù)自己的喜愛截取圖片,接下來(lái)通過(guò)本文給大家介紹Android實(shí)現(xiàn)從本地圖庫(kù)/相機(jī)拍照后裁剪圖片并設(shè)置頭像,需要的朋友參考下2016-02-02

