Skip to content

Commit 3088c10

Browse files
committed
修复系统 Toast 在后台 setText 会有一定几率触发 Crash 的问题
1 parent de577a2 commit 3088c10

File tree

9 files changed

+55
-13
lines changed

9 files changed

+55
-13
lines changed

.github/ISSUE_TEMPLATE/issue_en_template_bug.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ assignees: getActivity
1919

2020
* Whether the problem can be reproduced [Required]: Yes/No
2121

22+
* Android Project targetSdkVersion [Required]: Please fill in your project target sdk version
23+
2224
* Phone information in question [Required]: Please fill in the phone brand and model in question
2325

2426
* Android version in question [Required]: Please fill in the Android version in question

.github/ISSUE_TEMPLATE/issue_zh_template_bug.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ assignees: getActivity
1818

1919
* 是否必现【必填】:是/否
2020

21+
* 项目 targetSdkVersion【必填】:XX
22+
2123
* 出现问题的手机信息【必填】:请填写出现问题的品牌和机型
2224

2325
* 出现问题的安卓版本【必填】:请填写出现问题的 Android 版本

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.0/Toaster.apk)
7+
* [Click here to download demo apk directly](https://github.com/getActivity/Toaster/releases/download/12.2/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.0'
52+
implementation 'com.github.getActivity:Toaster:12.2'
5353
}
5454
```
5555

@@ -134,7 +134,7 @@ Toaster.getInterceptor();
134134

135135
| Function or detail | [Toaster](https://github.com/getActivity/Toaster) |[ AndroidUtilCode-ToastUtils ](https://github.com/Blankj/AndroidUtilCode)| [Toasty](https://github.com/GrenderG/Toasty) |
136136
| :----: | :------: | :-----: | :-----: |
137-
| Corresponding version | 12.0 | 1.30.6 | 1.5.0 |
137+
| Corresponding version | 12.2 | 1.30.6 | 1.5.0 |
138138
| 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) |
139139
| Framework pack size | 31 KB | 500 KB | 50 KB |
140140
| Framework maintenance status| 维护中 | 停止维护 | 停止维护 |

README.md

Lines changed: 6 additions & 4 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.0/Toaster.apk)
9+
* 可以扫码下载 Demo 进行演示或者测试,如果扫码下载不了的,[点击此处可直接下载](https://github.com/getActivity/Toaster/releases/download/12.2/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.0'
56+
implementation 'com.github.getActivity:Toaster:12.2'
5757
}
5858
```
5959

@@ -138,9 +138,9 @@ Toaster.getInterceptor();
138138

139139
| 功能或细节 | [Toaster](https://github.com/getActivity/Toaster) | [AndroidUtilCode-ToastUtils](https://github.com/Blankj/AndroidUtilCode) | [Toasty](https://github.com/GrenderG/Toasty) |
140140
| :----: | :------: | :-----: | :-----: |
141-
| 对应版本 | 12.0 | 1.30.6 | 1.5.0 |
141+
| 对应版本 | 12.2 | 1.30.6 | 1.5.0 |
142142
| 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) |
143-
| 框架体积 | 31 KB | 500 KB | 50 KB |
143+
| 框架体积 | 32 KB | 500 KB | 50 KB |
144144
| 框架维护状态 | 维护中 | 停止维护 | 停止维护 |
145145
| 调用代码定位 ||||
146146
| 支持在子线程中调用显示 ||||
@@ -264,6 +264,8 @@ new Toast
264264

