android - 在 Android 上登录到文件

标签 android logging

有没有办法从 Android 手机中检索日志消息。

我正在构建一个使用 HTC Hero 的 GPS 的应用程序。我可以从 Eclipse 运行和调试应用程序,但这不是 GPS 的一个很好的用例,坐在我的办公 table 前。

当我四处走动时启动应用程序时,我会遇到间歇性异常。无论如何我可以将这些异常输出到 SD 卡上的文本文件中,或者将对 Log.x("") 的调用输出到文本文件中,以便我可以看到异常是什么。

谢谢

编辑:解决方案

这是我最终使用的代码...

Thread.currentThread().setUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
    @Override
    public void uncaughtException(Thread thread, Throwable ex) {

    PrintWriter pw;
    try {
        pw = new PrintWriter(
                new FileWriter(Environment.getExternalStorageDirectory()+"/rt.log", true));
        ex.printStackTrace(pw);
        pw.flush();
        pw.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}
});

我不得不换行

pw = new PrintWriter(new FileWriter(Environment.getExternalStorageDirectory()+"/rt.log", true));

在 try/catch 中,因为 Eclipse 不允许我编译应用程序。它一直在说

Unhandled exception type IOException

1 quick fix
    Sorround with try/catch

所以我做到了,而且一切正常,这对我来说很好,但它确实让我想知道 Eclipse 是关于什么的......

最佳答案

您可以使用 Thread.setUncaughtExceptionHandler()捕获异常。

写入 SD 卡就像使用 Environment.getExternalStorageDirectory() 检索卡的目录一样简单并在那里创建一个文件。

File f = new File(Environment.getExternalStorageDirectory(),filename);

您需要通过将其添加到您的 list 中,为您的应用授予写入 SD 卡的正确权限:

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

关于android - 在 Android 上登录到文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2116260/

相关文章:

javascript - IBM Worklight 6.1 将客户端日志写入 messages.log

java - com.google.firebase.database.DatabaseException : Can't convert object of type java. lang.String 类型 com.test.consti.last.QuestionEntry

android - 在 Android 上禁用 "Bluetooth Permission Request"确认 - 使用 ACTION_REQUEST_DISCOVERABLE

android - Kotlin Android : How to pass ArrayList of Serializable objects through intent without unchecked cast warning?

java - R.Java 未显示

python - logging.info 和 logging.getLogger().info 有什么区别?

c++ - OS X 系统日志忽略 setlogmask 并且只记录错误

java - 开始记录 Tomcat 的最简单快捷的方法是什么?

java - log4j 属性 : LevelMatchFilter doesn't work

java - 在 setContentView(R.layout.activity_home_screen) 处获取 NullPointerException;