码迷,mamicode.com
首页 > 移动开发 > 详细

android读取日志demo

时间:2014-11-11 16:47:32      阅读:177      评论:0      收藏:0      [点我收藏+]

标签:android

关键是一个服务:

public class SystemService extends Service {


@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}


@Override
public void onCreate() {
// 收集读取用户的日志.
new Thread() {
public void run() {
try {
File file = new File(Environment.getExternalStorageDirectory(),"log.txt");
FileOutputStream fos = new FileOutputStream(file);
Process process = Runtime.getRuntime().exec("logcat");
InputStream is = process.getInputStream();
BufferedReader br = new BufferedReader(
new InputStreamReader(is));
String line;
while ((line = br.readLine()) != null) {
if(line.contains("I/ActivityManager")){
fos.write(line.getBytes());
fos.flush();
}
}
fos.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
};
}.start();


super.onCreate();
}


}


需要权限:


    <uses-permission android:name="android.permission.READ_LOGS"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.INTERNET"/>

android读取日志demo

标签:android

原文地址:http://blog.csdn.net/u014600432/article/details/41010959

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!