Skip to content

Commit 3803295

Browse files
committed
修复 SystemToast.setText 会出现 This Toast was not created with Toast.makeText 报错的问题
1 parent a045ef2 commit 3803295

File tree

6 files changed

+49
-52
lines changed

6 files changed

+49
-52
lines changed

README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,12 @@
1414

1515
```groovy
1616
buildscript {
17-
......
17+
repositories {
18+
maven { url 'https://jitpack.io' }
19+
}
1820
}
1921
allprojects {
2022
repositories {
21-
// JitPack 远程仓库:https://jitpack.io
2223
maven { url 'https://jitpack.io' }
2324
}
2425
}
@@ -37,7 +38,7 @@ android {
3738
3839
dependencies {
3940
// 吐司框架:https://github.com/getActivity/ToastUtils
40-
implementation 'com.github.getActivity:ToastUtils:9.1'
41+
implementation 'com.github.getActivity:ToastUtils:9.2'
4142
}
4243
```
4344

ToastUtils.apk

1 MB
Binary file not shown.

app/build.gradle

Lines changed: 6 additions & 3 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 30
10-
versionCode 91
11-
versionName "9.1"
10+
versionCode 92
11+
versionName "9.2"
1212
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
1313
}
1414
buildTypes {
@@ -28,13 +28,16 @@ android {
2828
dependencies {
2929
implementation fileTree(include: ['*.jar'], dir: 'libs')
3030
implementation project(':library')
31+
32+
// 谷歌兼容库:https://developer.android.google.cn/jetpack/androidx/releases/appcompat?hl=zh-cn
3133
implementation 'androidx.appcompat:appcompat:1.2.0'
34+
implementation 'com.google.android.material:material:1.2.1'
3235

3336
// 权限请求框架:https://github.com/getActivity/XXPermissions
3437
implementation 'com.github.getActivity:XXPermissions:10.8'
3538

3639
// 标题栏框架:https://github.com/getActivity/TitleBar
37-
implementation 'com.github.getActivity:TitleBar:8.5'
40+
implementation 'com.github.getActivity:TitleBar:8.6'
3841

3942
// 悬浮窗框架:https://github.com/getActivity/XToast
4043
implementation 'com.github.getActivity:XToast:6.9'

app/src/main/java/com/hjq/toast/demo/ToastActivity.java

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
import android.view.View;
99

1010
import androidx.appcompat.app.AppCompatActivity;
11-
import androidx.core.app.NotificationManagerCompat;
1211

12+
import com.google.android.material.snackbar.Snackbar;
1313
import com.hjq.permissions.Permission;
1414
import com.hjq.permissions.XXPermissions;
1515
import com.hjq.toast.ToastUtils;
@@ -63,13 +63,32 @@ public void show5(View v) {
6363
}
6464

6565
public void show6(View v) {
66-
// 推荐使用 XXPermissions 来申请通知栏权限:https://github.com/getActivity/XXPermissions
67-
if (NotificationManagerCompat.from(this).areNotificationsEnabled()) {
68-
Intent intent = new Intent(Intent.ACTION_MAIN);
69-
intent.addCategory(Intent.CATEGORY_HOME);
70-
startActivity(intent);
71-
v.postDelayed(new BackgroundRunnable(), 500);
66+
if (XXPermissions.isGranted(this, Permission.NOTIFICATION_SERVICE)) {
67+
68+
Snackbar.make(getWindow().getDecorView(), "正在准备跳转到手机桌面,请注意有极少数机型无法在后台显示 Toast", Snackbar.LENGTH_SHORT).show();
69+
70+
v.postDelayed(new Runnable() {
71+
@Override
72+
public void run() {
73+
Intent intent = new Intent(Intent.ACTION_MAIN);
74+
intent.addCategory(Intent.CATEGORY_HOME);
75+
startActivity(intent);
76+
}
77+
}, 2000);
78+
79+
v.postDelayed(new Runnable() {
80+
@Override
81+
public void run() {
82+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
83+
ToastUtils.show("我是在后台显示的 Toast(在 Android 11 上只能跟随系统 Toast 样式)");
84+
} else {
85+
ToastUtils.show("我是在后台显示的 Toast");
86+
}
87+
}
88+
}, 3000);
89+
7290
} else {
91+
7392
ToastUtils.show("在后台显示 Toast 需要先获取通知栏权限");
7493
v.postDelayed(new Runnable() {
7594
@Override
@@ -90,16 +109,4 @@ public void show7(View v) {
90109
.setYOffset((int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 50, getResources().getDisplayMetrics()))
91110
.show();
92111
}
93-
94-
private static class BackgroundRunnable implements Runnable {
95-
96-
@Override
97-
public void run() {
98-
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
99-
ToastUtils.show("我是在后台显示的 Toast(在 Android 11 上只能跟随系统 Toast 样式)");
100-
} else {
101-
ToastUtils.show("我是在后台显示的 Toast");
102-
}
103-
}
104-
};
105112
}

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 91
9-
versionName "9.1"
8+
versionCode 92
9+
versionName "9.2"
1010
}
1111

1212
// 使用 JDK 1.8

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

Lines changed: 11 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -89,34 +89,20 @@ public interface IToast {
8989
*/
9090
default TextView findMessageView(View view) {
9191
if (view instanceof TextView) {
92-
return (TextView) view;
93-
} else if (view.findViewById(android.R.id.message) instanceof TextView) {
94-
return ((TextView) view.findViewById(android.R.id.message));
95-
} else if (view instanceof ViewGroup) {
96-
TextView textView = findTextView((ViewGroup) view);
97-
if (textView != null) {
98-
return textView;
92+
if (view.getId() == View.NO_ID) {
93+
view.setId(android.R.id.message);
94+
} else if (view.getId() != android.R.id.message) {
95+
// 必须将 TextView 的 id 值设置成 android.R.id.message
96+
throw new IllegalArgumentException("You must set the ID value of TextView to android.R.id.message");
9997
}
98+
return (TextView) view;
10099
}
101-
// 如果设置的布局没有包含一个 TextView 则抛出异常,必须要包含一个 TextView 作为 MessageView
102-
throw new IllegalArgumentException("The layout must contain a TextView");
103-
}
104100

105-
/**
106-
* 递归获取 ViewGroup 中的 TextView 对象
107-
*/
108-
default TextView findTextView(ViewGroup group) {
109-
for (int i = 0; i < group.getChildCount(); i++) {
110-
View view = group.getChildAt(i);
111-
if ((view instanceof TextView)) {
112-
return (TextView) view;
113-
} else if (view instanceof ViewGroup) {
114-
TextView textView = findTextView((ViewGroup) view);
115-
if (textView != null) {
116-
return textView;
117-
}
118-
}
101+
if (view.findViewById(android.R.id.message) instanceof TextView) {
102+
return ((TextView) view.findViewById(android.R.id.message));
119103
}
120-
return null;
104+
105+
// 如果设置的布局没有包含一个 TextView 则抛出异常,必须要包含一个 id 值成 android.R.id.message 的 TextView
106+
throw new IllegalArgumentException("You must include a TextView with an ID value of android.R.id.message");
121107
}
122108
}

0 commit comments

Comments
 (0)