162 lines
5.9 KiB
Java
162 lines
5.9 KiB
Java
package com.xxpatx.os.base;
|
|
|
|
import android.app.Application;
|
|
import android.content.Intent;
|
|
import android.os.Build;
|
|
import android.os.Handler;
|
|
import android.os.Looper;
|
|
import android.text.TextUtils;
|
|
import android.util.Log;
|
|
|
|
import com.alibaba.sdk.android.push.CloudPushService;
|
|
import com.alibaba.sdk.android.push.CommonCallback;
|
|
import com.alibaba.sdk.android.push.noonesdk.PushServiceFactory;
|
|
import com.arialyy.aria.core.Aria;
|
|
import com.hjq.toast.Toaster;
|
|
import com.qweather.sdk.view.HeConfig;
|
|
import com.tencent.bugly.crashreport.CrashReport;
|
|
import com.tencent.mmkv.MMKV;
|
|
import com.xxpatx.os.BuildConfig;
|
|
import com.xxpatx.os.alarm.AlarmUtils;
|
|
import com.xxpatx.os.db.ContactCacheUtils;
|
|
import com.xxpatx.os.db.WechatContactManager;
|
|
import com.xxpatx.os.manager.AmapManager;
|
|
import com.xxpatx.os.manager.AppManager;
|
|
import com.xxpatx.os.manager.AppStatusManager;
|
|
import com.xxpatx.os.manager.ConnectManager;
|
|
import com.xxpatx.os.manager.DesktopIconManager;
|
|
import com.xxpatx.os.network.NetInterfaceManager;
|
|
import com.xxpatx.os.push.PushManager;
|
|
import com.xxpatx.os.service.main.MainService;
|
|
import com.xxpatx.os.shortcut.ShortcutUtils;
|
|
import com.xxpatx.os.utils.AppUsedTimeUtils;
|
|
import com.xxpatx.os.utils.Utils;
|
|
import com.xxpatx.os.utils.WiFiUtils;
|
|
|
|
public class BaseApplication extends Application {
|
|
private static final String TAG = "BaseApplication";
|
|
|
|
@Override
|
|
public void onCreate() {
|
|
super.onCreate();
|
|
if (!BuildConfig.DEBUG) {
|
|
catchException();
|
|
}
|
|
String rootDir = MMKV.initialize(this);
|
|
Log.e(TAG, "mmkv root: " + rootDir);
|
|
|
|
// 初始化 Toast 框架
|
|
Toaster.init(this);
|
|
|
|
CrashReport.initCrashReport(getApplicationContext(), "09fec11058", false);
|
|
CrashReport.setDeviceModel(getApplicationContext(), Build.MODEL);
|
|
CrashReport.setDeviceId(this, Utils.getSerial());
|
|
xcrash.XCrash.init(this);
|
|
|
|
aliyunPushInit();
|
|
PushManager.init(this);
|
|
|
|
Aria.init(this);
|
|
Aria.get(this).getDownloadConfig().setMaxTaskNum(1);
|
|
Aria.get(this).getDownloadConfig().setConvertSpeed(true);
|
|
|
|
AppManager.init(this);
|
|
WiFiUtils.init(this);
|
|
AppUsedTimeUtils.init(this);
|
|
AlarmUtils.init(this);
|
|
ShortcutUtils.init(this);
|
|
|
|
AmapManager.init(this);
|
|
HeConfig.init("HE2407111551551292", "28301b41b0ae42c2b6cecf12862ade1f");
|
|
//切换至开发版服务
|
|
HeConfig.switchToDevService();
|
|
|
|
ConnectManager.init(this);
|
|
AppStatusManager.init(this);
|
|
NetInterfaceManager.init(this);
|
|
try {
|
|
startService(new Intent(this, MainService.class));
|
|
} catch (Exception e) {
|
|
Log.e(TAG, "onCreate: " + e.getMessage());
|
|
}
|
|
WechatContactManager.init(this);
|
|
ContactCacheUtils.init(this);
|
|
DesktopIconManager.init(this);
|
|
}
|
|
|
|
private void aliyunPushInit() {
|
|
PushServiceFactory.init(this);
|
|
final CloudPushService pushService = PushServiceFactory.getCloudPushService();
|
|
pushService.setLogLevel(CloudPushService.LOG_DEBUG);
|
|
pushService.register(this, new CommonCallback() {
|
|
@Override
|
|
public void onSuccess(String response) {
|
|
Log.e("AliyunPush", "init cloudchannel success");
|
|
Log.e("AliyunPush", "init cloudchannel success " + pushService.getDeviceId());
|
|
String sn = Utils.getSerial();
|
|
if (TextUtils.isEmpty(sn)) {
|
|
return;
|
|
}
|
|
pushService.bindAccount(sn, new CommonCallback() {
|
|
@Override
|
|
public void onSuccess(String s) {
|
|
Log.e("AliyunPush", "bind account " + sn + " success\n");
|
|
}
|
|
|
|
@Override
|
|
public void onFailed(String errorCode, String errorMsg) {
|
|
Log.e("AliyunPush", "bind account " + sn + " failed." +
|
|
"errorCode: " + errorCode + ", errorMsg:" + errorMsg);
|
|
}
|
|
});
|
|
pushService.addAlias(sn, new CommonCallback() {
|
|
@Override
|
|
public void onSuccess(String s) {
|
|
Log.e("AliyunPush", "add alias " + sn + " success\n");
|
|
}
|
|
|
|
@Override
|
|
public void onFailed(String errorCode, String errorMsg) {
|
|
Log.e("AliyunPush", "add alias " + sn + " failed." +
|
|
"errorCode: " + errorCode + ", errorMsg:" + errorMsg + "\n");
|
|
}
|
|
});
|
|
}
|
|
|
|
@Override
|
|
public void onFailed(String errorCode, String errorMessage) {
|
|
Log.e("AliyunPush", "init cloudchannel failed -- errorcode:" + errorCode + " -- errorMessage:" + errorMessage);
|
|
}
|
|
});
|
|
|
|
}
|
|
|
|
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();
|
|
}
|
|
}
|
|
}
|
|
});
|
|
}
|
|
|
|
|
|
}
|