Skip to content

Commit 6590f50

Browse files
committed
修复多次设置 Toast 策略会导致多次注册 Activity 生命周期监听的问题
优化框架中部分字段命名、参数命名、代码写法、代码注释
1 parent 5804d7c commit 6590f50

File tree

10 files changed

+70
-41
lines changed

10 files changed

+70
-41
lines changed

README-en.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
* Project address: [Github](https://github.com/getActivity/Toaster)
66

7-
* [Click here to download demo apk directly](https://github.com/getActivity/Toaster/releases/download/12.5/Toaster.apk)
7+
* [Click here to download demo apk directly](https://github.com/getActivity/Toaster/releases/download/12.6/Toaster.apk)
88

99
![](picture/en/demo_toast_activity.jpg) ![](picture/en/demo_toast_style_white.jpg) ![](picture/en/demo_toast_style_black.jpg)
1010

@@ -49,7 +49,7 @@ android {
4949
5050
dependencies {
5151
// Toast framework:https://github.com/getActivity/Toaster
52-
implementation 'com.github.getActivity:Toaster:12.5'
52+
implementation 'com.github.getActivity:Toaster:12.6'
5353
}
5454
```
5555

@@ -142,7 +142,7 @@ Toaster.getInterceptor();
142142

143143
| Function or detail | [Toaster](https://github.com/getActivity/Toaster) |[ AndroidUtilCode-ToastUtils ](https://github.com/Blankj/AndroidUtilCode)| [Toasty](https://github.com/GrenderG/Toasty) |
144144
| :----: | :------: | :-----: | :-----: |
145-
| Corresponding version | 12.5 | 1.30.6 | 1.5.0 |
145+
| Corresponding version | 12.6 | 1.30.6 | 1.5.0 |
146146
| Number of issues | [![](https://img.shields.io/github/issues/getActivity/Toaster.svg)](https://github.com/getActivity/Toaster/issues) |[![](https://img.shields.io/github/issues/Blankj/AndroidUtilCode.svg)](https://github.com/Blankj/AndroidUtilCode/issues)| [![](https://img.shields.io/github/issues/GrenderG/Toasty.svg)](https://github.com/GrenderG/Toasty/issues) |
147147
| Framework pack size | 31 KB | 500 KB | 50 KB |
148148
| Framework maintenance status| 维护中 | 停止维护 | 停止维护 |

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
* 博客地址:[只需体验三分钟,你就会跟我一样,爱上这款 Toast](https://www.jianshu.com/p/9b174ee2c571)
88

9-
* 可以扫码下载 Demo 进行演示或者测试,如果扫码下载不了的,[点击此处可直接下载](https://github.com/getActivity/Toaster/releases/download/12.5/Toaster.apk)
9+
* 可以扫码下载 Demo 进行演示或者测试,如果扫码下载不了的,[点击此处可直接下载](https://github.com/getActivity/Toaster/releases/download/12.6/Toaster.apk)
1010

1111
![](picture/zh/download_demo_apk_qr_code.png)
1212

@@ -53,7 +53,7 @@ android {
5353
5454
dependencies {
5555
// 吐司框架:https://github.com/getActivity/Toaster
56-
implementation 'com.github.getActivity:Toaster:12.5'
56+
implementation 'com.github.getActivity:Toaster:12.6'
5757
}
5858
```
5959

@@ -146,7 +146,7 @@ Toaster.getInterceptor();
146146

147147
| 功能或细节 | [Toaster](https://github.com/getActivity/Toaster) | [AndroidUtilCode-ToastUtils](https://github.com/Blankj/AndroidUtilCode) | [Toasty](https://github.com/GrenderG/Toasty) |
148148
| :----: | :------: | :-----: | :-----: |
149-
| 对应版本 | 12.5 | 1.30.6 | 1.5.0 |
149+
| 对应版本 | 12.6 | 1.30.6 | 1.5.0 |
150150
| issues 数 | [![](https://img.shields.io/github/issues/getActivity/Toaster.svg)](https://github.com/getActivity/Toaster/issues) | [![](https://img.shields.io/github/issues/Blankj/AndroidUtilCode.svg)](https://github.com/Blankj/AndroidUtilCode/issues) | [![](https://img.shields.io/github/issues/GrenderG/Toasty.svg)](https://github.com/GrenderG/Toasty/issues) |
151151
| 框架体积 | 32 KB | 500 KB | 50 KB |
152152
| 框架维护状态 | 维护中 | 停止维护 | 停止维护 |

app/build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ android {
77
applicationId "com.hjq.toast.demo"
88
minSdkVersion 16
99
targetSdkVersion 33
10-
versionCode 1205
11-
versionName "12.5"
10+
versionCode 1206
11+
versionName "12.6"
1212
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
1313
}
1414

build.gradle

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,23 @@ allprojects {
3131
jcenter()
3232
}
3333

34-
// 将构建文件统一输出到项目根目录下的 build 文件夹
35-
setBuildDir(new File(rootDir, "build/${path.replaceAll(':', '/')}"))
34+
// 读取 local.properties 文件配置
35+
def properties = new Properties()
36+
def localPropertiesFile = rootProject.file("local.properties")
37+
if (localPropertiesFile.exists()) {
38+
localPropertiesFile.withInputStream { inputStream ->
39+
properties.load(inputStream)
40+
}
41+
}
42+
43+
String buildDirPath = properties.getProperty("build.dir")
44+
if (buildDirPath != null && buildDirPath != "") {
45+
// 将构建文件统一输出到指定的目录下
46+
setBuildDir(new File(buildDirPath, rootProject.name + "/build/${path.replaceAll(':', '/')}"))
47+
} else {
48+
// 将构建文件统一输出到项目根目录下的 build 文件夹
49+
setBuildDir(new File(rootDir, "build/${path.replaceAll(':', '/')}"))
50+
}
3651
}
3752

3853
tasks.register('clean', Delete) {

library/build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ android {
55

66
defaultConfig {
77
minSdkVersion 14
8-
versionCode 1205
9-
versionName "12.5"
8+
versionCode 1206
9+
versionName "12.6"
1010
}
1111

1212
// 使用 JDK 1.8

library/src/main/java/com/hjq/toast/ActivityStack.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ final class ActivityStack implements Application.ActivityLifecycleCallbacks {
1919
public static ActivityStack getInstance() {
2020
if(sInstance == null) {
2121
synchronized (ActivityStack.class) {
22-
if(sInstance == null) {
22+
if (sInstance == null) {
2323
sInstance = new ActivityStack();
2424
}
2525
}

library/src/main/java/com/hjq/toast/NotificationServiceProxy.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,15 @@
77
* author : Android 轮子哥
88
* github : https://github.com/getActivity/Toaster
99
* time : 2021/11/13
10-
* desc : 通知服务代理代理对象
10+
* desc : 通知服务代理对象
1111
*/
1212
final class NotificationServiceProxy implements InvocationHandler {
1313

1414
/** 被代理的对象 */
15-
private final Object mSource;
15+
private final Object mRealObject;
1616

17-
public NotificationServiceProxy(Object source) {
18-
mSource = source;
17+
public NotificationServiceProxy(Object realObject) {
18+
mRealObject = realObject;
1919
}
2020

2121
@Override
@@ -25,13 +25,13 @@ public Object invoke(Object proxy, Method method, Object[] args) throws Throwabl
2525
case "enqueueToastEx":
2626
case "cancelToast":
2727
// 将包名修改成系统包名,这样就可以绕过系统的拦截
28-
// 部分华为机将 enqueueToast 修改成了 enqueueToastEx
28+
// 部分华为机将 enqueueToast 方法名修改成了 enqueueToastEx
2929
args[0] = "android";
3030
break;
3131
default:
3232
break;
3333
}
3434
// 使用动态代理
35-
return method.invoke(mSource, args);
35+
return method.invoke(mRealObject, args);
3636
}
3737
}

library/src/main/java/com/hjq/toast/ToastImpl.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -112,12 +112,18 @@ private void trySendAccessibilityEvent(View view) {
112112
if (!accessibilityManager.isEnabled()) {
113113
return;
114114
}
115-
// 将 Toast 视为通知,因为它们用于向用户宣布短暂的信息
116-
AccessibilityEvent event = AccessibilityEvent.obtain(
117-
AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED);
115+
int eventType = AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED;
116+
AccessibilityEvent event;
117+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
118+
event = new AccessibilityEvent();
119+
event.setEventType(eventType);
120+
} else {
121+
event = AccessibilityEvent.obtain(eventType);
122+
}
118123
event.setClassName(Toast.class.getName());
119124
event.setPackageName(context.getPackageName());
120125
view.dispatchPopulateAccessibilityEvent(event);
126+
// 将 Toast 视为通知,因为它们用于向用户宣布短暂的信息
121127
accessibilityManager.sendAccessibilityEvent(event);
122128
}
123129

@@ -162,7 +168,6 @@ public void run() {
162168
}
163169

164170
try {
165-
166171
windowManager.addView(mToast.getView(), params);
167172
// 添加一个移除吐司的任务
168173
HANDLER.postDelayed(() -> cancel(), mToast.getDuration() == Toast.LENGTH_LONG ?

library/src/main/java/com/hjq/toast/ToastStrategy.java

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
import android.app.NotificationManager;
88
import android.content.Context;
99
import android.os.Build;
10+
import android.os.Build.VERSION;
11+
import android.os.Build.VERSION_CODES;
1012
import android.os.Handler;
1113
import android.os.Looper;
1214
import android.os.SystemClock;
@@ -47,7 +49,7 @@ public class ToastStrategy implements IToastStrategy {
4749
public static final int SHOW_STRATEGY_TYPE_QUEUE = 1;
4850

4951
/** Handler 对象 */
50-
private final static Handler HANDLER = new Handler(Looper.getMainLooper());
52+
private static final Handler HANDLER = new Handler(Looper.getMainLooper());
5153

5254
/**
5355
* 默认延迟时间
@@ -79,6 +81,11 @@ public ToastStrategy() {
7981
this(ToastStrategy.SHOW_STRATEGY_TYPE_IMMEDIATELY);
8082
}
8183

84+
@Override
85+
public void registerStrategy(Application application) {
86+
mApplication = application;
87+
}
88+
8289
public ToastStrategy(int type) {
8390
mShowStrategyType = type;
8491
switch (mShowStrategyType) {
@@ -90,21 +97,16 @@ public ToastStrategy(int type) {
9097
}
9198
}
9299

93-
@Override
94-
public void registerStrategy(Application application) {
95-
mApplication = application;
96-
ActivityStack.getInstance().register(application);
97-
}
98-
99100
@Override
100101
public IToast createToast(ToastParams params) {
101-
Activity foregroundActivity = ActivityStack.getInstance().getForegroundActivity();
102+
Activity foregroundActivity = getForegroundActivity();
102103
IToast toast;
103104
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M &&
104105
Settings.canDrawOverlays(mApplication)) {
105106
// 如果有悬浮窗权限,就开启全局的 Toast
106107
toast = new GlobalToast(mApplication);
107-
} else if (foregroundActivity != null && !params.crossPageShow) {
108+
} else if (!params.crossPageShow && foregroundActivity != null && !foregroundActivity.isFinishing() &&
109+
(VERSION.SDK_INT >= VERSION_CODES.JELLY_BEAN_MR1 && !foregroundActivity.isDestroyed())) {
108110
// 如果没有悬浮窗权限,就开启一个依附于 Activity 的 Toast
109111
toast = new ActivityToast(foregroundActivity);
110112
} else if (Build.VERSION.SDK_INT == Build.VERSION_CODES.N_MR1) {
@@ -267,17 +269,11 @@ protected boolean isChangeEnabledCompat(long changeId) {
267269
// 因为 Compatibility.isChangeEnabled() 普通应用根本调用不到,反射也不行
268270
// 通过 Toast.isSystemRenderedTextToast 也没有办法反射到
269271
// 最后发现反射 CompatChanges.isChangeEnabled 是可以的
270-
Class<?> aClass = Class.forName("android.app.compat.CompatChanges");
271-
Method method = aClass.getMethod("isChangeEnabled", long.class);
272+
Class<?> clazz = Class.forName("android.app.compat.CompatChanges");
273+
Method method = clazz.getMethod("isChangeEnabled", long.class);
272274
method.setAccessible(true);
273275
return Boolean.parseBoolean(String.valueOf(method.invoke(null, changeId)));
274-
} catch (ClassNotFoundException e) {
275-
e.printStackTrace();
276-
} catch (InvocationTargetException e) {
277-
e.printStackTrace();
278-
} catch (NoSuchMethodException e) {
279-
e.printStackTrace();
280-
} catch (IllegalAccessException e) {
276+
} catch (ClassNotFoundException | InvocationTargetException | NoSuchMethodException | IllegalAccessException e) {
281277
e.printStackTrace();
282278
}
283279
return false;
@@ -311,4 +307,11 @@ protected boolean areNotificationsEnabled(Context context) {
311307
}
312308
return true;
313309
}
310+
311+
/**
312+
* 获取前台的 Activity
313+
*/
314+
protected Activity getForegroundActivity() {
315+
return ActivityStack.getInstance().getForegroundActivity();
316+
}
314317
}

library/src/main/java/com/hjq/toast/Toaster.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,13 @@ public static void init(Application application, IToastStyle<?> style) {
6666
* @param style Toast 样式
6767
*/
6868
public static void init(Application application, IToastStrategy strategy, IToastStyle<?> style) {
69+
// 如果当前已经初始化过了,就不要再重复初始化了
70+
if (isInit()) {
71+
return;
72+
}
73+
6974
sApplication = application;
75+
ActivityStack.getInstance().register(application);
7076

7177
// 初始化 Toast 策略
7278
if (strategy == null) {

0 commit comments

Comments
 (0)