android hashmap 写入文件,android – ACRA 4.9.0:如何将ACRA报告写入文件(在应用程序数据文件夹中)...

这篇博客详细介绍了如何使用Acra4.9.0在Android应用中捕获崩溃报告并将其写入文本文件。开发者创建了一个名为MyOwnSender的自定义发送器,将崩溃报告数据写入到名为'AcraReport.txt'的文件中,包含了ReportID、DeviceID、AppVersionName等关键信息。同时,应用中还配置了ACRA以静默方式发送报告。
摘要由CSDN通过智能技术生成

我想使用最新的Acra 4.9.0在文本文件中编写崩溃报告.

我可以举例说明这个最新版本.

我尝试使用可用的文档.

Acra已启用

但它,不是写在文件中.

myApp

package com.myApp;

import org.acra.ACRA;

import android.app.AlertDialog;

import android.os.Bundle;

import android.support.v4.app.FragmentActivity;

import android.view.KeyEvent;

import android.view.View;

import com.myApp.Application.AppLauncher;

import com.myApp.interfaces.AppEvents;

import com.myApp.R;

import com.utils.Config;

import com.utils.Constants;

import com.utils.DeviceValidator;

public class myApp extends FragmentActivity

{

private AppLauncher appLauncher = null;

@Override

protected void onCreate(Bundle savedInstanceState)

{

super.onCreate(savedInstanceState);

if(!ACRA.isACRASenderServiceProcess())

{

setContentView(R.layout.activity_myApp);

appLauncher = new AppLauncher();

appLauncher.registerEventListener(appEventsListener);

appLauncher.initApp(this);

}

}

@Override

public void onPause() {

super.onPause();

if(!DeviceValidator.isDeviceValid())

{

return;

}

appLauncher.activityOnPause();

}

@Override

protected void onRestart() {

super.onRestart();

}

@Override

protected void onStart()

{

super.onStart();

}

@Override

public void onResume()

{

super.onResume();

appLauncher.activityOnResume();

}

}

AcraApplication

package com.myAPP;

import org.acra.ACRA;

import org.acra.ReportField;

import org.acra.ReportingInteractionMode;

import org.acra.annotation.ReportsCrashes;

import org.acra.sender.HttpSender.Method;

import android.app.Application;

@ReportsCrashes(

formUri = "http://staging.jemtv.com/variable_dump/index.php",

customReportContent = { ReportField.REPORT_ID, ReportField.DEVICE_ID, ReportField.APP_VERSION_NAME, ReportField.ANDROID_VERSION, ReportField.STACK_TRACE, ReportField.CUSTOM_DATA, ReportField.LOGCAT },

httpMethod = Method.POST,

reportSenderFactoryClasses = org.acra.util.MyOwnSenderFactory.class,

mode = ReportingInteractionMode.SILENT

)

public class AcraApplication extends Application

{

public AcraApplication()

{

super();

}

@Override

public void onCreate() {

super.onCreate();

ACRA.init(this);

}

}

MyOwnSender

package org.acra.util;

import java.io.File;

import java.io.FileOutputStream;

import org.acra.ReportField;

import org.acra.collector.CrashReportData;

import org.acra.config.ACRAConfiguration;

import org.acra.sender.ReportSender;

import org.acra.sender.ReportSenderException;

import android.content.Context;

import android.support.annotation.NonNull;

import android.util.Log;

public class MyOwnSender implements ReportSender {

private static final String FILE_NAME = "AcraReport.txt";

//private final Map mMapping = new HashMap();

//private FileWriter crashReport = null;

MyOwnSender(Context context, @NonNull ACRAConfiguration config)

{

Log.d("testAcra", "MyOwnSender created");

/* File logFile = new File(context.getFilesDir().getPath() + "/" + FILE_NAME, FILE_NAME);

try {

crashReport = new FileWriter(logFile, true);

} catch (IOException e) {

e.printStackTrace();

}*/

}

@Override

public void send(Context context, CrashReportData report) throws ReportSenderException

{

// Iterate over the CrashReportData instance and do whatever

// you need with each pair of ReportField key / String value

String finalReport = createCrashReport(report);

String tempFile = context.getFilesDir().getPath() + "/" + FILE_NAME;

try

{

File detailedFile = new File(tempFile);

if(!detailedFile.exists())

detailedFile.createNewFile();

FileOutputStream stream = new FileOutputStream(detailedFile, true);

stream.write(finalReport.getBytes());

Log.d("testAcra","adding to file: "+stream);

stream.close();

}

catch (Exception e)

{

e.printStackTrace();

}

/*final Map finalReport = remap(report);

try {

BufferedWriter buf = new BufferedWriter(crashReport);

Set> set = reportBody.entrySet();

Iterator> i = set.iterator();

while (i.hasNext()) {

Map.Entry me = (Entry) i.next();

buf.append("[" + me.getKey() + "]=" + me.getValue());

}

buf.flush();

buf.close();

} catch (IOException e) {

Log.e("TAG", "IO ERROR", e);

}*/

}

private String createCrashReport(CrashReportData crashReportData){

StringBuilder body = new StringBuilder();

body.append("ReportID : " + crashReportData.getProperty(ReportField.REPORT_ID))

.append("\n")

.append("DeviceID : " + crashReportData.getProperty(ReportField.DEVICE_ID))

.append("\n")

.append("AppVersionName : " + crashReportData.getProperty(ReportField.APP_VERSION_NAME))

.append("\n")

.append("Android Version : " + crashReportData.getProperty(ReportField.ANDROID_VERSION))

.append("\n")

.append("CustomData : " + crashReportData.getProperty(ReportField.CUSTOM_DATA))

.append("\n")

.append("STACK TRACE : \n" + crashReportData.getProperty(ReportField.STACK_TRACE))

.append("\n")

.append("LogCAT : \n" + crashReportData.getProperty(ReportField.LOGCAT));

return body.toString();

}

/* private Map remap(Map report) {

Setfields = ACRA.getConfig().getReportFields();

final Map finalReport = new HashMap(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;

}*/

}

MyOwnSenderFactory

package org.acra.util;

import org.acra.config.ACRAConfiguration;

import org.acra.sender.ReportSender;

import org.acra.sender.ReportSenderFactory;

import android.content.Context;

import android.support.annotation.NonNull;

public class MyOwnSenderFactory implements ReportSenderFactory {

// NB requires a no arg constructor.

/*MyOwnSenderFactory()

{

Log.e("testAcra", "MyOwnSenderFactory created");

}*/

@Override

@NonNull

public ReportSender create(@NonNull Context context, @NonNull ACRAConfiguration config) {

// TODO Auto-generated method stub

return new MyOwnSender(context, config);

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值