LoginSignup
7
6

More than 5 years have passed since last update.

[Android]ACRAを利用して好きなタイミングでクラッシュレポートを送信する

Posted at

参考:
https://github.com/ACRA/acra/wiki/AdvancedUsage

ACRAライブラリを利用することでクラッシュレポートの通知ができます。
ただ、ネットワーク環境がない状態でエラーが発生した場合の状況も入手したかったので、
その方法をメモで残そうと思います。

エラー発生時はエラー情報をファイルに保存するだけにし、
ネットワーク環境につながっているときに後から送信するという方法で実装しました。

  1. クラッシュレポート用のクラス作成
  2. ReportSenderの作成
  3. ACRAの設定

クラッシュレポート用のクラス作成

クラッシュレポートの内容をファイルに保存、読み込みを行うクラス
※Read,Write処理部分は省略

CrashReport.java
public class CrashReport {
    private static final String CRASH_REPORT_FILE = "crash_report.txt";

    private Context context;
    private StringBuilder report = new StringBuilder();
    public CrashReport(Context context) {
        this.context = context;
        // ロード処理
        .
        .
        .
        report.append(loadData);
    }

    public void add(CrashReportData errorContent) throws JSONReportBuilder.JSONReportException{
        // レポートを追加
        report.append("\n------------------------------------------------------------\n");
        report.append(errorContent.toJSON().toString().replaceAll("\\\\n", "\n").replaceAll("\\\\t", "\t"));
    }

    public void save() throws IOException{
       // レポートの保存
    }

    public void send(String email){
        // メールでクラッシュレポートを送信
        Intent intent = new Intent();
        intent.setType("message/rfc822");
        intent.setAction(Intent.ACTION_SENDTO);
        intent.setData(Uri.parse("mailto:" + email));
        intent.putExtra(Intent.EXTRA_SUBJECT, String.format("Crash report![%s]", context.getPackageName()));
        intent.putExtra(Intent.EXTRA_TEXT, report.toString());

        context.startActivity(intent);
    }

    public void clear(){
        // クラッシュレポートのクリア
        report = new StringBuilder();
        try {
            save();
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}

ReportSenderの作成

クラッシュしたときに動作するSenderクラスを作成

CrashReportSender.java
public class CrashReportSender implements ReportSender {

    @Override
    public void send(Context context, CrashReportData errorContent) throws ReportSenderException {
        CrashReport crashReport = new CrashReport(context);

        try {
            crashReport.add(errorContent);
            crashReport.save();
        }catch (Exception e){
            Log.e("CrashReportSender", "CrashReport save is failed.");
            e.printStackTrace();
        }
    }
}

ACRAの設定

最後にACRAを設定して完了!

AcraCrashReportApplication.java
@ReportsCrashes(
        mode= ReportingInteractionMode.TOAST,
        resToastText = R.string.acra_dialog_text,
        resDialogCommentPrompt= R.string.acra_dialog_comment_prompt // 発生状況を入力する欄のラベル
)
public class AcraCrashReportApplication extends Application {
    @Override
    public void onCreate() {
        // The following line triggers the initialization of ACRA
        super.onCreate();

        // ACRAの設定
        ACRA.init(this);
        CrashReportSender sender = new CrashReportSender();
        ACRA.getErrorReporter().setReportSender(sender);
    }
}
AndroidManifest.xml
android:name=lang:AcraCrashReportApplication

あとは、どこかの画面でCrashReportのsendを呼び出せばOKです。

7
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7
6