Android instance-Mobile security Defender (39)-Custom Toast (text format, display window)

Source: Internet
Author: User

First, the goal

Custom display of toast, including content text format, display window format.

Second, the Code implementation

1. Customize the Toast method (named Mytoast (String Sting)) in the "Show Number Attribution" Service Class (Showphoneaddservice). The argument string is a string that needs to be displayed (note: In this example, you only need to use a custom toast to display the number attribution, not elsewhere, so create a method in the class). In the custom method (Mytoast):

(1) Define a TextView object (named view) through the new TextView (context context) method, with the context of the application (Getapplicationcontext ());

(2) through the various set methods of TextView (named TextView) to pass in and beautify the string to be displayed (Sting);

(3) A Windowmanager.layoutparams object (named params) is instantiated with the Layoutparams () object under the new WindowManager interface, which is used to set custom toast display window parameters;

(4) Set its individual properties by means of the ". Property = value" of the Windowmanager.layoutparams object (params):

① window width, height (width, height) are package contents (WindowManager.LayoutParams.WRAP_CONTENT)

The Flags property of the ② window is set to not get focus (WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE), not Touch (WindowManager.LayoutParams.FLAG_NOT_ touchable), keep the screen turned on (WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON) equivalent (multiple values of the same attribute are available and the ' | ' Connection

③ window style (format) is translucent (pixelformat.translucent);

④ window types (type) are toast (WindowManager.LayoutParams.TYPE_TOAST);

(5) Define the member variables of the window manager WindowManager object (named WM) in the service class (Showphoneaddservice) and pass the Getsystemservice (String name) in the Create (OnCreate) method Method Instantiation (Parameter name for window service Window_service), strong turn. Then in the custom Toast Method (Mytoast) through the window Manager WindowManager object (WM) AddView (view view, Layoutparams params) Method loads the parameters of this view (view) and display window (params) to the window manager for display.

Custom Toast Method Code:

1  Public voidmytoast (String string) {2         //set up display text content and UI for toast3TextView view =NewTextView (Getapplicationcontext ());4 View.settext (string);5 View.settextcolor (color.red);6View.settextsize (25);7         //setting display window parameters for toast8Windowmanager.layoutparams params =NewWindowmanager.layoutparams ();//Creating a Window object9Params.height = WindowManager.LayoutParams.WRAP_CONTENT;//Set window height for package contentsTenParams.width = WindowManager.LayoutParams.WRAP_CONTENT;//Set window width to package contents OneParams.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE//mark the window as not getting focus A| WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE//mark a window as not touchable -| WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON;//mark a window to keep the screen open -Params.format = pixelformat.translucent;//window style is translucent theParams.type = WindowManager.LayoutParams.TYPE_TOAST;//window type is toast -         //loading into the window Manager via text and Windows - Wm.addview (view, params); -}
View Code

2, in the service class (Showphoneaddservice) in the custom Call monitoring Class (Myphonelistener) and the power-off service class (Outcallreceiver), by querying the database to obtain the number attribution, The attribution can be displayed by a custom toast method.

Incoming code;

1 mytoast (incomingphoneadd);
View Code

Android instance-Mobile security Defender (39)-Custom Toast (text format, display window)

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.