For my Monodroid application, I'd like to do the following after an unhandled exception:
- Send the error to the server.
- Notify the user that the application has crashed (perhaps with a toast message).
- Exit the application gracefully.
I've implemented #1, but I'm struggling to implement #2 and #3.
Toast doesn't seem to be available after an unhandled exception, and I've been warned that it's a bad idea to exit an app on a user's behalf.
Can anyone point me in the right direction?
Here is my code:
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.
}
}
}