137 lines
5.1 KiB
Java
137 lines
5.1 KiB
Java
package com.ttstd.dialer.base;
|
||
|
||
import android.annotation.SuppressLint;
|
||
import android.app.Application;
|
||
import android.content.Context;
|
||
import android.os.Build;
|
||
import android.os.Handler;
|
||
import android.os.Looper;
|
||
import android.util.Log;
|
||
|
||
import com.alibaba.android.arouter.launcher.ARouter;
|
||
import com.arialyy.aria.core.Aria;
|
||
import com.hjq.toast.Toaster;
|
||
import com.tencent.bugly.crashreport.CrashReport;
|
||
import com.tencent.mmkv.MMKV;
|
||
import com.ttstd.dialer.BuildConfig;
|
||
import com.ttstd.dialer.manager.AppManager;
|
||
import com.ttstd.dialer.manager.MapManager;
|
||
import com.ttstd.dialer.manager.WeatherManager;
|
||
import com.ttstd.dialer.utils.Logger;
|
||
import com.ttstd.dialer.utils.SystemUtils;
|
||
import com.ttstd.iconloader.IconCacheManager;
|
||
|
||
import cn.jiguang.api.JCoreInterface;
|
||
import cn.jiguang.api.utils.JCollectionAuth;
|
||
import cn.jpush.android.api.JPushInterface;
|
||
|
||
|
||
public class BaseApplication extends Application {
|
||
private static final String TAG = "BaseApplication";
|
||
|
||
/**
|
||
* ViewModel中因为经常旋转导致弱引用为空
|
||
*/
|
||
@SuppressLint("StaticFieldLeak")
|
||
private static Context context;
|
||
|
||
public static Context getContext() {
|
||
return context;
|
||
}
|
||
|
||
@Override
|
||
public void onCreate() {
|
||
super.onCreate();
|
||
Log.e(TAG, "onCreate: ");
|
||
context = getApplicationContext();
|
||
if (!BuildConfig.DEBUG) {
|
||
catchException();
|
||
}
|
||
// 在开始分析的地方调用,传入路径
|
||
// 如果是放到外部路径,需要添加权限
|
||
// 默认存储在/sdcard/Android/data/packagename/files
|
||
// Debug.startMethodTracing("App" + System.currentTimeMillis());
|
||
init();
|
||
}
|
||
|
||
private void init() {
|
||
Log.e(TAG, "init: ");
|
||
if (SystemUtils.isMainProcessName(this, android.os.Process.myPid())) {
|
||
Logger.initialize(this, BuildConfig.DEBUG);
|
||
Logger.setLogLevel(Logger.LogLevel.DEBUG); // 开发阶段记录所有日志
|
||
|
||
String rootDir = MMKV.initialize(this);
|
||
Log.e(TAG, "mmkv root: " + rootDir);
|
||
|
||
/*jpush start*/
|
||
JPushInterface.setDebugMode(true);
|
||
// 调整点一:调用启用推送业务功能代码前增加setAuth调用
|
||
boolean isPrivacyReady = true; // app根据是否已弹窗获取隐私授权来赋值
|
||
if (!isPrivacyReady) {
|
||
// JCore 5.0.4之前版本需要显式设置false
|
||
if (JCoreInterface.getJCoreSDKVersionInt() < 504) { // 5.0.4版本号对应504
|
||
JCollectionAuth.setAuth(context, false);
|
||
}
|
||
// 所有版本在未授权时都不应初始化SDK
|
||
return;
|
||
}
|
||
JPushInterface.init(this);
|
||
JPushInterface.setAlias(this, 0, SystemUtils.getSerial());
|
||
|
||
// 调整点二:App用户同意了隐私政策授权,并且开发者确定要开启推送服务后调用
|
||
// JCore 5.0.4+会自动处理授权状态,可不需要显式设置true
|
||
JCollectionAuth.setAuth(context, true);
|
||
/*jpush end*/
|
||
|
||
if (BuildConfig.DEBUG) { // 这两行必须写在init之前,否则这些配置在init过程中将无效
|
||
ARouter.openLog(); // 打印日志
|
||
ARouter.openDebug(); // 开启调试模式(如果在InstantRun模式下运行,必须开启调试模式!线上版本需要关闭,否则有安全风险)
|
||
}
|
||
ARouter.init(this); // 尽可能早,推荐在Application中初始化
|
||
|
||
// 初始化 Toast 框架
|
||
Toaster.init(this);
|
||
|
||
Log.e(TAG, "slowInit: ");
|
||
Aria.init(this);
|
||
CrashReport.initCrashReport(getApplicationContext(), "845e3ed68c", false);
|
||
CrashReport.setDeviceId(this, Build.MODEL);
|
||
xcrash.XCrash.init(this);
|
||
|
||
AppManager.init(this);
|
||
MapManager.init(this);
|
||
MapManager.getInstance().initMap();
|
||
WeatherManager.init(this);
|
||
IconCacheManager.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();
|
||
}
|
||
}
|
||
}
|
||
});
|
||
}
|
||
|
||
|
||
}
|