我是否需要在每个活动的 OnCreate 中启动 AppCenter,还是只需要在第一个活动中启动 AppCenter?



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

在您的应用程序的mainActivity.cs中,使用以下语句添加以下语句。

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

在同一文件中,在ongreate()方法中添加以下内容。

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

但是,我有一个溅起的活动,在主动行动之前运行,容易崩溃 - 如果MainActivity在MainActivity之前崩溃,则不会报告崩溃,并致电AppCenter.Start

因此,我还将AppCenter.Start添加到了飞溅性的开始。这是否意味着我应该从Main Activity中删除AppCenter.Start,以防我启动多个实例?还是AppCenter实例与每个活动分开,我需要将AppCenter.Start添加到项目中的每个活动(例如,包括我的设置页面活动)?

添加新类并从应用程序类中继承它:

 #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));
  }

最新更新