265265
* 表情包大集合:[EmojiPackage](https://github.com/getActivity/EmojiPackage) ![](https://img.shields.io/github/stars/getActivity/EmojiPackage.svg) ![](https://img.shields.io/github/forks/getActivity/EmojiPackage.svg)
266266

267+
* AI 资源大汇总:[AiIndex](https://github.com/getActivity/AiIndex) ![](https://img.shields.io/github/stars/getActivity/AiIndex.svg) ![](https://img.shields.io/github/forks/getActivity/AiIndex.svg)
268+
267269
* 省市区 Json 数据:[ProvinceJson](https://github.com/getActivity/ProvinceJson) ![](https://img.shields.io/github/stars/getActivity/ProvinceJson.svg) ![](https://img.shields.io/github/forks/getActivity/ProvinceJson.svg)
268270

269271
* Markdown 语法文档:[MarkdownDoc](https://github.com/getActivity/MarkdownDoc) ![](https://img.shields.io/github/stars/getActivity/MarkdownDoc.svg) ![](https://img.shields.io/github/forks/getActivity/MarkdownDoc.svg)

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 31
10-
versionCode 1200
11-
versionName "12.0"
10+
versionCode 1202
11+
versionName "12.2"
1212
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
1313
}
1414

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 1200
9-
versionName "12.0"
8+
versionCode 1202
9+
versionName "12.2"
1010
}
1111

1212
// 使用 JDK 1.8

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

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,8 +124,7 @@ public IToast createToast(IToastStyle<?> style) {
124124
} else {
125125
toast = new SystemToast(mApplication);
126126
}
127-
128-
if (isSupportToastStyle(toast)) {
127+
if (isSupportToastStyle(toast) || !onlyShowSystemToastStyle()) {
129128
diyToastStyle(toast, style);
130129
}
131130
return toast;
@@ -251,6 +250,41 @@ public void run() {
251250
}
252251
}
253252

253+
/**
254+
* 当前是否只能显示系统 Toast 样式
255+
*/
256+
protected boolean onlyShowSystemToastStyle() {
257+
// Github issue 地址:https://github.com/getActivity/Toaster/issues/103
258+
// Toast.CHANGE_TEXT_TOASTS_IN_THE_SYSTEM = 147798919L
259+
return isChangeEnabledCompat(147798919L);
260+
}
261+
262+
@SuppressLint("PrivateApi")
263+
protected boolean isChangeEnabledCompat(long changeId) {
264+
// 需要注意的是这个 api 是在 android 11 的时候出现的,反射前需要先判断好版本
265+
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.R) {
266+
return true;
267+
}
268+
try {
269+
// 因为 Compatibility.isChangeEnabled() 普通应用根本调用不到,反射也不行
270+
// 通过 Toast.isSystemRenderedTextToast 也没有办法反射到
271+
// 最后发现反射 CompatChanges.isChangeEnabled 是可以的
272+
Class<?> aClass = Class.forName("android.app.compat.CompatChanges");
273+
Method method = aClass.getMethod("isChangeEnabled", long.class);
274+
method.setAccessible(true);
275+
return Boolean.parseBoolean(String.valueOf(method.invoke(null, changeId)));
276+
} catch (ClassNotFoundException e) {
277+
e.printStackTrace();
278+
} catch (InvocationTargetException e) {
279+
e.printStackTrace();
280+
} catch (NoSuchMethodException e) {
281+
e.printStackTrace();
282+
} catch (IllegalAccessException e) {
283+
e.printStackTrace();
284+
}
285+
return false;
286+
}
287+
254288
/**
255289
* 是否有通知栏权限
256290
*/

library/src/main/java/com/hjq/toast/config/IToast.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,8 @@ default TextView findMessageView(View view) {
9393
view.setId(android.R.id.message);
9494
} else if (view.getId() != android.R.id.message) {
9595
// 必须将 TextView 的 id 值设置成 android.R.id.message
96+
// 否则 Android 11 手机上在后台 toast.setText 的时候会出现报错
97+
// java.lang.RuntimeException: This Toast was not created with Toast.makeText()
9698
throw new IllegalArgumentException("You must set the ID value of TextView to android.R.id.message");
9799
}
98100
return (TextView) view;

picture/zh/help_doc_rename_vote.jpg

-91.8 KB
Loading

0 commit comments

Comments
 (0)