Solutions not shown in toast above Android 5.0

Cause analysis

When users use Android 5.0 or above systems to install the app, they turn off the permission of message notification. In fact, the user only wants to close notification, but the show method of toast calls the inoticationmanager class, which is disabled when the user closes the message notification permission, so our toast cannot be displayed.

Toast.show()

design sketch

Custom toast (top) and toast (bottom) comparison

Problem solving

Since the system does not allow us to call toast, we will set up our own portal - write a toast ourselves. Our general idea is to add a view to the layout of the activity to achieve the toast effect.

Toast background shape definition

We know that the background of the shape is a translucent black fillet effect:

Therefore, we use the corners and solid nodes of shape:

Define layout

In the layout, we mainly use the textview control to set the corresponding margin and background. The layout code is as follows:

Java code logic

The Java code logic of the custom toast mainly imitates the two methods of maketext() and show() of the system toast. In addition, the reset() method is also required to switch the context when the activity switches during the toast display. The key codes are as follows:

Maketext (context, string message, int hide_delay) method:

Maketext (context, int resid, int hide_delay) method

Show() method

Method call

The use of custom toast is similar to that of system toast. The calling method is as follows:

summary

The above is the whole content of this article. I hope the content of this article can be helpful to Android developers. If you have any questions, you can leave a message.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>