Skip to content

Commit

Permalink
修复系统 Toast 在后台 setText 会有一定几率触发 Crash 的问题
Browse files Browse the repository at this point in the history
  • Loading branch information
getActivity committed Apr 9, 2023
1 parent de577a2 commit 3088c10
Show file tree
Hide file tree
Showing 9 changed files with 55 additions and 13 deletions.
2 changes: 2 additions & 0 deletions .github/ISSUE_TEMPLATE/issue_en_template_bug.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ assignees: getActivity

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

* Android Project targetSdkVersion [Required]: Please fill in your project target sdk version

* Phone information in question [Required]: Please fill in the phone brand and model in question

* Android version in question [Required]: Please fill in the Android version in question
Expand Down
2 changes: 2 additions & 0 deletions .github/ISSUE_TEMPLATE/issue_zh_template_bug.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ assignees: getActivity

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

* 项目 targetSdkVersion【必填】:XX

* 出现问题的手机信息【必填】:请填写出现问题的品牌和机型

* 出现问题的安卓版本【必填】:请填写出现问题的 Android 版本
Expand Down
6 changes: 3 additions & 3 deletions README-en.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

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

* [Click here to download demo apk directly](https://github.com/getActivity/Toaster/releases/download/12.0/Toaster.apk)
* [Click here to download demo apk directly](https://github.com/getActivity/Toaster/releases/download/12.2/Toaster.apk)

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

Expand Down Expand Up @@ -49,7 +49,7 @@ android {
dependencies {
// Toast framework:https://github.com/getActivity/Toaster
implementation 'com.github.getActivity:Toaster:12.0'
implementation 'com.github.getActivity:Toaster:12.2'
}
```

Expand Down Expand Up @@ -134,7 +134,7 @@ Toaster.getInterceptor();

| Function or detail | [Toaster](https://github.com/getActivity/Toaster) |[ AndroidUtilCode-ToastUtils ](https://github.com/Blankj/AndroidUtilCode)| [Toasty](https://github.com/GrenderG/Toasty) |
| :----: | :------: | :-----: | :-----: |
| Corresponding version | 12.0 | 1.30.6 | 1.5.0 |
| Corresponding version | 12.2 | 1.30.6 | 1.5.0 |
| 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) |
| Framework pack size | 31 KB | 500 KB | 50 KB |
| Framework maintenance status| 维护中 | 停止维护 | 停止维护 |
Expand Down
10 changes: 6 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

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

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

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

Expand Down Expand Up @@ -53,7 +53,7 @@ android {
dependencies {
// 吐司框架:https://github.com/getActivity/Toaster
implementation 'com.github.getActivity:Toaster:12.0'
implementation 'com.github.getActivity:Toaster:12.2'
}
```

Expand Down Expand Up @@ -138,9 +138,9 @@ Toaster.getInterceptor();

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

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

* 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)

* 省市区 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)

* 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)
Expand Down
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ android {
applicationId "com.hjq.toast.demo"
minSdkVersion 16
targetSdkVersion 31
versionCode 1200
versionName "12.0"
versionCode 1202
versionName "12.2"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}

Expand Down
4 changes: 2 additions & 2 deletions library/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ android {

defaultConfig {
minSdkVersion 14
versionCode 1200
versionName "12.0"
versionCode 1202
versionName "12.2"
}

// 使用 JDK 1.8
Expand Down
38 changes: 36 additions & 2 deletions library/src/main/java/com/hjq/toast/ToastStrategy.java
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,7 @@ public IToast createToast(IToastStyle<?> style) {
} else {
toast = new SystemToast(mApplication);
}

if (isSupportToastStyle(toast)) {
if (isSupportToastStyle(toast) || !onlyShowSystemToastStyle()) {
diyToastStyle(toast, style);
}
return toast;
Expand Down Expand Up @@ -251,6 +250,41 @@ public void run() {
}
}

/**
* 当前是否只能显示系统 Toast 样式
*/
protected boolean onlyShowSystemToastStyle() {
// Github issue 地址:https://github.com/getActivity/Toaster/issues/103
// Toast.CHANGE_TEXT_TOASTS_IN_THE_SYSTEM = 147798919L
return isChangeEnabledCompat(147798919L);
}

@SuppressLint("PrivateApi")
protected boolean isChangeEnabledCompat(long changeId) {
// 需要注意的是这个 api 是在 android 11 的时候出现的,反射前需要先判断好版本
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.R) {
return true;
}
try {
// 因为 Compatibility.isChangeEnabled() 普通应用根本调用不到,反射也不行
// 通过 Toast.isSystemRenderedTextToast 也没有办法反射到
// 最后发现反射 CompatChanges.isChangeEnabled 是可以的
Class<?> aClass = Class.forName("android.app.compat.CompatChanges");
Method method = aClass.getMethod("isChangeEnabled", long.class);
method.setAccessible(true);
return Boolean.parseBoolean(String.valueOf(method.invoke(null, changeId)));
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
return false;
}

/**
* 是否有通知栏权限
*/
Expand Down
2 changes: 2 additions & 0 deletions library/src/main/java/com/hjq/toast/config/IToast.java
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@ default TextView findMessageView(View view) {
view.setId(android.R.id.message);
} else if (view.getId() != android.R.id.message) {
// 必须将 TextView 的 id 值设置成 android.R.id.message
// 否则 Android 11 手机上在后台 toast.setText 的时候会出现报错
// java.lang.RuntimeException: This Toast was not created with Toast.makeText()
throw new IllegalArgumentException("You must set the ID value of TextView to android.R.id.message");
}
return (TextView) view;
Expand Down
Binary file modified picture/zh/help_doc_rename_vote.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 3088c10

Please sign in to comment.