Idea of developing your own toast System on Android

Source: Internet
Author: User

It is found that there is a problem with the system's toast cancel mechanism. Once the previous cancel is removed and a new toast is displayed, the new toast will be stuck for a period of time and cannot be displayed.

Note the following when creating a toast system:
1. Create a service dedicated to display, hide, etc.
2. Use addview and removeview of windowmanager in the service for display and hide
3. Pay attention to the flag, format, type, and other parameters to be set.
4. Add the following permissions to androidmanifest. xml:

<Uses-Permission Android: Name = "android. Permission. system_alert_window"/>

Without this permission, the focus of the current window of the system will be taken away.

KeyCodeAs follows:

Public void show (context, string content) {windowmanager = (windowmanager) Context. getsystemservice (context. window_service); If (TV! = NULL) {windowmanager. removeview (TV);} windowmanager. layoutparams Params = new windowmanager. layoutparams (); Params. height = windowmanager. layoutparams. wrap_content; Params. width = windowmanager. layoutparams. wrap_content; Params. Y + = 20; Params. flags = windowmanager. layoutparams. flag_not_focusable | windowmanager. layoutparams. flag_not_touchable; Params. format = pixelformat. translucent; Params. type = windowmanager. layoutparams. type_toast; layoutinflater = (layoutinflater) context. getsystemservice (context. layout_inflater_service); TV = (textview) layoutinflater. inflate (R. layout. renren_toast, null); TV. settext (content); windowmanager. addview (TV, Params );}

Supplement: 2011.11.18

After finding the solution, you can directly take the original toast and reset the display text and time.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.