Implementation of an Android global suspension window without activity-dependent _android

Source: Internet
Author: User

Objective

When we use 360 security guards on our phones, a small floating window appears at all times on the screen, clicking the floating window to jump to the security defender's operating interface, and the floating window is not affected by the coverage of other activity (Dom also has a small, floating window that interacts with the main interface). So how does this suspension window, which is unaffected by the activity interface, be implemented?

Android Suspension window implementation

Implementation Basics

Android Suspension window implementation usingWindowManager
WindowManager Introduction
The Context.getSystemService(Context.WINDOW_SERVICE) windowmanager object can be obtained by using the.

Each WindowManager object is bound to a specific Display .

To get a different display of the WindowManager, you can use createDisplayContext(Display) to get the display context, and then use: Context.getSystemService(Context.WINDOW_SERVICE) to get WindowManager.

Using WindowManager , you can display windows at the top of other applications, even on the top of your mobile desktop.

The AddView method and Removeview method that WindowManager inherits from the base class are invoked to display and hide the window. See the example later.

another: API 17 launches the presentation, which automatically acquires the context and WindowManagerof display, and can easily display a window on a second display.

WindowManager to realize suspension window need to declare permission

First, add the following permissions in the manifest :

<!--display top floating window--><uses-permission android:name= "Android.permission.SYSTEM_ALERT_WINDOW"/>

  Note: in the MIUI need to open the application of the "Display suspension window" switch, and restart the application, otherwise the suspension window can only be displayed in this application interface, can not be displayed on the phone desktop.

Service acquisition and basic parameter settings

[Java] View plain copy print? View the code slice from my code to get the application context Mcontext = Context.getapplicationcontext (); 
Get WindowManager Mwindowmanager = (windowmanager) mcontext. Getsystemservice (Context.window_service); 
 Parameter setting: Final windowmanager.layoutparams params = new Windowmanager.layoutparams (); 
 Type params.type = WindowManager.LayoutParams.TYPE_SYSTEM_ALERT; WindowManager.LayoutParams.TYPE_SYSTEM_ALERT//Set FLAG int flags = WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_I 
 M // | 
 WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE; 
 If WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE is set, the pop-up view does not receive the event Params.flags = Flags of the back key; 
 The transparent mask that does not set this pop-up box is shown as black Params.format = pixelformat.translucent; 
 Flag_not_touch_modal does not block event delivery to the rear of the window//Set flag_not_focusable suspension window is smaller, the following application icon from the can not be long press to be long press//not set this FLAG, the home page screen will have problems 
 Params.width = layoutparams.match_parent; 
 Params.height = layoutparams.match_parent;  params.gravity = Gravity.center;

Click and key Events

In addition to the click events of the individual controls in the view , the vanishing control of the window view requires some processing.

Click on the window to hide the effect of the window, first of all, the suspension window is full screen, but the outermost layer is transparent or translucent:

Concrete implementation

