Using the phone 360 and QQ mobile phone butler and other software friends, will find that in these applications, there will be a suspended form, such as QQ phone butler in the phone scene:
This form, in addition to being displayed, can also move its position and always appear. In addition to closing the current program, the window does not actively disappear. In fact, its use of the principle is very simple, is to borrow WindowManager this management class to achieve.
Note: To add the use rights in Androidmanifest.xml:
[HTML]View Plaincopy
- <uses-permission
- android:name="Android.permission.SYSTEM_ALERT_WINDOW" />
Here, I use the code layout of the way, imitate the QQ this interface effect:
[Java]View Plaincopy
- Import Android.content.Context;
- Import Android.widget.ImageView;
- Import Android.widget.LinearLayout;
- Import Android.widget.TextView;
- Public class Desktoplayout extends LinearLayout {
- Public Desktoplayout (context context) {
- super (context);
- SetOrientation (linearlayout.horizontal);
- Layoutparams mlayoutparams = new Layoutparams (
- Layoutparams.wrap_content, layoutparams.wrap_content);
- Setlayoutparams (Mlayoutparams);
- //Display icon
- ImageView Mimageview = new ImageView (context);
- Mimageview.setimageresource (R.drawable.icon);
- AddView (Mimageview, mlayoutparams);
- //Display of text
- TextView Mtextview = new TextView (context);
- Mtextview.settext ("Hello");
- Mtextview.settextsize (30);
- AddView (Mtextview, mlayoutparams);
- }
- }
Next, let it show up in the activity. The first thing to do is to set Windowmanager.layoutparams:
[Java]View Plaincopy
- Get System Form
- Mwindowmanager = (WindowManager) getapplicationcontext ()
- . Getsystemservice ("window");
- Layout styles for Forms
- Mlayoutparams = new Windowmanager.layoutparams ();
- Set the form display Type--type_system_alert (System tip)
- Mlayoutparams.type = WindowManager.LayoutParams.TYPE_SYSTEM_ALERT;
- Set the form focus and touch:
- Flag_not_focusable (Cannot get key input focus)
- Mlayoutparams.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
- Set the display mode
- Mlayoutparams.format = pixelformat.rgba_8888;
- To set the Alignment method
- mlayoutparams.gravity = Gravity.top | Gravity.left;
- Set form width and height
- Mlayoutparams.width = WindowManager.LayoutParams.WRAP_CONTENT;
- Mlayoutparams.height = WindowManager.LayoutParams.WRAP_CONTENT;
- Set the position of the form to display otherwise in the center of the screen
- Mlayoutparams.x = 50;
- Mlayoutparams.y = 50;
To display a form and close a form:
[Java]View Plaincopy
- Mwindowmanager.addview (Mdesktoplayout, mlayoutparams);
- Mwindowmanager.removeview (mdesktoplayout);
Here is the original code for activity, which is designed to double-click the effect of closing the form:
[Java]View Plaincopy
- Import android.app.Activity;
- Import Android.graphics.PixelFormat;
- Import Android.os.Bundle;
- Import android.view.Gravity;
- Import android.view.MotionEvent;
- Import Android.view.View;
- Import Android.view.View.OnClickListener;
- Import Android.view.View.OnTouchListener;
- Import Android.view.WindowManager;
- Import Android.widget.Button;
- Public class Desktip extends Activity {
- private WindowManager Mwindowmanager;
- private Windowmanager.layoutparams Mlayoutparams;
- private Desktoplayout mdesktoplayout;
- private long starttime;
- /**
- * Create a suspended form
- */
- private void Createdesktoplayout () {
- Mdesktoplayout = New Desktoplayout (this);
- Mdesktoplayout.setontouchlistener (new Ontouchlistener () {
- Public Boolean OnTouch (View V, motionevent event) {
- Onactionmove (event);
- return true;
- }
- });
- }
- /**
- * Set WindowManager
- */
- private void Createwindowmanager () {
- //Get system form
- Mwindowmanager = (WindowManager) getapplicationcontext ()
- . Getsystemservice ("window");
- //Layout style of the form
- Mlayoutparams = new Windowmanager.layoutparams ();
- //Set form display Type--type_system_alert (System prompt)
- Mlayoutparams.type = WindowManager.LayoutParams.TYPE_SYSTEM_ALERT;
- //Set the form focus and touch:
- //Flag_not_focusable (Cannot get key input focus)
- Mlayoutparams.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
- //Set the display mode
- Mlayoutparams.format = pixelformat.rgba_8888;
- //Set the Alignment method
- mlayoutparams.gravity = Gravity.top | Gravity.left;
- //Set form width and height
- Mlayoutparams.width = WindowManager.LayoutParams.WRAP_CONTENT;
- Mlayoutparams.height = WindowManager.LayoutParams.WRAP_CONTENT;
- //Set the position of the form to display otherwise in the center of the screen
- Mlayoutparams.x = 50;
- Mlayoutparams.y = 50;
- }
- private void Onactionmove (Motionevent event) {
- if (event.getaction () = = Motionevent.action_down) {
- Long end = System.currenttimemillis ()-starttime;
- //Double-click Interval between 200ms to 500ms
- if (End > && End < + ) {
- Closedesk ();
- return;
- }
- StartTime = System.currenttimemillis ();
- }
- Mlayoutparams.x = (int) (EVENT.GETRAWX ()-(Mdesktoplayout.getwidth ()));
- Mlayoutparams.y = (int) (Event.getrawy ()-(Mdesktoplayout.getheight ()));
- Mwindowmanager.updateviewlayout (Mdesktoplayout, mlayoutparams);
- }
- /**
- * Show Desktoplayout
- */
- private void Showdesk () {
- Mwindowmanager.addview (Mdesktoplayout, mlayoutparams);
- Finish ();
- }
- /**
- * Close Desktoplayout
- */
- private void Closedesk () {
- Mwindowmanager.removeview (mdesktoplayout);
- Finish ();
- }
- public void OnCreate (Bundle savedinstancestate) {
- super.oncreate (savedinstancestate);
- Setcontentview (R.layout.main);
- Createwindowmanager ();
- Createdesktoplayout ();
- Button btn = (button) Findviewbyid (R.ID.BTN);
- Btn.setonclicklistener (new Onclicklistener () {
- public void OnClick (View v) {
- Showdesk ();
- }
- });
- }
- }
Show the effect:
Example of a suspended form
http://blog.csdn.net/xyz_fly/article/details/7546096