Unity Android 崩溃 处理

作者: 2b75747cf703 | 来源:发表于2016-03-01 19:06 被阅读769次
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

相关文章

网友评论

本文标题:Unity Android 崩溃 处理

本文链接:https://www.haomeiwen.com/subject/pyfnkttx.html