package com.uiui.os.base;

import android.annotation.SuppressLint;
import android.app.Application;
import android.content.Context;
import android.os.Handler;
import android.os.Looper;
import android.util.Log;

import com.qweather.sdk.view.HeConfig;
import com.uiui.os.utils.AmapManager;


public class BaseApplication extends Application {
    private static final String TAG = BaseApplication.class.getSimpleName();

    @SuppressLint("StaticFieldLeak")
    public static Context context;
    @SuppressLint("StaticFieldLeak")
    private static BaseApplication instance;

    public static Context getAppContext() {
        return context;
    }

    // 单例模式中获取唯一的ExitApplication实例
    public static BaseApplication getInstance() {
        if (null == instance) {
            instance = new BaseApplication();
        }
        return instance;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        context = this;
        HeConfig.init("HE2111041506381545", "32b5ec69545e44119583a5e0ed4e87df");
        AmapManager.init(this);
    }


    private void catchException() {
        Thread.setDefaultUncaughtExceptionHandler(
                new Thread.UncaughtExceptionHandler() {
                    @Override
                    public void uncaughtException(Thread t, Throwable e) {
                        Log.e("捕获异常子线程:", Thread.currentThread().getName() +
                                "在:" + e.getStackTrace()[0].getClassName());
                    }
                }
        );
        //下面是新增方法!
        new Handler(Looper.getMainLooper()).post(new Runnable() {
            @Override
            public void run() {
                while (true) {
                    try {
                        Looper.loop(); //会先执行这个方法,然后在执行下面的异常捕获方法!
                    } catch (Exception e) {
                        Log.e("捕获异常主线程:", Thread.currentThread().getName() + "在:" + e.getStackTrace()[0].getClassName());
                        e.printStackTrace();
                    }
                }
            }
        });
    }


}
