我是否需要在每个Activity的OnCreate或第一个中启动AppCenter?

问题描述 投票:0回答:1

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

在应用程序的MainActivity.cs中,添加以下using语句。

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

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

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

但是,我有一个在MainActivity之前运行的易于崩溃的启动活动 - 如果在MainActivity开始更改并调用AppCenter.Start之前,启动活动崩溃,则不会报告崩溃。

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

xamarin.android visual-studio-app-center
1个回答
1
投票

添加一个新类并从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));
  }
© www.soinside.com 2019 - 2024. All rights reserved.