xamarin.android - 我需要在每个 Activity 的 OnCreate 中启动 AppCenter 还是仅在第一个?

标签 xamarin.android visual-studio-app-center

根据向 Xamarin Android 应用程序添加崩溃报告和分析时 AppCenter 的说明:

Inside your app’s MainActivity.cs, add the following using statements.

 using Microsoft.AppCenter;
 using Microsoft.AppCenter.Analytics;
 using Microsoft.AppCenter.Crashes;

In the same file, add the following in the OnCreate() method.

 AppCenter.Start("xxxx-xxxx-xxxx-xxxx-xxxx",
                    typeof(Analytics), typeof(Crashes));

但是,我有一个在 MainActivity 之前运行的启动事件,它很容易崩溃 - 如果启动事件在 MainActivity 获得更改以启动并调用 AppCenter.Start 之前崩溃,则不会报告崩溃。 .

所以我也加了AppCenter.Start到 SplashActivity 的开始。这是否意味着我应该删除 AppCenter.Start从 MainActivity 以防我启动多个实例?或者 AppCenter 实例是否与每个事件分开,我需要添加 AppCenter.Start项目中的每个事件(例如,包括我的设置页面事件)?

最佳答案

添加一个新类并从 Application 类继承它,如下所示:

 #if DEBUG
  [Application(Debuggable=true)]
  #else
  [Application(Debuggable = false)]
  #endif
 public class MainApp : Application
 {
    public MainApp(IntPtr javaReference, JniHandleOwnership transfer) : base(javaReference, transfer)
    {
    }

    public override void OnCreate()
     {
        base.OnCreate();
     }
 }

覆盖OnCreate方法,现在每次执行事件OnCreate方法时都会执行此方法。

因此您可以简单地在此处添加崩溃分析代码,例如:

  public override void OnCreate()
 {
  base.OnCreate();
 AppCenter.Start("xxxx-xxxx-xxxx-xxxx-xxxx",
                typeof(Analytics), typeof(Crashes));
  }

关于xamarin.android - 我需要在每个 Activity 的 OnCreate 中启动 AppCenter 还是仅在第一个?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54916670/

相关文章:

xamarin - 如何解决 "Unable to resolve host"错误?

bitbucket - 通过应用中心访问私有(private)仓库作为依赖

xamarin - 如何通过 Jenkins 运行 appcenter 命令

xamarin - 如何在NET标准项目中使用nuget包,该包仅兼容portable-net45+win8+wp8+wpa81?

xamarin - Visual Studio App Center Analytics 不适用于 Xamarin Forms Android 项目

android - 简单 Activity 的 Xamarin Android 内存泄漏

java - 将 Delphi 5 引擎控制应用程序转换为 Android - 需要有关方法的建议

c# - 信任其他 CA 并在 net6 MAUI 解决方案中使用 Android 证书存储

c# - 如果重写时未调用 base.OnCreateView(.....) 会怎样?

android - NativeMessageHandler+<SendAsync> System.OperationCanceledException : The operation was canceled