1

对于我的 Monodroid 应用程序,我想在未处理的异常之后执行以下操作:

  1. 将错误发送到服务器。
  2. 通知用户应用程序已崩溃(可能使用 toast 消息)。
  3. 优雅地退出应用程序。

我已经实现了#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.
        }
    }
}
4

1 回答 1

2

不确定上下文Context指的是什么:

Toast.MakeText(Context, "ERROR", ToastLength.Short).Show();

你可以试试

Toast.MakeText(YourMainApplication.this, "ERROR", ToastLength.Short).Show();

反而。

于 2013-07-16T21:58:46.797 回答