package com.xwad.os.base; import android.annotation.SuppressLint; import android.app.Application; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.os.Handler; import android.os.Looper; import android.util.Log; import androidx.multidex.MultiDex; import com.alibaba.sdk.android.push.CloudPushService; import com.alibaba.sdk.android.push.CommonCallback; import com.alibaba.sdk.android.push.noonesdk.PushInitConfig; import com.alibaba.sdk.android.push.noonesdk.PushServiceFactory; import com.arialyy.aria.core.Aria; import com.hjq.toast.Toaster; import com.tencent.bugly.crashreport.CrashReport; import com.tencent.mmkv.MMKV; import com.xwad.os.BuildConfig; import com.xwad.os.alarm.AlarmUtils; import com.xwad.os.config.CommonConfig; import com.xwad.os.manager.AmapManager; import com.xwad.os.manager.AppManager; import com.xwad.os.manager.ConnectManager; import com.xwad.os.manager.ControlManager; import com.xwad.os.manager.DeviceSNManager; import com.xwad.os.manager.RemoteManager; import com.xwad.os.network.NetInterfaceManager; import com.xwad.os.push.PushManager; import com.xwad.os.receiver.ApkInstallReceiver; import com.xwad.os.utils.ActivationUtil; import com.xwad.os.utils.AppUsedTimeUtils; import com.xwad.os.utils.JgyUtils; import com.xwad.os.utils.OpenApkUtils; import com.xwad.os.utils.SystemUtils; import java.util.ArrayList; import java.util.Arrays; import java.util.HashSet; import java.util.Set; public class BaseApplication extends Application { private static final String TAG = "BaseApplication"; /** * ViewModel中因为经常旋转导致弱引用为空 */ @SuppressLint("StaticFieldLeak") private static BaseApplication sInstance; public static BaseApplication getInstance() { return sInstance; } @Override protected void attachBaseContext(Context base) { super.attachBaseContext(base); MultiDex.install(this); } @Override public void onCreate() { super.onCreate(); Log.e(TAG, "onCreate: "); sInstance = this; if (!BuildConfig.DEBUG) { // catchException(); } // 在开始分析的地方调用,传入路径 // 如果是放到外部路径,需要添加权限 // 默认存储在/sdcard/Android/data/packagename/files // Debug.startMethodTracing("App" + System.currentTimeMillis()); init(); // 在结束的地方调用 // Debug.stopMethodTracing(); } private void init() { Log.e(TAG, "init: "); long time = System.currentTimeMillis(); if (!BuildConfig.DEBUG) { catchException(); } if (SystemUtils.isMainProcessName(this, android.os.Process.myPid())) { String rootDir = MMKV.initialize(this); Log.e(TAG, "mmkv root: " + rootDir); ActivationUtil.init(this); RemoteManager.init(this); JgyUtils.init(this); CrashReport.initCrashReport(getApplicationContext(), "4efcaad4c9", false); CrashReport.setDeviceId(BaseApplication.this, DeviceSNManager.getDeviceSN()); // 初始化 Toast 框架 Toaster.init(this); AmapManager.init(this); AppManager.init(this); PushManager.init(this); AlarmUtils.init(this); AppUsedTimeUtils.init(this); OpenApkUtils.init(this); ConnectManager.init(this); ControlManager.init(this); NetInterfaceManager.init(this); // startService(new Intent(this, MainService.class)); registAppReceive(); Aria.init(this); // Aria.get(this).getDownloadConfig().setMaxTaskNum(1); // Aria.get(this).getDownloadConfig().setConvertSpeed(true); aliyunPushInit(); } Log.e(this.getClass().getSimpleName(), "init: " + (System.currentTimeMillis() - time) + "ms"); } public void aliyunPushInit() { // 特殊场景 需要定时拉起channel PushInitConfig config = new PushInitConfig.Builder() .application(this) .loopStartChannel(true) .loopInterval(60 * 10 * 1000) // 开启channel进程 .disableChannelProcess(false) // 开启channel进程心跳 .disableChannelProcessheartbeat(false) .build(); PushServiceFactory.init(config); final CloudPushService pushService = PushServiceFactory.getCloudPushService(); pushService.setLogLevel(CloudPushService.LOG_OFF); 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()); MMKV.mmkvWithID(CommonConfig.MMKV_ID, MMKV.MULTI_PROCESS_MODE).encode(CommonConfig.ALIYUN_PUSH_ID, pushService.getDeviceId()); Set tagSets = new HashSet<>(); tagSets.add(BuildConfig.platform); String[] tag = new ArrayList<>(tagSets).toArray(new String[tagSets.size()]); pushService.bindTag(CloudPushService.DEVICE_TARGET, tag, null, new CommonCallback() { @Override public void onSuccess(String s) { Log.e("AliyunPush", "bind tag " + Arrays.toString(tag) + " success\n"); } @Override public void onFailed(String errorCode, String errorMsg) { Log.e("AliyunPush", "bind tag " + Arrays.toString(tag) + " 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 ApkInstallReceiver apKinstallReceiver; private void registAppReceive() { if (null == apKinstallReceiver) { apKinstallReceiver = new ApkInstallReceiver(); } IntentFilter filter = new IntentFilter(); filter.setPriority(IntentFilter.SYSTEM_HIGH_PRIORITY); filter.addAction(Intent.ACTION_PACKAGE_ADDED); filter.addAction(Intent.ACTION_PACKAGE_REPLACED); filter.addAction(Intent.ACTION_PACKAGE_REMOVED); filter.addDataScheme("package"); registerReceiver(apKinstallReceiver, filter); } 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(); } } } }); } }