[Java] View plain copy print? To view the code slices on the codes derivation to my Code slice package Com.robert.floatingwindow; 
Import Android.content.Context; 
Import Android.graphics.PixelFormat; 
Import Android.graphics.Rect; 
Import android.view.Gravity; 
Import android.view.KeyEvent; 
Import Android.view.LayoutInflater; 
Import android.view.MotionEvent; 
Import Android.view.View; 
Import Android.view.View.OnKeyListener; 
Import Android.view.View.OnTouchListener; 
Import Android.view.WindowManager; 
Import Android.view.View.OnClickListener; 
Import Android.view.WindowManager.LayoutParams; 
Import Android.widget.Button; /** * @ClassName windowutils * * * * * * */public class Windowutils {private static final String Log_tag = " 
  Windowutils "; 
  private static View Mview = null; 
  private static WindowManager Mwindowmanager = null; 
  private static context mcontext = NULL; 
  public static Boolean Isshown = false; /** * Display Pop-up box * * @param context * @param view/public static void showPopupwindow (Final context) {if (Isshown) {logutil.i (Log_tag, ' return cause already shown '); 
    Return 
    } Isshown = true; 
    Logutil.i (Log_tag, "Showpopupwindow"); 
    Gets the application context Mcontext = Context.getapplicationcontext (); 
    Get WindowManager Mwindowmanager = (windowmanager) mcontext. Getsystemservice (Context.window_service); 
    Mview = Setupview (context); 
    Final Windowmanager.layoutparams params = new Windowmanager.layoutparams (); 
    Type params.type = WindowManager.LayoutParams.TYPE_SYSTEM_ALERT; WindowManager.LayoutParams.TYPE_SYSTEM_ALERT//Set FLAG int flags = WindowManager.LayoutParams.FLAG_ALT_FOCUSA 
    Ble_im; // | 
    WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE; 
    If WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE is set, the pop-up view does not receive the event Params.flags = Flags of the back key; 
    The transparent mask that does not set this pop-up box is shown as black Params.format = pixelformat.translucent; 
   Flag_not_touch_modal does not block events passing to the rear window When setting the Flag_not_focusable suspension window, the following application icon can be changed from not long to long press//not set this FLAG, the home page screen will have problems params.width = LAYOUTPARAMS.MATC 
    H_parent; 
    Params.height = layoutparams.match_parent; 
    params.gravity = Gravity.center; 
    Mwindowmanager.addview (Mview, params); 
  Logutil.i (Log_tag, "Add View"); }/** * Hide popup/public static void Hidepopupwindow () {logutil.i (Log_tag, "hide" + Isshown + "," + M 
    View); 
      if (Isshown && null!= mview) {logutil.i (Log_tag, "Hidepopupwindow"); 
      Mwindowmanager.removeview (Mview); 
    Isshown = false; 
    } private static View Setupview (final context) {logutil.i (Log_tag, "setUp View"); 
    View view = Layoutinflater.from (context). Inflate (R.layout.popupwindow, NULL); 
    Button positivebtn = (button) View.findviewbyid (R.ID.POSITIVEBTN); Positivebtn.setonclicklistener (New Onclicklistener () {@Override public void OnClick (View v) {LogU Til.i (Log_tag, "OK on Click"); 
      Open the mounting kit//Hidden window Windowutils.hidepopupwindow (); 
    } 
    }); 
    Button negativebtn = (button) View.findviewbyid (R.ID.NEGATIVEBTN); Negativebtn.setonclicklistener (New Onclicklistener () {@Override public void OnClick (View v) {LogU 
        Til.i (Log_tag, "Cancel on Click"); 
      Windowutils.hidepopupwindow (); 
    } 
    });  Click the external area of the window to eliminate//This implementation of the main suspension window set to full screen size, the outer layer has a transparent background, the middle part as the content area//So click on the outside of the content area as a click of the final View of the suspension window Popupwindowview 
      = View.findviewbyid (R.id.popup_window);//non-transparent content area View.setontouchlistener (new Ontouchlistener () {@Override 
        public boolean Ontouch (View V, motionevent event) {logutil.i (Log_tag, "Ontouch"); 
        int x = (int) event.getx (); 
        int y = (int) event.gety (); 
        Rect Rect = new Rect (); 
        Popupwindowview.getglobalvisiblerect (rect); 
        if (!rect.contains (x, y)) {Windowutils.hidepopupwindow (); } 
        Logutil.i (Log_tag, "Ontouch:" + x + "," + y + ", rect:" + rect); " 
      return false; 
    } 
    }); Click the back key to eliminate View.setonkeylistener (new Onkeylistener () {@Override public boolean onkey (view v, int ke Ycode, KeyEvent event) {switch (keycode) {case KeyEvent.KEYCODE_BACK:WindowUtils.hidePopup 
          Window (); 
        return true; 
        Default:return false; 
    } 
      } 
    }); 
  return view;  } 
}

Summarize

The above is the entire content of this article, this article takes the example form to analyze the Android suspension window principle and the concrete realization technique in detail, has certain reference value, hoped for everybody in the development Android application to be helpful.

Related Article

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.