Windows Manager for Android)

Source: Internet
Author: User

During work, some projects need to display the form at the top layer, such as calling pop-up window display phone numbers and other information, blocking text message information to the user or adjust the voice in the game, we think that when we put the data on the top layer, the activity will not meet our needs. Some developers use the loop display toast method, and toast cannot get the focus, this method is not advisable. What should we do at this time?
Originally, the window mechanism of the entire android system was based on a windowmanager interface. This interface can be used to add a View to the screen or delete a view from the screen. It targets the screen at one end of the object and view at the other end, directly ignoring our previous activity or dialog stuff. In fact, the underlying implementation of our activity or dialog is also implemented through windowmanager, which is global and the entire system is the only thing. It is the bottom layer of the display view.

Windowmanager is mainly used to manage the status, attributes, view addition, deletion, update, window order, and message Collection and Processing of windows. You can use context. getsystemservice (context. window_service) to obtain the windowmanager instance.

Windowmanager is inherited from ViewManager, which involves three important window management methods:

* Addview ();

* Updateviewlayout ();

* Removeview ();

 

As follows:

 

 

The code for moving the floating box is as follows:

Public class windowmanagedemoactivity extends activity {private windowmanager mwindowmanager; private windowmanager. layoutparams Param; private floatview mlayout;/** called when the activity is first created. * // @ override public void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. main); showview ();} private void showview () {mlayout = new floatview (getapplicationcontext (); mlayout. setbackgroundresource (R. drawable. faceback_head); // obtain windowmanager mwindowmanager = (windowmanager) getapplicationcontext (). getsystemservice (context. window_service); // set the layoutparams (global variable) parameter Param = (myapplication) getapplication ()). getmywmparams (); Param. type = windowmanager. layoutparams. type_system_alert; // system prompt type, important Param. format = 1; Param. flags = windowmanager. layoutparams. flag_not_focusable; // The cluster focus cannot be preemptible. flags = Param. flags | windowmanager. layoutparams. flag_watch_outside_touch; Param. flags = Param. flags | windowmanager. layoutparams. flag_layout_no_limits; // the layout is unrestricted. alpha = 1.0f; Param. gravity = gravity. left | gravity. top; // adjust the floating window to the upper left corner. // use the upper left corner of the screen as the origin and set the initial values of X and Y to Param. X = 0; Param. y = 0; // set the length and width of the floating window data Param. width = 140; Param. height = 140; // display the myfloatview image mwindowmanager. addview (mlayout, Param) ;}@ override public void ondestroy () {super. ondestroy (); // destroy the floating window mwindowmanager when the program exits (the activity is destroyed. removeview (mlayout );}}

Floatview code:

Public class floatview extends view {private float mtouchstartx; private float mtouchstarty; private float X; private float y; private windowmanager WM = (windowmanager) getcontext (). getapplicationcontext (). getsystemservice (context. window_service); Private windowmanager. layoutparams wmparams = (myapplication) getcontext (). getapplicationcontext ()). getmywmparams (); Public floatview (context) {super (context); // todo auto-generated constructor stub} @ override public Boolean ontouchevent (motionevent event) {// obtain the coordinates of the relative screen, that is, starting from the top left corner of the screen x = event. getrawx (); y = event. getrawy ()-25; // 25 is the height log of the system status bar. I ("currp", "currx" + x + "=== Curry" + Y); Switch (event. getaction () {Case motionevent. action_down: // obtain the coordinates of the relative view, that is, the upper-left corner of the view is the origin mtouchstartx = event. getx (); mtouchstarty = event. gety (); log. I ("startp", "startx" + mtouchstartx + "=== starty" + mtouchstarty); break; Case motionevent. action_move: updateviewposition (); break; Case motionevent. action_up: updateviewposition (); mtouchstartx = mtouchstarty = 0; break;} return true;} private void updateviewposition () {// update the floating window position parameter wmparams. X = (INT) (X-mtouchstartx); wmparams. y = (INT) (Y-mtouchstarty); WM. updateviewlayout (this, wmparams );}}

Finally, you need to add the following permissions to androidmanifest. XML to use a floating window:
<Uses-Permission Android: Name = "android. Permission. system_alert_window"/>


Myapplication code:

Public class myapplication extends application {/*** create global variable * global variables generally prefer to create a separate data class file, and use static variables ** here we use the method of adding data to the application to implement global variables * Note that in androidmanifest. add Android: Name = ". myapplication "Property **/private windowmanager. layoutparams wmparams = new windowmanager. layoutparams (); Public windowmanager. layoutparams getmywmparams () {return wmparams ;}}

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.