Android logcat信息记录到手机文件

添加权限:

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

示例代码:

public class MyApp extends Application {

    private Process mLogProcess;

    @Override
    public void onCreate() {
        super.onCreate();
        try {
            int pid = android.os.Process.myPid();
            Log.d("MyApp", "PID is " + pid);
            Calendar calendar = Calendar.getInstance();
            String filename = String.format("/sdcard/MyApp_Log_%04d%02d%02d_%02d%02d%02d.txt",
                    calendar.get(Calendar.YEAR),
                    calendar.get(Calendar.MONTH),
                    calendar.get(Calendar.DAY_OF_MONTH),
                    calendar.get(Calendar.HOUR_OF_DAY),
                    calendar.get(Calendar.MINUTE),
                    calendar.get(Calendar.SECOND));
            mLogProcess = Runtime.getRuntime().exec(String.format("logcat -v time -f %s", filename));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onTerminate() {
        super.onTerminate();

        if(mLogProcess != null ){
            mLogProcess.destroy();
        }
    }
}


猜你喜欢

转载自blog.csdn.net/konga/article/details/70197030