我是靠谱客的博主 懵懂哑铃,这篇文章主要介绍PHP打印安卓log,在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());

}

}

当我们这个类写完之后,我们再把权限添加进去就可以了。

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

cfe5c94e316749b23c874378196b0f0a.png

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

2117938562117558727963ceef72eb9a.png

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

6f972489b2f6d8ba9aca356449b643d9.png

由于我开启了两次所以打印出了两次的log.

三、之后我们先创建页面,然后在按行读取Txt文本中的内容

首先我们开始编写XMl视图文件:

xmlns:tools="http://schemas.android.com/tools"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical"

tools:context=".MainActivity" >

android:layout_width="match_parent"

android:layout_weight="7"

android:orientation="vertical"

>

android:id="@+id/ListLog"

android:layout_width="match_parent"

android:layout_height="match_parent"

>

android:layout_width="match_parent"

android:layout_weight="1"

android:gravity="center"

android:orientation="horizontal" >

android:layout_gravity="center"

android:gravity="center"

android:id="@+id/BtnLog"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="清空日志"

/>

编写完成后,我们开始在MainActivity里面初始化我们的类

private ListView listView;

private Button btn;

listView = (ListView) findViewById(R.id.ListLog);

btn = (Button) findViewById(R.id.BtnLog);

之后,我们开始编写我们的读取TXT文件的方法

/**

* 根据行读取内容

* @return

*/

public List Txt() {

//将读出来的一行行数据使用List存储

String filePath = "/sdcard/Log.txt";

List newList=new ArrayList();

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

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 adapter = new ArrayAdapter(this, R.layout.log_list_item,Txt());

listView.setAdapter(adapter);

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

5b1afbc6927f09d6a16282f8a2af8cef.png

好了,我们的显示日志也已经成功了。接下来就是要可以清空日志;

最后、清空日志

如何清空日志呢?

其实非常简单

/**

* 删除Log文件

* @param fileName 文件路径和名称

*/

public static void delFile(String fileName){

File file = new File(fileName);

if(file.isFile()){

file.delete();

}

file.exists();

}

我们只需要把路径传过去,进行判断,如果有就直接删除。

然后我们对ListView进行刷新就可以了。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

最后

以上就是懵懂哑铃最近收集整理的关于PHP打印安卓log,在Android界面上显示和获取Logcat日志输出的方法的全部内容,更多相关PHP打印安卓log,在Android界面上显示和获取Logcat日志输出内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(72)

评论列表共有 0 条评论

立即
投稿
返回
顶部