Android does not rely on activity of the Global Suspension window implementation

Source: Internet
Author: User

Android Hover window implementation

Implement the Basics

Android Hover window implementation using WindowManager, WindowManager introduction

WindowManager objects can be obtained by Context.getsystemservice (Context.window_service).

Each of the WindowManager objects is bound to a specific display.
To get a different display of the WindowManager, you can use Createdisplaycontext (display) to get the Context of that display, Then use: Context.getsystemservice (Context.window_service) to get the WindowManager.
Use WindowManager to display windows on top of other apps, even on the top of your phone's desktop.
Called WindowManager inherits the AddView method from the base class and Removeview method to show and hide the window. See the following example for details.
Another: API 17 launches the presentation, which automatically gets the context and WindowManager of the display, which makes it easy to display the window on another display.

WindowManager implementation of suspended Windows requires declarative permission
First, add the following permissions in manifest:
<!--show top-level floating windows--><uses-permission android:name= "Android.permission.SYSTEM_ALERT_WINDOW"/>
  
Note: In the MIUI, you need to open the "Show suspended window" switch in the application and restart the application, otherwise the hover window can only be displayed in the application interface and not on the mobile desktop.

Service acquisition and basic parameter settings
       Gets the app's context Mcontext = Context.getapplicationcontext (); Get WindowManager Mwindowmanager = (windowmanager) mcontext. Getsystemservice (Context.window_servic     E);        Parameter settings: 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_FO        Cusable_im; // |        WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;        If WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE is set, the pop-up view does not receive the back key event params.flags = flags;        The transparent matte that does not set this popup appears as black Params.format = pixelformat.translucent; Flag_not_touch_modal do not block events to the back of the window//settings flag_not_focusable when the hover window is small, the app icon behind it is not long pressed to long press//Do not set this FLAG, home page        The screen will have a problem 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 individual controls in view, the vanishing control of pop-up view requires some processing.
Click on the window to hide the effect of the pop-up window, first of all, the suspension window is full screen, but the outermost layer is transparent or translucent:
Specific Implementation
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;/*** pop-up Window helper class * * @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; /** * Show Popup Box * * @param context * @param view */public static void Showpopupwindow (final context Co ntext) {if (Isshown) {logutil.i (Log_tag, "RetuRN cause already shown ");        Return        } Isshown = true;        Logutil.i (Log_tag, "Showpopupwindow");        Gets the app's context Mcontext = Context.getapplicationcontext (); Get WindowManager Mwindowmanager = (windowmanager) mcontext. Getsystemservice (Context.window_servic        E);        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_FO        Cusable_im; // |        WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;        If WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE is set, the pop-up view does not receive the back key event params.flags = flags;        The transparent matte that does not set this popup appears as black Params.format = pixelformat.translucent; Flag_not_touch_modal does not block events passing to the back of the window//setting flag_not_focusable when the hover window is small, the rearApplication icon from the non-long press to long press//Do not set this flag, the home page screen will have a problem params.width = layoutparams.match_parent;        Params.height = layoutparams.match_parent;        params.gravity = Gravity.center;        Mwindowmanager.addview (MView, params);    Logutil.i (Log_tag, "Add View");  }/** * Hide pop-up box */public static void Hidepopupwindow () {logutil.i (Log_tag, "hide" + Isshown + "," +        MView);            if (Isshown && null! = MView) {logutil.i (Log_tag, "Hidepopupwindow");            Mwindowmanager.removeview (MView);        Isshown = false;        }} private static View Setupview (final context 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) {     Logutil.i (Log_tag, "OK on Click");            Open the installation package//Hide pop-up window Windowutils.hidepopupwindow ();        }        });        Button negativebtn = (button) View.findviewbyid (R.ID.NEGATIVEBTN);                Negativebtn.setonclicklistener (New Onclicklistener () {@Override public void OnClick (View v) {                Logutil.i (Log_tag, "Cancel on Click");            Windowutils.hidepopupwindow ();        }        }); Click on the external area of the window to eliminate//This implementation mainly set the hover window to full-screen size, the outer layer has a transparent background, the middle part is considered the content area//So click outside the content area as click on the suspended window final View popupwind            Owview = 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 button to remove View.setonkeylistener (new Onkeylistener () {@Override public boolean onKey (view                    V, int keycode, keyevent event) {switch (keycode) {case Keyevent.keycode_back:                    Windowutils.hidepopupwindow ();                return true;                Default:return false;        }            }        });    return view; }}


Welcome to scan QR Code, follow public account



Android does not rely on activity of the Global Suspension window implementation

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.