首页 > 代码库 > android读取日志demo

android读取日志demo

关键是一个服务:

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