Main.xml
<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android " android:layout_width=" fill_parent " android:layout_height=" fill_parent " android:o rientation= "vertical" > <button android:id= "@+id/start" android:layout_width= "Fill_parent" android:layout_height= "wrap_content" android:text= "@string/start"/> <button android:id= "@+id/stop" android:layout_width= "fill_parent" android:layout_height= "Wrap_content" android: text= "@string/stop"/></linearlayout>
Float_layout.xml
<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android " android:layout_width=" 100dip " android:layout_height=" 100dip " android:orientation=" Vertical "> <button android:id=" @+id/float_id " android:layout_width=" Wrap_content " android:layout_height= "Wrap_content" android:text= "@string/floating"/></linearlayout>
Result.xml (click on the Hover window to enter the interface)
<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android " android:layout_width=" match_parent " android:layout_height=" match_parent "> < ImageView android:layout_width= "wrap_content" android:layout_height= "wrap_content" android:src= "@ Drawable/ic_launcher "/></linearlayout>
Floatwindowtest
Package Com.phicomm.hu;import Android.app.activity;import Android.content.context;import android.content.Intent; Import Android.graphics.pixelformat;import android.os.bundle;import android.util.log;import android.view.Gravity; Import Android.view.layoutinflater;import Android.view.motionevent;import Android.view.view;import Android.view.windowmanager;import Android.view.view.onclicklistener;import Android.view.View.OnTouchListener; Import Android.view.windowmanager.layoutparams;import Android.widget.button;import android.widget.LinearLayout; public class Floatwindowtest extends Activity {private static final String TAG = ' floatwindowtest '; @Overridepublic void on Create (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (r.layout.main);//Mobile, Remove Suspension window Button start = (Button) Findviewbyid (R.id.start); Button stop = (button) Findviewbyid (r.id.stop);/** * Start */start.setonclicklistener (new Onclicklistener () {@ overridepublic void OnClick (View v) {Createfloatview ();}}); * * Remove */stop.setonclicklistener (New Onclicklistener () {@Overridepublic void OnClick (View v) {if (mfloatlayout! = null) {Mwindo Wmanager.removeview (mfloatlayout); Finish ();}});} /** * Start the suspension window */windowmanager Mwindowmanager; Windowmanager.layoutparams wmparams;//Suspension Window layout linearlayout mfloatlayout;//suspension window Buttonbutton mFloatView;private void Createfloatview () {//Get Layoutparams object wmparams = new Windowmanager.layoutparams ();// Gets the Localwindowmanager object Mwindowmanager = This.getwindowmanager ();//Set Window Typewmparams.type = Layoutparams.type_ phone;//set the picture format, the effect is transparent Wmparams.format = pixelformat.rgba_8888;//The floating window will not get focus, but can get drag wmparams.flags = layoutparams.flag_not_focusable;//initialization in the upper left corner of the screen wmparams.gravity = Gravity.left | gravity.top;//the upper-left corner of the screen as the origin, set X, y initial value wmparams.x = 0;WMPARAMS.Y = 0;//Set the width of the hover window Wmparams.width = Windowmanager.layoutparams.wrap_content;wmparams.height = WindowManager.LayoutParams.WRAP_CONTENT; Layoutinflater inflater = This.getlayoutinflater ();//layout of the suspended window Mfloatlayout = (linearlayout) inflater.inFlate (r.layout.float_layout,null);/** * View the view to is added to this window. Params the Layoutparams to * assign to view. */mwindowmanager.addview (Mfloatlayout, wmparams); Mfloatview = (Button) Mfloatlayout.findviewbyid (R.id.float_id);/* * * bind Touch Mobile Monitor */mfloatview.setontouchlistener (new Ontouchlistener () {@Overridepublic Boolean onTouch (View V, motionevent Event) {wmparams.x = (int) event.getrawx ()-mfloatlayout.getwidth ()/2;//25 is the status bar height wmparams.y = (int) Event.getrawy ()-MF Loatlayout.getheight ()/2-40;mwindowmanager.updateviewlayout (Mfloatlayout, wmparams); return false;}}); /bind point to monitor Mfloatview.setonclicklistener (new Onclicklistener () {@Overridepublic void OnClick (View v) {//TODO Auto-generated method Stubintent Intent = new Intent (Floatwindowtest.this,resultactivity.class); StartActivity (Intent );}});}}
Resultactivity (click on the hover window to jump to the interface)
Package Com.phicomm.hu;import Android.app.activity;import Android.os.bundle;public class Resultactivity extends Activity {@Overrideprotected void onCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.result);}}
The second method ************************************************
Main.xml
<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android " android:layout_width=" fill_parent " android:layout_height=" fill_parent " android:o rientation= "vertical" > <button android:id= "@+id/start_id" android:layout_width= "Wrap_content " android:layout_height=" wrap_content " android:text=" @string/btn "/> <button android:id=" @+id/remove_id " android:layout_width=" wrap_content " android:layout_height=" Wrap_content " android:text= "@string/stop" /></linearlayout>
Float_layout.xml
<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android " android:layout_width=" wrap_content " android:layout_height=" wrap_content " android:o rientation= "vertical" > <button android:id= "@+id/float_id" android:layout_width= "Wrap_content " android:layout_height=" wrap_content " android:text=" @string/floating "/> </LinearLayout>
Mainactivity
Package Com.phicomm.hu;import Java.lang.reflect.method;import Android.app.activity;import Android.app.activitymanager;import Android.app.application;import Android.content.context;import Android.content.intent;import Android.net.uri;import Android.os.bundle;import Android.view.View;import Android.view.windowmanager;import Android.view.view.onclicklistener;import Android.widget.button;import Android.widget.linearlayout;public class Mainactivity extends Activity {//Create floating Window object to set layout parameters @overridepublic void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (r.layout.main);// Create remove hover box Button start = (Button) Findviewbyid (r.id.start_id); Button remove = (Button) Findviewbyid (r.id.remove_id);/** * Create a hover box */start.setonclicklistener (new Onclicklistener () {@ overridepublic void OnClick (View v) {Intent Intent = new Intent (mainactivity.this, Fxservice.class); StartService (Intent Finish ();}); * * Remove suspension frame */remove.setonclicklistener (new Onclicklistener () {@Overridepublic voID OnClick (View v) {Intent Intent = new Intent (mainactivity.this, Fxservice.class); StopService (Intent);}});}}
Fxservice
Package Com.phicomm.hu;import Android.app.service;import Android.content.intent;import Android.graphics.pixelformat;import Android.os.handler;import Android.os.ibinder;import Android.util.Log;import Android.view.gravity;import Android.view.layoutinflater;import Android.view.motionevent;import Android.view.View; Import Android.view.windowmanager;import Android.view.view.onclicklistener;import Android.view.view.ontouchlistener;import Android.view.windowmanager.layoutparams;import Android.widget.Button; Import Android.widget.linearlayout;import Android.widget.toast;public class Fxservice extends Service {// Define the floating window layout linearlayout mfloatlayout; Windowmanager.layoutparams wmparams;//Create a floating window set the layout parameters of the object WindowManager mwindowmanager;//the hover Window layout Buttons button mfloatview;@ overridepublic void OnCreate () {super.oncreate (); Createfloatview ();} @Overridepublic ibinder onbind (Intent Intent) {return null;} /** * Create suspension box */private void Createfloatview () {wmparams = new windowmanager.layoutparams ();//Get windowmanagerimpl.comPatmodewrappermwindowmanager = (WindowManager) getapplication (). Getsystemservice (Getapplication (). Window_service);//Set window Typewmparams.type = layoutparams.type_phone;//to set the picture format, the effect is transparent background Wmparams.format = pixelformat.rgba_8888;//setting a floating window is not focusable (enables operation of other visible windows except floating windows) Wmparams.flags =layoutparams.flag_not_focusable;// Adjust the hover window display docked position to the left top wmparams.gravity = Gravity.left | gravity.top;//the upper-left corner of the screen as the origin, set X, y initial value wmparams.x = 0;WMPARAMS.Y = 0;//Set the hover window length-width data Wmparams.width = Windowmanager.layoutparams.wrap_content;wmparams.height = WindowManager.LayoutParams.WRAP_CONTENT; Layoutinflater inflater = Layoutinflater.from (Getapplication ());//Gets the layout of the floating window view mfloatlayout = (linearlayout) Inflater.inflate (r.layout.float_layout,null);//Add Mfloatlayoutmwindowmanager.addview (MFloatLayout, wmParams);// Floating window button Mfloatview = (button) Mfloatlayout.findviewbyid (r.id.float_id); Mfloatlayout.measure ( View.MeasureSpec.makeMeasureSpec (0,view.measurespec.unspecified), View.MeasureSpec.makeMeasureSpec (0, View.MeasureSpec.UNSPECIFIED));/Set the touch move mfloatview.setontouchlistener (new Ontouchlistener () {@Overridepublic Boolean onTouch (View V, Motionevent event) {//GETRAWX is the coordinate of the touch position relative to the screen, the getx is relative to the button's coordinates wmparams.x = (int) event.getrawx ()- Mfloatview.getmeasuredwidth ()/2;//25 is the height of the status bar WMPARAMS.Y = (int) Event.getrawy ()-mfloatview.getmeasuredheight ()/2-25 ;//Refresh Mwindowmanager.updateviewlayout (Mfloatlayout, wmparams); return false;}}); Mfloatview.setonclicklistener (New Onclicklistener () {@Overridepublic void OnClick (View v) {Toast.maketext ( Fxservice.this, "OnClick", Toast.length_short). Show ();}}); @Overridepublic void OnDestroy () {Super.ondestroy (); if (mfloatlayout! = null) {Mwindowmanager.removeview (mfloatlayout );}}}
2 Ways of suspending windows