Display and retrieve Logcat log output on the Android interface
First, we need to get logs in Logcat
How to get it?
First we need to define a String [] array with the code
//The first one is Logcat, which is the log log we want to get.
//The second is - s, which means filtering.
//The third is that the type W we want to filter represents warm. We can also replace it with D: debug, I: info, E: error, and so on.
String[] running = new String[]{"logcat","-s","adb logcat *: W"};
When we set it up, we also need a process class. The function of this class is to use Java code for adb command line operation code.
Process exec = Runtime.getRuntime().exec(running);
Through the above methods, we can obtain and filter the methods in Logcat.
2. Next, I will start using IO stream for character manipulation and save the data in Android SDCard.
First, we define an InputStream.
final InputStream is = exec.getInputStream
Next, a thread is opened. The method in the thread is to read the data in Logcat through the IO stream, and then write the data into SDCard through the OutPutStream method.
new Thread() {
@Override
public void run() {
FileOutputStream os = null;
try {
//Create a new path information
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());
}
}
When we have finished writing this class, we can add permissions to it.
<! - Read Log permissions - > <uses-permission android:name="android.permission.READ_LOGS" /> <! - Create and delete file permissions in SDCD --> ___________. <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" /> <! - Write data permissions to SDCard - > <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <! - Read data permissions from SDCard - > <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
After adding permissions, let's run it for a try.
Then we open the file directory in our SDCard:
In this way, we have obtained the logs in Logcat (which can be compared with the console):
Because I opened it twice, I printed out the log twice.
Third, we create the page first, and then read the contents of Txt text by line.
First we started writing XMl view files:
<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="Clear log"
/>
</LinearLayout>
</LinearLayout>
After writing, we started initializing our class in MainActivity
private ListView listView;
private Button btn;
listView = (ListView) findViewById(R.id.ListLog);
btn = (Button) findViewById(R.id.BtnLog);
After that, we started writing our way to read TXT files.
/**
* Read content by line
* @return
*/
public List<String> Txt() {
//Use List to store a row of read data
String filePath = "/sdcard/Log.txt";
List newList=new ArrayList<String>();
try {
File file = new File(filePath);
int count = 0;//Initialize key value
if (file.isFile() && file.exists()) {//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 regular expressions
newList.add(count, reds);
count++;
}
}
isr.close();
br.close();
}else {
Log.e("tag", "can not find file");
}
} catch (Exception e) {
e.printStackTrace();
}
return newList;
}
Let's look at the code for d, which is actually the IO read-write operation.
if (file.isFile() && file.exists()) //This line is to determine whether a document exists or not.
Then we use InputStreamReader to read the files in our SDCard.
The BufferedReader method is used to read the character stream we acquired.
Finally, we use While loops and regular expressions to put each line into a List.
Finally, we return to 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 regular expressions
newList.add(count, reds);
count++;
}
}
There is also an XML view file named 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"
/>
Next, put the List in the ListView:
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.log_list_item,Txt());
listView.setAdapter(adapter);
So let's run it and see how it works.
Well, our display log has been successful. The next step is to clear the log.
Finally, clear the log
How to clear the log?
It's very simple.
/**
* Delete Log File
* @param fileName File path and name
*/
public static void delFile(String fileName){
File file = new File(fileName);
if(file.isFile()){
file.delete();
}
file.exists();
}
We just need to pass the path, make a judgment, and delete it directly if there is one.
Then we can refresh the ListView.