class my_thread extends Thread{ public void run(){
// 读取Logcat内容
try{
// 读取Logcat,写入log.txt
File file = new File("/sdcard/log.txt");
// 输出流
FileOutputStream fos = new FileOutputStream(file);
// 清空Logcat缓冲区
Runtime.getRuntime().exec("logcat -c");
// 获取Logcat所对应的进程
Process process = Runtime.getRuntime().exec("logcat");
// 获取该对象的输出流
InputStream is = process.getInputStream();
// 创建一个缓冲区,将Logcat中对应的数据存入缓存中
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
// 接收从缓冲区中读到的数据
String result = null;
// 循环读取Logcat对应的缓冲中的日志信息
while ((result = reader.readLine()) != null){
// 判断服务是否处于开启状态,如果关闭,需要将流对象关闭,
// 同时结束当前的onCreate方法(结束子线程)
if (!system_service.is_run){
// 关流
fos.close();
// 结束当前方法的执行,目的是要结束子线程的执行
return;
}
// 判断Logcat中是否包含"I/ActivityManager",
// 如果包含,则将其写入到文件中
// if (result.contains("I/ActivityManager")){
fos.write(result.getBytes());
fos.flush();
// }
// 使用完流对象后,关闭流
fos.close();
}
}
catch(Exception e)
{
e.printStackTrace();
}
}应该是将logcat的内容写到/sdcard/log.txt,运行后发现/sdcard/log.txt一片空白,什么都没有。
调试发现,运行到while ((result = reader.readLine()) != null)是就直接跳出循环了,就是说while循环根本没有执行,但是在DOS窗口输入adb logcat,又有很多行信息。
这是怎么回事呢?
// 读取Logcat内容
try{
// 读取Logcat,写入log.txt
File file = new File("/sdcard/log.txt");
// 输出流
FileOutputStream fos = new FileOutputStream(file);
// 清空Logcat缓冲区
Runtime.getRuntime().exec("logcat -c");
// 获取Logcat所对应的进程
Process process = Runtime.getRuntime().exec("logcat");
// 获取该对象的输出流
InputStream is = process.getInputStream();
// 创建一个缓冲区,将Logcat中对应的数据存入缓存中
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
// 接收从缓冲区中读到的数据
String result = null;
// 循环读取Logcat对应的缓冲中的日志信息
while ((result = reader.readLine()) != null){
// 判断服务是否处于开启状态,如果关闭,需要将流对象关闭,
// 同时结束当前的onCreate方法(结束子线程)
if (!system_service.is_run){
// 关流
fos.close();
// 结束当前方法的执行,目的是要结束子线程的执行
return;
}
// 判断Logcat中是否包含"I/ActivityManager",
// 如果包含,则将其写入到文件中
// if (result.contains("I/ActivityManager")){
fos.write(result.getBytes());
fos.flush();
// }
// 使用完流对象后,关闭流
fos.close();
}
}
catch(Exception e)
{
e.printStackTrace();
}
}应该是将logcat的内容写到/sdcard/log.txt,运行后发现/sdcard/log.txt一片空白,什么都没有。
调试发现,运行到while ((result = reader.readLine()) != null)是就直接跳出循环了,就是说while循环根本没有执行,但是在DOS窗口输入adb logcat,又有很多行信息。
这是怎么回事呢?
解决方案 »
- jquery mobile 固定导航栏会闪屏
- android中如何获得Imageview中图像的像素
- 求助,关于,Ksoap2-android访问web服务,上传文件
- Php 服务器端获取不到Android端发送的post数据!!!求解
- 关于背景图片的问题
- android布局如何在图片中间显示文件
- 如何获取Notification回传的数据。
- android中px,dp,dip和sp有什么不同?
- OkHttp3中Request导入时出错,请各位大神指点一下!!!
- android APP连接ESP8266,点击按钮断开socket连接时闪退
- GitHub里ActiveAndroid 项目 用于SQLite的ORM框架 表自动创建问题
- 请问 怎样 去 控制 双卡的 手机去用 C,网 或G 网拨号?
if (socket.isConnected()) {
if (!socket.isInputShutdown()) {
if ((result= in.readLine()) != null) {
result+= "\n";
} else {
}
}
}
}