在Android中存储应用程序活动日志

时间:2011-04-19 09:43:15

标签: android logging data-storage

我想记录我的应用程序活动的一些数据,以便在系统中读取它。数据包括按下按钮时收集的字符串,浮点数等。这样做的最佳方式是什么?

2 个答案:

答案 0 :(得分:1)

我最终编写了自己的登录设备功能。

public static BufferedWriter out;
    private void createFileOnDevice(Boolean append) throws IOException {
            /*
             * Function to initially create the log file and it also writes the time of creation to file.
             */
            File Root = Environment.getExternalStorageDirectory();
            if(Root.canWrite()){
                 File  LogFile = new File(Root, "Log.txt");
                 FileWriter LogWriter = new FileWriter(LogFile, append);
                 out = new BufferedWriter(LogWriter);
                 Date date = new Date();
                 out.write("Logged at" + String.valueOf(date.getHours() + ":" + date.getMinutes() + ":" + date.getSeconds() + "\n"));
            }
        }

public void writeToFile(String message){
        try {
            out.write(message+"\n");
        } catch (IOException e) {
            e.printStackTrace();
        }

关于SO的相关问题是here

答案 1 :(得分:0)

Logcollector是您的选择,但您需要先在设备上安装它。

您可以再次通过adb保存活动日志

从命令提示符运行adb shell logcat > your_log_file.txt