1. How to implement the floating window
See that little Android icon, it will not be blocked by other formation, it can also respond to the user's click and drag events, its display and disappearance by the WindowManager directly managed, it is the Android floating window. The implementation of the Android floating window mainly depends on the WindowManager class. Through the WindowManager class of AddView (), Updateviewlayout (), Removeview (), we can add, update, and remove the view directly in the window.
2. Specific steps for floating window implementations
1) Since the implementation of the floating window depends on the WindowManager, then there is no doubt that we have to get the Windowmanger object first. Considering that the floating window is usually displayed after the application exits, we need to add and update the floating window in the service, and of course not forget to provide the user with a function to cancel the floating window.
2) Define the view you want to display. You can define it in a layout file, or you can customize the view.
3) Set the necessary parameters, there are several more important parameters need to set, please refer to the following code.
4) Add view to window, receive and process events, update view.
5) Add the corresponding permission in the manifest. <uses-permission android:name= "Android.permission.SYSTEM_ALERT_WINDOW"/>
3. Floating Window Implementation Code
Package Com.spreadst.floatwindow;
Import Android.app.Service;
Import Android.content.Context;
Import android.content.Intent;
Import Android.graphics.PixelFormat;
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;
public class Floatwindowservice extends Service {
private static Final TAG = "Floatwindowservice";
Private WindowManager Mwindowmanager;
Private Windowmanager.layoutparams Mlayoutparams;
Private Layoutinflater Mlayoutinflater;
Private View Mfloatview;
private int mcurrentx;
private int mcurrenty;
private static int mfloatviewwidth = 50;
private static int mfloatviewheight = 80;
@Override
public void OnCreate () {
TODO auto-generated Method Stub
Super.oncreate ();
Initializing WindowManager objects and Layoutinflater objects
Mwindowmanager = (WindowManager) getapplicationcontext (). Getsystemservice (Context.window_service);
Mlayoutinflater = Layoutinflater.from (this);
}
@Override
public void OnStart (Intent Intent, int startid) {
TODO auto-generated Method Stub
Super.onstart (Intent, Startid);
LOG.I (TAG, "OnStart ()");
CreateView ();
}
private void CreateView () {
TODO auto-generated Method Stub
Loading layout files
Mfloatview = mlayoutinflater.inflate (R.layout.main, NULL);
Set the listener for the view to handle user clicks and drags
Mfloatview.setontouchlistener (New Onfloatviewtouchlistener ());
/* Set parameters for View */
Mlayoutparams = new Windowmanager.layoutparams ();
Set the view default placement
mlayoutparams.gravity = Gravity.left | Gravity.top;
Set Window type
Mlayoutparams.type = WindowManager.LayoutParams.TYPE_PHONE;
Set Background to Transparent
Mlayoutparams.format = pixelformat.rgba_8888;
Note that the setting of this property is important, flag_not_focusable the floating window does not get the focus, and if you do not set the property, the other location of the screen is invalid, so they cannot get the focus
Mlayoutparams.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
To set the display position of the view, the location of the update view through WindowManager is actually the change (x, Y) value
Mcurrentx = Mlayoutparams.x = 50;
Mcurrenty = Mlayoutparams.y = 50;
Set the width and height of the view
Mlayoutparams.width = 100;
Mlayoutparams.height = 100;
Add a view to a window
Mwindowmanager.addview (Mfloatview, mlayoutparams);
}
/* Due to direct startservice (), this method is useless */
@Override
Public IBinder Onbind (Intent Intent) {
TODO auto-generated Method Stub
return null;
}
/* This method is used to update the location of the view, which is actually changing the value of (LAYOUTPARAMS.X,LAYOUTPARAMS.Y) */
private void Updatefloatview () {
Mlayoutparams.x = Mcurrentx;
Mlayoutparams.y = Mcurrenty;
Mwindowmanager.updateviewlayout (Mfloatview, mlayoutparams);
}
/* Handles The drag of the view, where only the move event is handled, and the user can handle the click event, for example: when clicking on a floating window, launch the app's main activity*/
Private class Onfloatviewtouchlistener implements View.ontouchlistener {
@Override
public boolean OnTouch (View V, motionevent event) {
TODO auto-generated Method Stub
LOG.I (TAG, "Mcurrentx:" + Mcurrentx + ", Mcurrenty:"
+ Mcurrenty + ", Mfloatviewwidth:" + mfloatviewwidth
+ ", Mfloatviewheight:" + mfloatviewheight);
/*
* GETRAWX (), Getrawy () These two methods are important. Normally, we use GETX (), GetY () to get the trigger point coordinates of the event,
* But Getx (), GetY () gets the coordinates of the event trigger point relative to the upper-left corner of the view, and Getrawx (), Getrawy (), gets the event trigger point
* The coordinates relative to the upper-left corner of the screen. Because X, y in Layoutparams is relative to the screen, you need to use GETRAWX (), Getrawy ().
*/
Mcurrentx = (int) event.getrawx ()-mfloatviewwidth;
Mcurrenty = (int) Event.getrawy ()-mfloatviewheight;
int action = Event.getaction ();
Switch (action) {
Case Motionevent.action_down:
Break
Case Motionevent.action_move:
Updatefloatview ();
Break
Case MOTIONEVENT.ACTION_UP:
Break
}
return true;
}
}
}
4. How to display the floating window only in the Launcher interface
Everyone should be familiar with the 360 security guard floating window, its floating window will only be displayed in the Launcher interface, when the user cut to another interface, the floating window is automatically removed.
To implement this function, we must know the current interface, if only to listen to the activity category, then we can only know when to enter the launcher interface, but do not know whether to leave the launcher interface. So how does 360 implement this function? You can decompile the code. Here is a practical approach, and our present is very simple to know whether the current activity is the activity of the launcher interface. Since the activity is managed in the form of a stack, as long as we look at the activity at the top of the stack is launcher activity. To get the task information for the activity, you need to add the corresponding permission in manifest, <uses-permission android:name = "Android.permission.GET_TASKS"/>.
Private String gettopactivity (context context) {
Get Activitymanager Object
Activitymanager manager = (Activitymanager) getsystemservice (Activity_service);
/*
* Get the list of currently running tasks, which are listed in the most recent chronological order, with parameters representing the maximum number of list items to return.
* Here we only need to get the task where the activity is in the Onresume state.
*/
list<runningtaskinfo> Runningtaskinfos = manager.getrunningtasks (1);
if (Runningtaskinfos! = null) {
Get the top of the stack activity in the task
Return (Runningtaskinfos.get (0). topactivity). toString ();
} else {
return null;
}
}
http://blog.csdn.net/missliang/article/details/8973707
Implementation of the Android floating window