Android instance-Mobile security Defender (43)-Free Mobile custom toast Location

Source: Internet
Author: User

First, the goal

Implementing a custom Toast window is free to move to any location on the screen when you call, and save the location

Second, the Code implementation

1. In the "Show Number Attribution" Service (Showphoneaddservice) class, in the custom Toast (Mytoast) method, In the note (flag) attribute of the Window object Windowmanager.layoutparams (params, which is needed to move the display position later, so it is defined as a member variable of the service class), To delete a non-touch attribute (WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE), set it to a type with a telephone priority in the Type property ( Windowmanager.layoutparams.type_priority_phone,toast type object is not touch at design time, increase the System window Popup permission (system_alert_ window) (does not increase this permission at run time without an error prompt, only the permission is denied prompt permission denied for this window type);

The relevant property code in the params:

 1  params.flags = Windowmanager.layoutparams.flag_not_focusable//  2  //  window is not touch marked WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE  3  | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON; //  4  params.format = pixelformat.translucent; //  The window style is translucent  5  params.type = Windowmanager.layoutparams.type_priority_phone;//  window type is a type with phone priority  
View Code

2. After the view object is obtained through the View object (view) inflate () method, the View object is

The Setontouchlistener (Ontouchlistener L) method sets the touch listener event for it, and the parameter Ontouchlistener L instantiates the Ontouchlistener () object listener through new, The Ontouch (View V, motionevent event) method is also replicated, and a variable of six int (named Startx,starty,newx,newy,dx,dy) is defined in Ontouchlistener (). The initial x-axis coordinates, y-axis coordinates, the new x-axis coordinates, the new y-axis coordinates, the x-axis movement distance, the y-axis movement distance, respectively, as the finger touches the screen, 3, in the Ontouch (View V, motionevent event) method, via switch ... Case ... The statement determines the action of the event (Event.getaction ()), (1) The Move Event Motionevent Object (event) passed through the Ontouch method when the event action is a finger-pressed screen (Motionevent.action_down).The getrawx () method gets the initial x-coordinate pixel position of the toast window when the finger presses the screen (note: The origin of this axis is the upper-left corner of the screen), and the coordinates of the y-axis are obtained similarly;(2) When an event action is moved by the finger on the screen (motionevent.action_move), ① gets the new x-axis of the toast window through the getrawx (), Getrawy () method of the Motionevent object (event),      The y-coordinate pixel position, gets the new axis position and the start axis position, and adds it to the x-axis of the toast window, plus the y-axis position, and then assigns it to the toast window. Y-axis; ② is set to 0 by judging the x-axis, y-coordinate position of the Window object Windowmanager.layoutparams (params) if it is less than 0 (that is, the toast window reaches the left or top edge of the screen). If it is greater than the difference between the screen width and the width of the toast window (that is, the toast window reaches the right or bottom edge), it is set to that difference; ③ through the Window management Object (WM)updateviewlayout (View view, layoutparams params) method updates the Toast window display position, the parameter view is the loaded View object (view), The params is the Window object Windowmanager.layoutparams (params); ④ the GETRAWX (), Getrawy () method of the event to retrieve the coordinates of the toast window and assign values to the initial x-axis, y-coordinate (StartX, starty);(3) When the event action for the finger leaves the screen instantly (MOTIONEVENT.ACTION_UP) Saves the window object through the Putint () method of the Sharedpreferences object (SP) Windowmanager.layoutparams (     params) x-axis, y-axis coordinates. (4) Returns True to allow the parent control to touch the event appropriately, and to set the code for the Touch listener event for the View object:
1View.setontouchlistener (NewOntouchlistener () {2             intStartx,starty,newx,newy,dx,dy;3                                 4 @Override5              Public BooleanOnTouch (View V, motionevent event) {6                 Switch(Event.getaction ()) {7                  CaseMotionevent.action_down://when your finger touches the screen8StartX = (int) event.getrawx ();//gets the initial x-axis position when the finger touches the screen9Starty = (int) Event.getrawy ();//gets the initial y-axis position when the finger touches the screenTen                      Break; One                  CaseMotionevent.action_move://finger moves on the screen ANewx = (int) event.getrawx (); -Newy = (int) Event.getrawy (); -DX = newx-StartX; theDY = Newy-Starty; -Params.x + =DX; -Params.y + =DY; -                     //boundary, whose x-axis coordinates are 0 when the toast reaches the left boundary, and its y-axis is the screen width minus the toast width when the right boundary is reached.  +                     if(params.x<0){ -params.x = 0; +                     } A                     if(Params.x> (Wm.getdefaultdisplay (). GetWidth ()-view.getwidth ())) { atParams.x = Wm.getdefaultdisplay (). GetWidth ()-view.getwidth (); -                     } -                     if(params.y<0){ -PARAMS.Y = 0; -                     } -                     if(Params.y> (Wm.getdefaultdisplay (). GetHeight ()-view.getheight ())) { inParams.y = Wm.getdefaultdisplay (). GetHeight ()-view.getheight (); -                     } toWm.updateviewlayout (view, params);//Update View Object layout +StartX = (int) event.getrawx ();//initial x-axis position when you re-initialize your finger touch screen -Starty = (int) Event.getrawy ();//initial y-axis position when you re-initialize your finger touch screen the                      Break; *                  CaseMOTIONEVENT.ACTION_UP://Finger left screen instantly $                     //the location where the toast is saved, and the next time the call is displayed based on the location of the last movePanax NotoginsengEditor Editor =Sp.edit (); -Editor.putint ("Toastx", params.x); theEditor.putint ("Toasty", params.y); + editor.commit (); A                      Break; the                 } +                 return false; -             } $});
View Code

4. When setting the Toast window display location (params.x, params.y), the Getint () method of the Sharedpreferences object (SP) gets the display location that has been saved.

Gets the saved display location code from the configuration file;

1         Params.x = Sp.getint ("Toastx", 0); // sets the offset of the left edge of the toast window from the left border of the screen window to 50 pixels 2         Params.y = Sp.getint ("toasty", 0); // sets the offset of the top edge of the toast window from the upper boundary of the screen window to 50 pixels
View Code

Android instance-Mobile security Defender (43)-Free Mobile custom toast Location

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.