To add a custom view using WindowManager

Source: Internet
Author: User

When writing mobile phone guard, the user calls | To answer the phone needs to show the number of attribution, and then there are some anomalies, leaving a mark here, hoping to help friends:

Bug Tutorials

When using view = View.inflate (this, r.layout.ui_toast, null), Wm.addview error occurred after obtaining the View object:

10-12 14:29:06.166:e/androidruntime (1268): caused By:android.view.windowmanager$badtokenexception:unable to add window [email protected]--Permission denied for this window type
Workaround:

Use another method to obtain:

Layoutinflater Inflater = (layoutinflater) getsystemservice (Context.layout_inflater_service); Mdesktoplayout = inflater.inflate (R.layout.volume_panel, NULL);
Full code:

layoutinflater inflater =  (Layoutinflater) Getsystemservice (Context.layout_inflater_service);  mdesktoplayout = inflater.inflate (r.layout.volume_panel, null); //  get system form   mwindowmanager =  (WindowManager)  getapplicationcontext (). Getsystemservice (WINDOW_SERVICE);  //  layout style of the form  mlayoutparams = new windowmanager.layoutparams (); //  Set the form display Type ――type_system_alert (System hint)  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 the  mlayoutparams.x is displayed in the center of the screen  = 50; mlayoutparams.y = 50; mwindowmanager.addview (mDesktopLayout,  Mlayoutparams);

Summary of issues

Neither of these shows a window that will fire the current activity's OnPause () event. It is said that you can start a window-style activity, but the display will fire the current window's OnPause () event, how exactly did not try.

Recommended style

<?xml version= "1.0"  encoding= "Utf-8"?>  <resources>       <style          name= "SelectorDialog"            parent= "@Android: Style/theme.dialog" >           <item name= "Android:windowframe" > @null </item> <!--border-->          <item name= "Android: Windowisfloating ">true</item><!--is on the activity-->           <item name= "Android:windowistranslucent" >false</item><!--Translucent-->           <item name= "Android:windownotitle" >true</ item><!--Untitled-->          <!--<item name= "Android:windowbackground"; @drawable/selector_dialog_bg</item> Background Transparent-->           <item name= "android:backgrounddimenabled" >false</item><!--fuzzy-->           <item name= "Android:backgrounddimamount" >0.6</item>       </style>  </resources>

OK, thank you for breaking the Breaking Dawn blog


To add a custom view using WindowManager

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.