Skip to content

Commit 0b1ba51

Browse files
committed
修复在部分 Android 11 后台弹 Toast 出现崩溃的问题
1 parent 59be1b1 commit 0b1ba51

File tree

1 file changed

+5
-6
lines changed

1 file changed

+5
-6
lines changed

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

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -174,12 +174,11 @@ public void cancelToast() {
174174
* 是否支持设置自定义 Toast 样式
175175
*/
176176
protected boolean areSupportCustomToastStyle(IToast toast) {
177-
// targetSdkVersion >= 30 的情况下在后台显示自定义样式的 Toast 会被系统屏蔽,并且日志会输出以下警告:
177+
// sdk 版本 >= 30 的情况下在后台显示自定义样式的 Toast 会被系统屏蔽,并且日志会输出以下警告:
178178
// Blocking custom toast from package com.xxx.xxx due to package not in the foreground
179-
// targetSdkVersion < 30 的情况下 new Toast,并且不设置视图显示,系统会抛出以下异常:
179+
// sdk 版本 < 30 的情况下 new Toast,并且不设置视图显示,系统会抛出以下异常:
180180
// java.lang.RuntimeException: This Toast was not created with Toast.makeText()
181-
return toast instanceof CustomToast || Build.VERSION.SDK_INT < Build.VERSION_CODES.R ||
182-
mApplication.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.R;
181+
return toast instanceof CustomToast || Build.VERSION.SDK_INT < Build.VERSION_CODES.R;
183182
}
184183

185184
/**
@@ -267,7 +266,7 @@ protected boolean onlyShowSystemToastStyle() {
267266
protected boolean isChangeEnabledCompat(long changeId) {
268267
// 需要注意的是这个 api 是在 android 11 的时候出现的,反射前需要先判断好版本
269268
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.R) {
270-
return true;
269+
return false;
271270
}
272271
try {
273272
// 因为 Compatibility.isChangeEnabled() 普通应用根本调用不到,反射也不行
@@ -279,8 +278,8 @@ protected boolean isChangeEnabledCompat(long changeId) {
279278
return Boolean.parseBoolean(String.valueOf(method.invoke(null, changeId)));
280279
} catch (ClassNotFoundException | InvocationTargetException | NoSuchMethodException | IllegalAccessException e) {
281280
e.printStackTrace();
281+
return false;
282282
}
283-
return false;
284283
}
285284

286285
/**

0 commit comments

Comments
 (0)