package com.sinyee.babybus.listeners;
public interface UncaughtExceptionListener
{
void uncaughtException(String message, String stackTrace);
}
package com.sinyee.babybus.talk2kiki;
import java.lang.Thread.UncaughtExceptionHandler;
import com.sinyee.babybus.listeners.UncaughtExceptionListener;
import android.app.Application;
import android.util.Log;
import org.apache.commons.lang3.exception.ExceptionUtils;
public class MyApplication extends Application
{
private static MyApplication instance;
private final String TAG = "MyApplication";
private UncaughtExceptionListener uncaughtExceptionListener;
private UncaughtExceptionHandler defaultUncaughtExceptionHandler;
public MyApplication()
{
super();
instance = this;
defaultUncaughtExceptionHandler = Thread.getDefaultUncaughtExceptionHandler();
// setup handler for uncaught exception
Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
@Override
public void uncaughtException(Thread thread, Throwable ex)
{
if(uncaughtExceptionListener != null)
{
String message = ex.getMessage();
String stackTrace = ExceptionUtils.getStackTrace(ex);
Log.e(TAG, "uncaughtException message " + message);
Log.e(TAG, "uncaughtException stackTrace " + stackTrace);
uncaughtExceptionListener.uncaughtException(message, stackTrace);
}
defaultUncaughtExceptionHandler.uncaughtException(thread, ex);
}
});
}
public static void setUncaughtExceptionListener(UncaughtExceptionListener listener)
{
instance.uncaughtExceptionListener = listener;
}
}
using UnityEngine;
#if UNITY_ANDROID
public class UncaughtExceptionListener : AndroidJavaProxy
{
public UncaughtExceptionListener() : base("com.sinyee.babybus.listeners.UncaughtExceptionListener") { }
public virtual void uncaughtException(string message, string stackTrace)
{
Debug.Log("UncaughtExceptionListener uncaughtException");
}
}
#endif
using UnityEngine;
using System;
#if UNITY_ANDROID
public class MyApplication
{
public static void SetUncaughtExceptionListener(AndroidJavaProxy uncaughtExceptionListener)
{
if (Application.platform != RuntimePlatform.Android)
return;
new AndroidJavaClass("com.sinyee.babybus.talk2kiki.MyApplication").CallStatic("setUncaughtExceptionListener", uncaughtExceptionListener);
}
}
#endif
网友评论