MyApplication.java 3.24 KB
package com.hjx.personalcenter.activity;

import android.app.Application;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.Looper;
import android.view.Gravity;
import android.widget.TextView;
import android.widget.Toast;

import com.facebook.drawee.backends.pipeline.Fresco;
import com.hjx.personalcenter.R;
import com.hjx.personalcenter.crash.CrashHandler;
import com.hjx.personalcenter.util.ImageCache;
import com.squareup.leakcanary.LeakCanary;
import com.tencent.bugly.crashreport.CrashReport;

/**
 * Created by ${yf} on 2017/2/16.
 */

public class MyApplication extends Application {

 	private ImageCache mImageCache;
    private static Context context;
	private static MyApplication instance;
    @Override
    public void onCreate() {
        context = getApplicationContext();
        //c初始化内存检测
        LeakCanary.install(this);
        //初始化Fresco
        Fresco.initialize(this);
		mImageCache = new ImageCache();
        instance = this;
        CrashReport.initCrashReport(getApplicationContext(), "c2170557a0", false);
        CrashHandler crashHandler = CrashHandler.getInstance();
        //生成错误日志,上线的时候得放开
        //crashHandler.init(getApplicationContext());
        crashHandler.registerHandler(new CrashHandler.ExceptionHandler() {

            @Override
            public boolean handleCrash(final Context context, final Throwable ex) {
                new Thread(new Runnable() {

                    @Override
                    public void run() {
                        Looper.prepare();
                        System.out.println("bcz = " + ex.toString());
                        Toast toast = new Toast(context);
                        TextView tv = new TextView(context);
                        tv.setText(" 很抱歉,"+context.getString(R.string.app_name)+"崩溃了,我们会尽快修复.");
                        tv.setPadding(10, 10, 10, 10);
                        tv.setTextSize(20);
                        tv.setBackgroundResource(R.drawable.black_btn_default);
                        tv.setTextColor(context.getResources().getColor(android.R.color.white));
                        Drawable drawable= context.getResources().getDrawable(R.drawable.sorry);
                        drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
                        tv.setCompoundDrawables(drawable,null,null,null);
                        toast.setView(tv);
                        toast.setGravity(Gravity.CENTER, 0, 0);
                        toast.setDuration(Toast.LENGTH_LONG);
                        toast.show();
                        Looper.loop();
                    }
                }).start();
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                return false;
            }
        });
        super.onCreate();
    }
    public ImageCache getImageCache() {
        return mImageCache;
    }

    public static Context getContext() {
        return context;
    }
	public static MyApplication getInstance() {
        return instance;
    }

}