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,又有很多行信息。
这是怎么回事呢?

解决方案 »

  1.   

    while (true) {  
                    if (socket.isConnected()) {  
                        if (!socket.isInputShutdown()) {  
                            if ((result= in.readLine()) != null) {  
                                result+= "\n";  
                                
                            } else {  
      
                            }  
                        }  
                    }  
                }  
      

  2.   

    先打印出reader里面的长度,看看是否为0,如果为0,说明这个BufferedReader构造的对象出了问题,依次往上排查。