91 lines
2.9 KiB
Java
91 lines
2.9 KiB
Java
package com.uiui.os.base;
|
|
|
|
import android.annotation.SuppressLint;
|
|
import android.app.Application;
|
|
import android.content.Context;
|
|
import android.content.Intent;
|
|
import android.os.Handler;
|
|
import android.os.Looper;
|
|
import android.util.Log;
|
|
|
|
import com.arialyy.aria.core.Aria;
|
|
import com.qweather.sdk.view.HeConfig;
|
|
import com.tencent.mmkv.MMKV;
|
|
import com.uiui.os.BuildConfig;
|
|
import com.uiui.os.network.NetInterfaceManager;
|
|
import com.uiui.os.service.MainService;
|
|
import com.uiui.os.utils.AlarmUtils;
|
|
import com.uiui.os.utils.AmapManager;
|
|
import com.uiui.os.utils.AppUsedTimeUtils;
|
|
|
|
|
|
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;
|
|
if (!BuildConfig.DEBUG) {
|
|
catchException();
|
|
}
|
|
String rootDir = MMKV.initialize(this);
|
|
Log.e(TAG, "mmkv root: " + rootDir);
|
|
Aria.init(this);
|
|
Aria.get(this).getDownloadConfig().setMaxTaskNum(1);
|
|
Aria.get(this).getDownloadConfig().setConvertSpeed(true);
|
|
AppUsedTimeUtils.init(this);
|
|
AlarmUtils.init(this);
|
|
HeConfig.init("HE2111041506381545", "32b5ec69545e44119583a5e0ed4e87df");
|
|
AmapManager.init(this);
|
|
NetInterfaceManager.init(this);
|
|
startService(new Intent(this, MainService.class));
|
|
}
|
|
|
|
|
|
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();
|
|
}
|
|
}
|
|
}
|
|
});
|
|
}
|
|
|
|
|
|
}
|