ToastHandler

所属分类:Kotlin编程
开发工具:kotlin
文件大小:0KB
下载次数:0
上传日期:2021-03-09 09:04:05
上 传 者sh-1993
说明:  用于处理Android API级别25上发生的Toast BadTokenException的Android库_and_平滑显示Toast...
(An Android library for handling Toast BadTokenException happening on Android API level 25 and showing Toast smoothly on All Android versions.)

文件列表:
.idea/ (0, 2020-11-03)
.idea/$CACHE_FILE$ (1055, 2020-11-03)
.idea/codeStyles/ (0, 2020-11-03)
.idea/codeStyles/Project.xml (4353, 2020-11-03)
.idea/codeStyles/codeStyleConfig.xml (142, 2020-11-03)
.idea/compiler.xml (169, 2020-11-03)
.idea/gradle.xml (932, 2020-11-03)
.idea/jarRepositories.xml (1240, 2020-11-03)
.idea/misc.xml (528, 2020-11-03)
.idea/runConfigurations.xml (564, 2020-11-03)
.idea/vcs.xml (180, 2020-11-03)
LICENSE (11357, 2020-11-03)
app/ (0, 2020-11-03)
app/build.gradle (1622, 2020-11-03)
app/proguard-rules.pro (750, 2020-11-03)
app/release/ (0, 2020-11-03)
app/release/output.json (234, 2020-11-03)
app/src/ (0, 2020-11-03)
app/src/androidTest/ (0, 2020-11-03)
app/src/androidTest/java/ (0, 2020-11-03)
app/src/androidTest/java/com/ (0, 2020-11-03)
app/src/androidTest/java/com/sample/ (0, 2020-11-03)
app/src/androidTest/java/com/sample/toasthandler/ (0, 2020-11-03)
app/src/androidTest/java/com/sample/toasthandler/ExampleInstrumentedTest.kt (673, 2020-11-03)
app/src/main/ (0, 2020-11-03)
app/src/main/AndroidManifest.xml (854, 2020-11-03)
app/src/main/java/ (0, 2020-11-03)
app/src/main/java/com/ (0, 2020-11-03)
app/src/main/java/com/sample/ (0, 2020-11-03)
app/src/main/java/com/sample/toasthandler/ (0, 2020-11-03)
app/src/main/java/com/sample/toasthandler/MainActivity.kt (1506, 2020-11-03)
app/src/main/res/ (0, 2020-11-03)
app/src/main/res/drawable-v24/ (0, 2020-11-03)
app/src/main/res/drawable-v24/ic_launcher_foreground.xml (1702, 2020-11-03)
... ...

You can raise PR for any contribution and optimization. :-) # ToastHandler Reference from [StackOverFlow](https://stackoverflow.com/questions/51532449/fatal-exception-android-view-windowmanagerbadtokenexception-unable-to-add-wind) An Android library wrapper over [ToastCompat](https://github.com/PureWriter/ToastCompat) for handling Toast BadTokenException happening on Android API level 25 and showing Toast smoothly on All Android versions. (Handling some memory leaks and changed to Kotlin with Custom Lint added) Please refer this [blog](https://proandroiddev.com/how-i-resolved-windowmanager-badtokenexception-for-toast-handleshow-2308203ebb91) for more details and findings. ### Usage Add this to your app build.gradle: ```groovy implementation "com.toastfix:toastcompatwrapper:1.2.0" ``` Use this wherever you are showing Toast: Java ```java ToastHandler.showToast(this, "Hello,I am Toast", Toast.LENGTH_SHORT); ``` Kotlin ```kotlin ToastHandler.showToast(this, "Hello,I am Toast", Toast.LENGTH_SHORT) ```
Also, If you forget to use it, Let the Android Lint help you :-) Latest Feature: Added a custom lint which disallow the usage of Android's [Toast](https://developer.android.com/reference/android/widget/Toast) class in favor of `ToastHandler`. These are the properties of the custom lint. ```kotlin message = "Usage of android Toast is prohibited" briefDescription = "The android Toast should not be used" explanation = "The android Toast should not be used, use ToastHandler instead to avoid BadTokenException on Android API level 25" category = Category.CORRECTNESS priority = 6 severity = Severity.WARNING ``` The custom lint is added in the `toasthandler` module as `lintPublish`. So, adding the library dependency into any project will also include this custom lint. This lint replaces `Toast.makeText` with `ToastHandler.getToastInstance`. ```diff - Toast.makeText(this, "Hi, I am Toast", Toast.LENGTH_SHORT).show() + ToastHandler.getToastInstance(this, "Hi, I am Toast", Toast.LENGTH_SHORT).show() ``` Here's a small GIF showing how this will look in the IDE. ![lint](https://user-images.githubusercontent.com/22273871/94834413-fa0b0e00-042d-11eb-8dda-26087568176e.gif) Please refer the demo for usage. ### Why From API 25, Android added a new param `IBinder windowToken` for `Toast#handleShow()`, and It brought an exception. As Android said on API 26: ```java // Since the notification manager service cancels the token right // after it notifies us to cancel the toast there is an inherent // race and we may attempt to add a window after the token has been // invalidated. Let us hedge against that. ``` So they try-catch the `mWM.addView(mView, mParams)` on API 26. **However, API 25 is still at risk. Our applications will continue to produce such an exception, and can not capture it**: ```java Fatal Exception: android.view.WindowManager$BadTokenException: Unable to add window -- token android.os.BinderProxy@1c4411f is not valid; is your activity running? at android.view.ViewRootImpl.setView(ViewRootImpl.java:679) at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:342) at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:94) at android.widget.Toast$TN.handleShow(Toast.java:459) at android.widget.Toast$TN$2.handleMessage(Toast.java:342) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:154) at android.app.ActivityThread.main(ActivityThread.java:6236) at java.lang.reflect.Method.invoke(Method.java) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:891) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:781) ``` This exception occurs regardless of whether the `Context` you passed to `Toast` is an `Activity` or `ApplicationContext` or `Service`. And you can not `try-catch` it. See the detail diff of **Android Toast sources**: ![ToastDiff.png](art/ToastDiff.png) Have also handled some memory leaks and created ToastHandler as Singleton(lazy-loading) for better performance. This project is CI configured using Github Actions.

近期下载者

相关文件


收藏者