Create window display in Android service

Source: Internet
Author: User

Sometimes it is necessary to not interrupt the original activity when displaying the UI.

For example, when playing a video or playing a game, you need to display a menu (can be a system prompt, or similar to a TV menu)

Obviously doing it with activity is not possible, because when the new activity starts, it will pause the original activity.

How to do this, you can refer to the System Power indicator window or statusbar to start the window in the service

The new window will appear at the top of the UI, but will not break the original activity

, one is broadcast video, one is playing in the game:

Take a look at the two properties of Windowmanager.layoutparams:

int type_system_alert window Type:system window, such as low power ALERT.
int type_system_overlayWindow type:system OVERLAY windows, which need to being displayed on top of Everyth ing else.

These two are good, exactly what we want, on the code ~


Code in Seivece

private void Showsystemdialog () {/* Create UI */View v = view.inflate (mcontext, r.layout.main, NULL);          Alertdialog.builder B = new Alertdialog.builder (mcontext);          B.setview (v);           D = b.create ();          D.getwindow (). SetType (WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);      D.getwindow (). SetType (WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY);               D.show ();                        /* Set Size & POS */windowmanager.layoutparams LP = D.getwindow (). GetAttributes ();          WindowManager wm = (WindowManager) getsystemservice (Window_service);      Display display = Wm.getdefaultdisplay ();          if (Display.getheight () > Display.getwidth ()) {//lp.height = (int) (Display.getheight () * 0.5);              Lp.width = (int) (Display.getwidth () * 1.0);          } else {//lp.height = (int) (Display.getheight () * 0.75);                      Lp.width = (int) (Display.getwidth () * 0.5);       }   D.getwindow (). SetAttributes (LP);          /* Update UI Data */LV = (ListView) D.getwindow (). Findviewbyid (R.id.listview);                                             Simpleadapter adapter = new Simpleadapter (Mcontext, Getlistdata (), R.layout.list_item, New string[]{"Item_text", "item_img"}, new Int[]{r.id.item_text, R.id.           ITEM_IMG});                             Lv.setadapter (adapter); /* Set Listener */Lv.setonitemclicklistener (new Onitemclicklistener () {public void Onitemclick (Adapterv            Iew<?> Parent, view view, int POS, long id) {D.dismiss ();             }         }); }



created a dialog, can adjust dialog location, size, dialog created from R.layout.main, dynamically add UI data

Responding to onclick actions on the UI


* The corresponding permission is required in manifest

<uses-permission android:name= "Android.permission.SYSTEM_ALERT_WINDOW"/>
<uses-permission android:name= "Android.permission.SYSTEM_OVERLAY_WINDOW"/>

* The difference between Type_system_alert and Type_system_overlay

For example, in the code above, the System_alert window can get focus and respond to actions

When the System_overlay window is displayed, the focus is on the activity behind, and you can still manipulate the activity behind it.

* In the above code, please note the location of D.show (), only after show can adjust the dialog size and update data

* Can adjust the attributes, first dump out

Lp.flags = 0x20002,//Flag_dim_behind | Flag_alt_focusable_im

Lp.gravity = 0x11,//CENTER

Lp.type = 0x7d3,//Type_system_alert

These attr can be set, specifically defined in the API document Windowmanager.layoutparams


Create window display in Android service

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.