我可以通过处理未捕获的异常来使用ACRA库来管理强制关闭错误.该报告可以成功发送到谷歌文档,电子邮件和自定义Web服务..
但是我想要的……
>我如何将报告写入文件[例如. sdcard / myapp / myLog.txt]?
为什么我要这个..
>我的应用程序用户可能在强制关闭时没有互联网连接..如果是,那么我将错过报告,如果我将报告写入文件然后我可以在互联网连接可用时发送到我的服务器.
解决方法
我想您想要实现的目标已经由ACRA完成.这是我在abd logcat中看到的内容:
01-23 12:15:28.056: D/ACRA(614): Writing crash report file. 01-23 12:15:28.136: D/ACRA(614): Mark all pending reports as approved. 01-23 12:15:28.136: D/ACRA(614): Looking for error files in /data/data/com.ybi/files 01-23 12:15:28.136: V/ACRA(614): About to start ReportSenderWorker from #handleException 01-23 12:15:28.146: D/ACRA(614): Add user comment to null 01-23 12:15:28.146: D/ACRA(614): #checkAndSendReports - start 01-23 12:15:28.146: D/ACRA(614): Looking for error files in /data/data/com.ybi/files
ACRA所做的第一件事就是在应用程序的内部存储上创建一个文件报告.
然后,如果您在线并且错误报告器已正确初始化,则会发送报告.
否则,报告将保留在数据存储中(以便稍后发送).
我没有查看数据,但我正在研究自定义记录器.因此,如果您想要做与ACRA相同的事情,那很简单:
ACRA.init(this); // a custom reporter for your very own purposes ErrorReporter.getInstance().setReportSender(new LocalReportSender(this));
然后 :
import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStreamWriter; import java.util.HashMap; import java.util.Iterator; import java.util.Map; import java.util.Set; import org.acra.ACRA; import org.acra.CrashReportData; import org.acra.ReportField; import org.acra.sender.ReportSender; import org.acra.sender.ReportSenderException; import android.content.Context; import de.akquinet.android.androlog.Log; public class LocalReportSender implements ReportSender { private final Map<ReportField,String> mMapping = new HashMap<ReportField,String>() ; private FileOutputStream crashReport = null; public LocalReportSender(Context ctx) { // the destination try { crashReport = ctx.openFileOutput("crashReport",Context.MODE_WORLD_READABLE); } catch (FileNotFoundException e) { Log.e("TAG","IO ERROR",e); } } @Override public void send(CrashReportData report) throws ReportSenderException { final Map<String,String> finalReport = remap(report); try { OutputStreamWriter osw = new OutputStreamWriter(crashReport); Set set = finalReport.entrySet(); Iterator i = set.iterator(); while (i.hasNext()) { Map.Entry<String,String> me = (Map.Entry) i.next(); osw.write("[" + me.getKey() + "]=" + me.getValue()); } osw.flush(); osw.close(); } catch (IOException e) { Log.e("TAG",e); } } private static boolean isNull(String aString) { return aString == null || ACRA.NULL_VALUE.equals(aString); } private Map<String,String> remap(Map<ReportField,String> report) { ReportField[] fields = ACRA.getConfig().customreportContent(); if (fields.length == 0) { fields = ACRA.DEFAULT_REPORT_FIELDS; } final Map<String,String> finalReport = new HashMap<String,String>( report.size()); for (ReportField field : fields) { if (mMapping == null || mMapping.get(field) == null) { finalReport.put(field.toString(),report.get(field)); } else { finalReport.put(mMapping.get(field),report.get(field)); } } return finalReport; } }
我还没有完全测试它,但你明白了.希望能帮助到你.