[Android] 10, hover window and get other task information

Source: Internet
Author: User

Here is a floating window and use the floating window to display the current top-level application information ~ a need to write 2 layout and 2 Java

In Mainactivity, the listener event is mainly those two buttons, and the other activity is started and closed by intent.

1  Public classMainactivityextendsActivity {2 @Override3      Public voidonCreate (Bundle savedinstancestate) {4         Super. OnCreate (savedinstancestate);5 Setcontentview (r.layout.main);6         //layout View button for Floatingwindowactivity7Button start =(Button) Findviewbyid (r.id.start_id);8Button remove =(Button) Findviewbyid (r.id.remove_id);9Start.setonclicklistener (NewOnclicklistener () {Ten @Override One              Public voidOnClick (View v) { A                 //TODO auto-generated Method Stub -Intent Intent =NewIntent (mainactivity. This, Fxservice.class); - StartService (intent); the finish (); -             } -         }); -  +Remove.setonclicklistener (NewOnclicklistener () { - @Override +              Public voidOnClick (View v) { AIntent Intent =NewIntent (mainactivity. This, Fxservice.class); at StopService (intent); -             } -         }); -  -     } -}

The other fxservice is the complete implementation of the suspended window: The core is implemented in the Createfloatview () function, because this function is called in OnCreate. In this function, some of the properties of the floating window are set first, then the touch and click events are written, and the touch event is mainly the mobile Hover window position, the Click event is mainly to get the activity of the upper packagename[80-82], then the process name is obtained according to the package name [84-86], The following is an update of the suspended window display and toast based on the information obtained.

1 Private voidCreatefloatview () {2Wmparams =Newwindowmanager.layoutparams ();3     //Get Windowmanagerimpl.compatmodewrapper4Mwindowmanager =(WindowManager) getapplication (). Getsystemservice (5 getapplication (). Window_service);6     //Set Window type7Wmparams.type =Layoutparams.type_phone;8     //format picture, effect is transparent background9Wmparams.format =pixelformat.rgba_8888;Ten     //set the floating window to be non-focusable (operations that enable operation of other visible windows except floating windows) OneWmparams.flags = A     //Layoutparams.flag_not_touch_modal | - layoutparams.flag_not_focusable -     //layoutparams.flag_not_touchable the     ; -  -     //Adjust the hover window to display the docked position to the left top -wmparams.gravity = Gravity.left |Gravity.top; +  -     //set X, y initial values as the origin in the upper-left corner of the screen +wmparams.x = 0; AWMPARAMS.Y = 0; at  -     /* - *//Set the hover window length-width data Wmparams.width = Wmparams.height = x ; -      */ -  -     //Setting the hover window length-width data inWmparams.width =WindowManager.LayoutParams.WRAP_CONTENT; -Wmparams.height =WindowManager.LayoutParams.WRAP_CONTENT; to  +Layoutinflater Inflater =Layoutinflater.from (Getapplication ()); -     //get the layout of a floating window view theMfloatlayout =(LinearLayout) inflater.inflate (R.layout.float_layout, *             NULL); $     //Add MfloatlayoutPanax Notoginseng Mwindowmanager.addview (Mfloatlayout, wmparams); -  theLOG.I (TAG, "mfloatlayout-->left" +mfloatlayout.getleft ()); +LOG.I (TAG, "mfloatlayout-->right" +mfloatlayout.getright ()); ALOG.I (TAG, "mfloatlayout-->top" +mfloatlayout.gettop ()); theLOG.I (TAG, "Mfloatlayout-->bottom" +Mfloatlayout.getbottom ()); +  -     //Floating window Buttons $Mfloatview =(Button) Mfloatlayout.findviewbyid (r.id.float_id); $Mfloatlayout.measure (View.MeasureSpec.makeMeasureSpec (0, - View.MeasureSpec.UNSPECIFIED), View.measurespec -. Makemeasurespec (0, View.MeasureSpec.UNSPECIFIED)); theLOG.I (TAG, "WIDTH/2--->" + mfloatview.getmeasuredwidth ()/2); -LOG.I (TAG, "HEIGHT/2--->" + mfloatview.getmeasuredheight ()/2);Wuyi     //set the touch movement of the Monitor floating window theMfloatview.setontouchlistener (NewOntouchlistener () { -  Wu @Override -          Public BooleanOnTouch (View V, motionevent event) { About             //TODO auto-generated Method Stub $             //GETRAWX is the coordinate of the touch position relative to the screen, and the getx is relative to the button's coordinates -Wmparams.x = (int) event.getrawx () --Mfloatview.getmeasuredwidth ()/2; -             //log.i (TAG, "WIDTH/2--->" + mfloatview.getmeasuredwidth ()/2); ALOG.I (TAG, "rawx" +event.getrawx ()); +LOG.I (TAG, "X" +event.getx ()); the             //25 is the height of the status bar -Wmparams.y = (int) Event.getrawy () $-Mfloatview.getmeasuredheight ()/2-25; the             //log.i (TAG, "WIDTH/2--->" + mfloatview.getmeasuredheight ()/2); theLOG.I (TAG, "Rawy" +Event.getrawy ()); theLOG.I (TAG, "Y" +event.gety ()); the             //Refresh - mwindowmanager.updateviewlayout (Mfloatlayout, wmparams); in             return false; the         } the     }); About  theMfloatview.setonclicklistener (NewOnclicklistener () { the @Override the          Public voidOnClick (View v) {//shows which app is currently on the top level +             //TODO auto-generated Method Stub -PM =Getpackagemanager (); theAM =(Activitymanager) Getsystemservice (context.activity_service);BayiString PackageName = am.getrunningtasks (1). Get (0). Topactivity the . Getpackagename (); the             Try { -String Progressname =Pm.getapplicationlabel ( - Pm.getapplicationinfo (PackageName, the packagemanager.get_meta_data)). ToString (); theMfloatview =(Button) mfloatlayout the . Findviewbyid (r.id.float_id); the Mfloatview.settext (progressname); -Toast.maketext (Fxservice. This, Progressname, the toast.length_short). Show (); the}Catch(namenotfoundexception e) { the                 //TODO auto-generated Catch block94 e.printstacktrace (); the             } the         } the     });98}

Note: If you want to get the topactivity of your system, you must also get permissions:

<uses-permission android:name= "Android.permission.GET_TASKS"/>

<uses-permission android:name= "Android.permission.SYSTEM_ALERT_WINDOW"/>

This article link: http://www.cnblogs.com/zjutlitao/p/4231525.html

More Highlights: http://www.cnblogs.com/zjutlitao/

Github:https://github.com/beautifulzzzz/android/tree/master/floatingwindow

[Android] 10, hover window and get other task information

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.