登录到 Android 上的文件 [英] Logging to a file on Android

查看:18
本文介绍了登录到 Android 上的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

谢谢

解决方案

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

Thread.currentThread().setUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {@覆盖public void uncaughtException(Thread thread, Throwable ex) {PrintWriter 密码;尝试 {pw = 新的 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 不允许我编译应用程序.它一直在说

<块引用>

未处理的异常类型 IOException1 快速修复尝试/捕获

所以我做了并且一切正常,这对我来说很好,但它确实让我想知道 Eclipse 在做什么......

解决方案

你可以使用 Thread.setUncaughtExceptionHandler() 来捕捉异常.

写入 SD 卡就像使用 Environment.getExternalStorageDirectory() 并在那里创建一个文件.

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

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

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

Is there any way of retrieving log messages from an Android handset.

I'm building an application which uses the GPS of my HTC Hero. I can run and debug the application from eclipse but this isn't a good use case of GPS, sat at my desk.

When I fire the app up when I am walking around, I get an intermittent exception. Is there anyway I can output these exceptions to a text file on the SD card or output calls to Log.x("") to a text file so that I can see what the exception is.

Thanks

EDIT : Solution

Here is the code I finally went with...

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();
    }
}
});

I had to wrap the line

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

in a try/catch as Eclipse would not let me compile the app. It kept saying

Unhandled exception type IOException

1 quick fix
    Sorround with try/catch

So I did and it all works which is fine by me but it does make me wonder what Eclipse was on about...

解决方案

You could use Thread.setUncaughtExceptionHandler() to catch the Exceptions.

Writing to SD Card is as simple as retrieving the directory for the card using Environment.getExternalStorageDirectory() and creating a file there.

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

You will need to give you app the correct permission to write to the SD Card by adding this to your Manifest:

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

这篇关于登录到 Android 上的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