Skip to content

Commit

Permalink
修复 SystemToast.setText 会出现 This Toast was not created with Toast.make…
Browse files Browse the repository at this point in the history
…Text 报错的问题
  • Loading branch information
getActivity committed May 12, 2021
1 parent a045ef2 commit 3803295
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 52 deletions.
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,12 @@

```groovy
buildscript {
......
repositories {
maven { url 'https://jitpack.io' }
}
}
allprojects {
repositories {
// JitPack 远程仓库:https://jitpack.io
maven { url 'https://jitpack.io' }
}
}
Expand All @@ -37,7 +38,7 @@ android {
dependencies {
// 吐司框架:https://github.com/getActivity/ToastUtils
implementation 'com.github.getActivity:ToastUtils:9.1'
implementation 'com.github.getActivity:ToastUtils:9.2'
}
```

Expand Down
Binary file modified ToastUtils.apk
Binary file not shown.
9 changes: 6 additions & 3 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 30
versionCode 91
versionName "9.1"
versionCode 92
versionName "9.2"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
Expand All @@ -28,13 +28,16 @@ android {
dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation project(':library')

// 谷歌兼容库:https://developer.android.google.cn/jetpack/androidx/releases/appcompat?hl=zh-cn
implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'com.google.android.material:material:1.2.1'

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

// 标题栏框架:https://github.com/getActivity/TitleBar
implementation 'com.github.getActivity:TitleBar:8.5'
implementation 'com.github.getActivity:TitleBar:8.6'

// 悬浮窗框架:https://github.com/getActivity/XToast
implementation 'com.github.getActivity:XToast:6.9'
Expand Down
45 changes: 26 additions & 19 deletions app/src/main/java/com/hjq/toast/demo/ToastActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
import android.view.View;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.NotificationManagerCompat;

import com.google.android.material.snackbar.Snackbar;
import com.hjq.permissions.Permission;
import com.hjq.permissions.XXPermissions;
import com.hjq.toast.ToastUtils;
Expand Down Expand Up @@ -63,13 +63,32 @@ public void show5(View v) {
}

public void show6(View v) {
// 推荐使用 XXPermissions 来申请通知栏权限:https://github.com/getActivity/XXPermissions
if (NotificationManagerCompat.from(this).areNotificationsEnabled()) {
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
startActivity(intent);
v.postDelayed(new BackgroundRunnable(), 500);
if (XXPermissions.isGranted(this, Permission.NOTIFICATION_SERVICE)) {

Snackbar.make(getWindow().getDecorView(), "正在准备跳转到手机桌面,请注意有极少数机型无法在后台显示 Toast", Snackbar.LENGTH_SHORT).show();

v.postDelayed(new Runnable() {
@Override
public void run() {
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
startActivity(intent);
}
}, 2000);

v.postDelayed(new Runnable() {
@Override
public void run() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
ToastUtils.show("我是在后台显示的 Toast(在 Android 11 上只能跟随系统 Toast 样式)");
} else {
ToastUtils.show("我是在后台显示的 Toast");
}
}
}, 3000);

} else {

ToastUtils.show("在后台显示 Toast 需要先获取通知栏权限");
v.postDelayed(new Runnable() {
@Override
Expand All @@ -90,16 +109,4 @@ public void show7(View v) {
.setYOffset((int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 50, getResources().getDisplayMetrics()))
.show();
}

private static class BackgroundRunnable implements Runnable {

@Override
public void run() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
ToastUtils.show("我是在后台显示的 Toast(在 Android 11 上只能跟随系统 Toast 样式)");
} else {
ToastUtils.show("我是在后台显示的 Toast");
}
}
};
}
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 91
versionName "9.1"
versionCode 92
versionName "9.2"
}

// 使用 JDK 1.8
Expand Down
36 changes: 11 additions & 25 deletions library/src/main/java/com/hjq/toast/config/IToast.java
Original file line number Diff line number Diff line change
Expand Up @@ -89,34 +89,20 @@ public interface IToast {
*/
default TextView findMessageView(View view) {
if (view instanceof TextView) {
return (TextView) view;
} else if (view.findViewById(android.R.id.message) instanceof TextView) {
return ((TextView) view.findViewById(android.R.id.message));
} else if (view instanceof ViewGroup) {
TextView textView = findTextView((ViewGroup) view);
if (textView != null) {
return textView;
if (view.getId() == View.NO_ID) {
view.setId(android.R.id.message);
} else if (view.getId() != android.R.id.message) {
// 必须将 TextView 的 id 值设置成 android.R.id.message
throw new IllegalArgumentException("You must set the ID value of TextView to android.R.id.message");
}
return (TextView) view;
}
// 如果设置的布局没有包含一个 TextView 则抛出异常,必须要包含一个 TextView 作为 MessageView
throw new IllegalArgumentException("The layout must contain a TextView");
}

/**
* 递归获取 ViewGroup 中的 TextView 对象
*/
default TextView findTextView(ViewGroup group) {
for (int i = 0; i < group.getChildCount(); i++) {
View view = group.getChildAt(i);
if ((view instanceof TextView)) {
return (TextView) view;
} else if (view instanceof ViewGroup) {
TextView textView = findTextView((ViewGroup) view);
if (textView != null) {
return textView;
}
}
if (view.findViewById(android.R.id.message) instanceof TextView) {
return ((TextView) view.findViewById(android.R.id.message));
}
return null;

// 如果设置的布局没有包含一个 TextView 则抛出异常,必须要包含一个 id 值成 android.R.id.message 的 TextView
throw new IllegalArgumentException("You must include a TextView with an ID value of android.R.id.message");
}
}

0 comments on commit 3803295

Please sign in to comment.