软件编程
位置:首页>> 软件编程>> Android编程>> 在Android界面上显示和获取Logcat日志输出的方法

在Android界面上显示和获取Logcat日志输出的方法

作者:死磕自己  发布时间:2023-02-19 20:02:19 

标签: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() {
       @Override
       public 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" />

添加完权限,我们运行试试。

在Android界面上显示和获取Logcat日志输出的方法

然后我们再打开我们的SDCard中的文件目录:

在Android界面上显示和获取Logcat日志输出的方法

这样我们就已经获取到了Logcat中的日志(可以和控制台的对比一下):

在Android界面上显示和获取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>

<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文件的方法


/**
  * 根据行读取内容
  * @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);

好让我们运行一下看看效果:

在Android界面上显示和获取Logcat日志输出的方法

来源:https://www.jianshu.com/p/9e7961221862

0
投稿

猜你喜欢

手机版 软件编程 asp之家 www.aspxhome.com