根据向 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/