保存日志文本文件的设备时,关闭在Android
我的Android应用程序强制关闭某些时候它是在device.I运行时间应用力要存储错误日志在device.So一个文本文件,我可以看到背后原因力量关闭。因为当应用程序运行时很难识别何时发生突然的力量关闭。 你能否提供一些方法在SD卡上创建日志文件?保存日志文本文件的设备时,关闭在Android
在此先感谢。
我有以下代码时添加该代码的应用程序不强制关闭,和日志文件后的应用程序的开始,但它显示为空。
Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
@Override
public void uncaughtException(Thread thread, Throwable ex) {
Log.e("UNCAUGHT EXCEPTION", thread.toString());
Log.e("UNCAUGHT EXCEPTION", ""+ex.getMessage());
try {
PrintWriter out = new PrintWriter("mnt/sdcard/log.txt");
out.println(ex.getMessage());
out.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
});
力闭合屏幕是一款Android默认显示的指示,这不是处理在代码中出现错误或异常:你不能覆盖它,并防止它是要弄清楚,然后唯一的出路实际上处理您的代码中发生的异常。
有设置一个全球性的异常处理程序,这将赶上其在该线程造成任何异常的一种方式。所以你把它设置在你的主要活动中,它将适用于每个子活动。
final UncaughtExceptionHandler defaultHandler = Thread.getDefaultUncaughtExceptionHandler();
Thread.setDefaultUncaughtExceptionHandler(new UncaughtExceptionHandler() {
public void uncaughtException(Thread thread, Throwable ex) {
// get the crash info
//log it into the file
defaultHandler.uncaughtException(thread, ex);
}
});
编辑:
过这个传来:
您可以试着ACRA (Application Crash Report for Android)库:
ACRA是使Android应用程序自动发布他们的崩溃报告的GoogleDoc库形成。它的目标是android应用程序开发人员帮助他们在应用程序崩溃或行为错误时从其获取数据。
这很容易在你的应用程序安装,高度可配置的,不需要你随时随地托管服务器脚本...报告发送到谷歌文档电子表格!
还应考虑该Link
将Thread.setDefaultUncaughtExceptionHandler(new your.package.UncaughtExceptionHandler());
后从谷歌super.onCreate
我不想使用acra,因为它需要服务器setup.I只是想保持我的日志在文本文件。我的应用程序目前正处于发展阶段。 – Ravi 2014-09-10 19:21:21
尝试'Thread.setDefaultUncaughtExceptionHandler(new your.package.UncaughtExceptionHandler());'把'super.onCreate'放在主活动中 – 2014-09-10 19:22:43
你试过我发布的答案吗? – 2014-09-12 16:52:47
你可以尝试使用Thread.setDefaultUncaughtExceptionHandler并保存在SD卡上从那里异常信息。
您可以添加更多详细信息或代码示例吗? – lxg 2014-09-10 22:22:55
下载ghostlog应用play商店
它添加在lib文件夹:ghostlog - 集成 - 1.0.3。罐子
在mainfest:
<!--Receives intents from Ghost Log app to start & stop the integration service-->
<receiver android:name="com.readystatesoftware.ghostlog.integration.IntegrationReceiver"
android:permission="com.readystatesoftware.ghostlog.permission.READ_LOGS" >
<intent-filter>
<action android:name="com.readystatesoftware.ghostlog.integration.COMMAND" />
</intent-filter>
</receiver>
<!--Reads logs and broadcasts them to Ghost Log-->
<service android:name="com.readystatesoftware.ghostlog.integration.IntegrationService" />
看到此链接https://github.com/jgilfelt/GhostLog并感谢杰夫Gilfelt
我正在这样做,看看例外 – sunil 2014-09-10 19:07:38
的一种方式这将是更容易做到这一点:
下载从谷歌应用程序目录下载播放https://play.google.com/store/apps/details?id=com.nolanlawson.logcat&hl=en然后替换
e.printStackTrace();
与
Log.e("TAG FOR LOG e.g APP_NAME", Log.getStackTraceString(e));
之后,它会打印堆栈跟踪到的logcat可以通过执行以下操作中找到:使用任何你所做的标记搜索栏
打开目录下载应用程序,搜索在上面的日志中。
我只是想保持我的日志文本file.My应用程序目前正处于发展阶段 – Ravi 2014-09-10 19:21:57
请参阅我的更新问题 – Ravi 2014-09-10 18:57:22