对于我的Mondroid应用程序,我想在出现未处理的异常后执行以下操作:
- 将错误发送到服务器
- 通知用户应用程序已崩溃(可能带有toast消息)
- 优雅地退出应用程序
我已经实现了#1,但我正在努力实现#2和#3。
Toast在出现未经处理的异常后似乎不可用,有人警告我,代表用户退出应用程序是个坏主意。
有人能给我指正确的方向吗?
这是我的代码:
using System;
using Android.App;
using Android.Runtime;
using Android.Widget;
namespace MyAppsNamespace
{
[Application]
public class MyApplication : Application
{
public static MyApplication Current { get; private set; }
public MyApplication (IntPtr handle, global::Android.Runtime.JniHandleOwnership transfer) : base(handle, transfer)
{
Current = this;
}
public override void OnCreate()
{
base.OnCreate();
AndroidEnvironment.UnhandledExceptionRaiser += (sender, args) => LogException(args.Exception);
}
public static void LogException(Exception exception)
{
var phoneId = Guid.NewGuid(); // just for testing purposes
var client = AppConfig.ErrorLoggingServiceClient;
var response = client.Send<ErrorLoggingResponse>(new ErrorLoggingEntry
{
PhoneId = phoneId,
ErrorTime = DateTime.UtcNow,
Message = exception.Message,
StackTrace = exception.StackTrace
}); // This works fine (i.e. I've implemented #1)
Toast.MakeText(Context, String.Format("An error occurred. Please call Prod Support at 1-800-555-1212. [Phone Id: '{0}']", phoneId), ToastLength.Short).Show(); // This has no impact.
}
}
}
不确定Context
在中指的是什么上下文
Toast.MakeText(Context, "ERROR", ToastLength.Short).Show();
你可以试试
Toast.MakeText(YourMainApplication.this, "ERROR", ToastLength.Short).Show();
相反。