From adcafc7e16c9b8743eca86c35954ef4b049cbcec Mon Sep 17 00:00:00 2001 From: hch <16607480311@163.com> Date: Thu, 26 Oct 2023 14:50:17 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A4=84=E7=90=86=20=E7=A9=BA=E6=8C=87?= =?UTF-8?q?=E9=92=88=E5=BC=82=E5=B8=B8=20CommonAppContext.getTopActivity()?= =?UTF-8?q?=3D=3Dnull=20=E4=B8=BB=E8=A6=81=E5=A4=84=E7=90=86=E4=BA=86=20To?= =?UTF-8?q?astUtil,OpenAdManager?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/yunbao/common/CommonAppContext.java | 7 ++- .../yunbao/common/http/CommonHttpUtil.java | 2 +- .../yunbao/common/manager/OpenAdManager.java | 56 +++++++++---------- .../com/yunbao/common/utils/ToastUtil.java | 34 +++++++---- 4 files changed, 55 insertions(+), 44 deletions(-) diff --git a/common/src/main/java/com/yunbao/common/CommonAppContext.java b/common/src/main/java/com/yunbao/common/CommonAppContext.java index 453490b55..269464e41 100644 --- a/common/src/main/java/com/yunbao/common/CommonAppContext.java +++ b/common/src/main/java/com/yunbao/common/CommonAppContext.java @@ -56,9 +56,7 @@ public class CommonAppContext extends MultiDexApplication { //设置新加坡融云服务器 有时候国内收不到推送是因为这个 RongIMClient.setServerInfo("navsg01.cn.ronghub.com", null); //谷歌推送 - PushConfig gconfig = new PushConfig.Builder() - .enableFCM(true) - .build(); + PushConfig gconfig = new PushConfig.Builder().enableFCM(true).build(); RongPushClient.setPushConfig(gconfig); //初始化友盟统计 // UMConfigure.init(this, UMConfigure.DEVICE_TYPE_PHONE, null); @@ -86,6 +84,9 @@ public class CommonAppContext extends MultiDexApplication { } public static Activity getTopActivity() { + if (activityWeakReference == null) { + return null; + } return activityWeakReference.get(); } diff --git a/common/src/main/java/com/yunbao/common/http/CommonHttpUtil.java b/common/src/main/java/com/yunbao/common/http/CommonHttpUtil.java index f573f278c..c90987dff 100644 --- a/common/src/main/java/com/yunbao/common/http/CommonHttpUtil.java +++ b/common/src/main/java/com/yunbao/common/http/CommonHttpUtil.java @@ -262,7 +262,7 @@ public class CommonHttpUtil { } }).build().show(); } else { - ToastUtil.show(CommonAppContext.getTopActivity().getString(R.string.net_error) + "code:" + code + " msg:" + msg); + ToastUtil.show(context.getString(R.string.net_error) + "code:" + code + " msg:" + msg); } } diff --git a/common/src/main/java/com/yunbao/common/manager/OpenAdManager.java b/common/src/main/java/com/yunbao/common/manager/OpenAdManager.java index b9e967df9..7d64a092c 100644 --- a/common/src/main/java/com/yunbao/common/manager/OpenAdManager.java +++ b/common/src/main/java/com/yunbao/common/manager/OpenAdManager.java @@ -1,5 +1,6 @@ package com.yunbao.common.manager; +import android.content.Context; import android.os.Handler; import android.os.Looper; import android.util.Log; @@ -52,29 +53,32 @@ public class OpenAdManager { if (list != null && list.isEmpty()) { return; } - if(CommonAppContext.getTopActivity()==null){ + Context context = CommonAppContext.getTopActivity(); + if (context == null) { + context = CommonAppContext.sInstance; + } + if (context == null) { return; } - LiveNetManager.get(CommonAppContext.getTopActivity()) - .activityPopup(new HttpCallback>() { - @Override - public void onSuccess(List data) { - if (data == null || data.isEmpty()) { - Log.i(TAG, "onSuccess: 没有数据"); - list = new ArrayList<>(); - return; - } - list = data; - if (isShow) { - show(TYPE_HOME, false); - } - } + LiveNetManager.get(context).activityPopup(new HttpCallback>() { + @Override + public void onSuccess(List data) { + if (data == null || data.isEmpty()) { + Log.i(TAG, "onSuccess: 没有数据"); + list = new ArrayList<>(); + return; + } + list = data; + if (isShow) { + show(TYPE_HOME, false); + } + } - @Override - public void onError(String error) { - System.err.println("弹框列表:" + error); - } - }); + @Override + public void onError(String error) { + System.err.println("弹框列表:" + error); + } + }); } @@ -163,15 +167,11 @@ public class OpenAdManager { } showMap.put(model.getId(), true); if (model.getModel() == OpenAdModel.MODEL_BOTTOM) { - new OpenAdBottomDialogPopup(CommonAppContext.getTopActivity(), model) - .setListener((bean, position) -> { - }) - .showDialog(); + new OpenAdBottomDialogPopup(CommonAppContext.getTopActivity(), model).setListener((bean, position) -> { + }).showDialog(); } else { - new OpenAdCenterDialogPopup(CommonAppContext.getTopActivity(), model) - .setListener((bean, position) -> { - }) - .showDialog(); + new OpenAdCenterDialogPopup(CommonAppContext.getTopActivity(), model).setListener((bean, position) -> { + }).showDialog(); } } } diff --git a/common/src/main/java/com/yunbao/common/utils/ToastUtil.java b/common/src/main/java/com/yunbao/common/utils/ToastUtil.java index 80304292f..e0808c4af 100644 --- a/common/src/main/java/com/yunbao/common/utils/ToastUtil.java +++ b/common/src/main/java/com/yunbao/common/utils/ToastUtil.java @@ -60,11 +60,19 @@ public class ToastUtil { /** * 显示黑底白字的提示 * - * @param content 内容 + * @param content 内容 * @param delayMillis 显示时间 毫秒 */ public static void showStyle(String content, long delayMillis) { - Dialog dialog = new Dialog(CommonAppContext.getTopActivity(), R.style.dialog); + Context context = CommonAppContext.getTopActivity(); + if (context == null) { + context = CommonAppContext.sInstance; + } + if (context == null) { + return; + } + + Dialog dialog = new Dialog(context, R.style.dialog); dialog.setContentView(R.layout.dialog_toast); dialog.setCancelable(false); dialog.setCanceledOnTouchOutside(false); @@ -79,14 +87,16 @@ public class ToastUtil { dialog.show(); new Handler(Looper.getMainLooper()).postDelayed(dialog::dismiss, delayMillis); } - public static void showDebug(String s){ - if(BuildConfig.DEBUG){ - show("开发模式:"+s); + + public static void showDebug(String s) { + if (BuildConfig.DEBUG) { + show("开发模式:" + s); } } - public static void showDebug(int s){ - if(BuildConfig.DEBUG){ - show("开发模式:"+s); + + public static void showDebug(int s) { + if (BuildConfig.DEBUG) { + show("开发模式:" + s); } } @@ -94,10 +104,10 @@ public class ToastUtil { if (TextUtils.isEmpty(s)) { return; } - Log.i("Toast", "Toast show: "+s); - Log.i("ts","ll"); - Toast.makeText(CommonAppContext.sInstance,s,Toast.LENGTH_SHORT).show(); - Log.i("ts","22"); + Log.i("Toast", "Toast show: " + s); + Log.i("ts", "ll"); + Toast.makeText(CommonAppContext.sInstance, s, Toast.LENGTH_SHORT).show(); + Log.i("ts", "22"); // long curTime = System.currentTimeMillis(); // if (curTime - sLastTime > 2000) {