Android Fabric - 以自定义间隔发送捕获的异常

Android Fabric - send Caught Exceptions at custom interval

根据 Fabric 文档 Fabric doc 为了减少用户流量,捕获的异常仅在应用启动时发送 -

Crashlytics processes exceptions on a dedicated background thread, so the performance impact to your app is minimal. To reduce your users’ network traffic, Crashlytics batches logged exceptions together and sends them the next time the app launches.

try {
  myMethodThatThrows();
} catch (Exception e) {
  Crashlytics.logException(e);
  // handle your exception here!
}

但对于这个特定的应用程序,该应用程序将始终处于打开状态,并且不会重新启动。所以问题是-

如何强制在一段时间后或在发生某些事件时发送日志?

这里是来自 Fabric 的迈克。

目前我们的 SDK 不提供从 运行 Android 应用手动刷新或推送日志的方法。记录的异常会在应用程序重新启动时发送。

我创建了某种简单的解决方案。现在报告会在 重新启动 应用后发送。如果用户因为 收到错误而不会重新启动应用程序 怎么办?有可能。

所以如果我们想在 关闭 应用后发送日志,我们需要创建简单的 "sticky" 服务。

public class ReportService extends Service {
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        System.err.println("ReportService refresh");
        return START_STICKY;
    }
}

让我们把 startService(new Intent(context, ReportService.class)); 行放在 onCreate main activity 方法中。

现在当应用程序关闭时,服务将再次启动代码 - Crashlytics 将发送记录的异常。