Android Floating window Implementation of the source code

Source: Internet
Author: User

Android Floating window Implementation of the source code

Reprint please indicate source: http://blog.csdn.net/pcaxb/article/details/48048529

The implementation of the suspended window is implemented with a service, custom view, Activity


The permission used to implement the suspended window:

    <!--the permissions required for the 360 floating plugin--    <uses-permission android:name= "Android.permission.SYSTEM_ALERT_WINDOW"/>    <uses-permission android:name= "Com.android.vending.BILLING"/>


Registering services in a manifest file

<service android:name= "Com.pca.nettraffic.MserServes"/>

Implementation of the Code

1.Activity of source code

Package Com.pca.nettraffic;import Android.app.activity;import Android.content.intent;import android.os.Bundle; Import Android.view.view;import Android.view.view.onclicklistener;import Android.widget.button;public class Widgetmainactivity extends Activity {//a activityprivate button with only one button btn; @Overrideprotected void OnCreate (Bundle Savedinstancestate) {//TODO auto-generated method Stubsuper.oncreate (savedinstancestate); Setcontentview ( R.layout.activity_imitate_widget_main) btn = (Button) Findviewbyid (R.ID.BTN); Btn.setonclicklistener (new Onclicklistener () {@Overridepublic void OnClick (View v) {//TODO auto-generated method Stubfunclick ();}});} public void Funclick () {//button is clicked This.startservice (New Intent (Getapplicationcontext (), mserserves.class));//New Tableshowview (This). Fun (); If it is only activated in activity//When the activity is running to the background [suspend state, or destroy State] The view we set to the desktop will disappear//So here is the start of a service, the service creates the view that we need to display on the table, and register it on WindowManager this.finish ();}}

2.Service Source Code

Package Com.pca.nettraffic;import Android.app.service;import Android.content.intent;import android.os.IBinder; public class Mserserves extends service {   //Services//      This kind of pure egg is used only in order to open a service after the activity clicks the button             @Override Public      IBinder Onbind (Intent Intent) {          //TODO auto-generated method stub          return null;      }        public void OnCreate () {      //creates a service when a Tableshowview object is instantiated and calls his fun () method to register it on          WindowManager Super.oncreate ();          New Tableshowview (Getapplicationcontext ()). Fun ();          System.out.println ("Open mser service ...");          @Override public      int Onstartcommand (Intent Intent, int flags, int startid) {          //TODO auto-generated method stub< C16/>return Super.onstartcommand (Intent, flags, Startid);      }  }  

3. Custom View Source (Tableshowview extends view)

Package Com.pca.nettraffic;import Android.content.context;import Android.graphics.color;import Android.view.layoutinflater;import Android.view.motionevent;import Android.view.view;import Android.view.windowmanager;import Android.widget.toast;public class Tableshowview extends View {// If you want to display lyrics then inherit the TextView and copy the OnDraw method. Open a thread to constantly call the OnDraw method to change what you have written to inherit from TextView.//Here is a random integration from the view of the = = This is not the focus of CONTEXTC; windowmanagermwm;//windowmanagerwindowmanager.layoutparamsmwmparams;//windowmanager parameter viewwin;int tag = 0;int Oldoffsetx;int Oldoffsety;public Tableshowview (Context context) {//TODO auto-generated Constructor Stubsuper (context) ; c = Context;} public void Fun () {//Set load view WindowManager parameter MWM = (WindowManager) c.getsystemservice (context.window_service); win = Layoutinflater.from (c). Inflate (R.layout.activity_imitate_widget_ctrl_window, null); Win.setbackgroundcolor ( color.transparent);//This is a random loading of a layout file Win.setontouchlistener (new Ontouchlistener () {//touchscreen monitoring FLOATLASTX, lasty;public Boolean OnTouch (View V,Motionevent event) {Final int action = Event.getaction (); float x = Event.getx (); Float y = event.gety (); if (tag = = 0) {old Offsetx= mwmparams.x; Offset oldoffsety = mwmparams.y; Offset} if (action = = motionevent.action_down) {lastx = X;lasty = y;} else if (action = = motionevent.action_move) {mwmparams.x + = (int) (X-LASTX);//offset Mwmparams.y + = (int) (y-lasty);// Offset tag = 1;mwm.updateviewlayout (win, mwmparams);} else if (action = = motionevent.action_up) {int newoffsetx = Mwmparams.x;int newoffsety = mwmparams.y;if (Oldoffsetx = = NewO Ffsetx && oldoffsety = = newoffsety) {Toast.maketext (c), "You've ordered me ... Hurt!!!! ", 1). Show ();} else {tag = 0;}} return true;}}); WindowManager wm = MWM; Windowmanager.layoutparams wmparams = new Windowmanager.layoutparams (); mwmparams = Wmparams;wmparams.type = 2002; Type is the key, here's 2002 for the System Level window, you can also try 2003. Wmparams.flags = 40;//This sentence to set the desktop controllable wmparams.width = 50;wmparams.height = 50;wmparams.format =-3; Transparent Wm.addview (win, wmparams);//This is the key to the WindowManager to throw in the value just set//onlyAddView can be displayed on the page. Register to WindowManager win is to just load the layout,wmparams is just set the WindowManager parameter set// The effect is to register win with WindowManager and its parameters are wmparams set in the Hungry}}

Source: http://download.csdn.net/detail/pcaxb/9057047


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Android Floating window Implementation of the source code

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.