The [Android Pro] Service displays a dialog or displays the view via Windowmanage

Source: Internet
Author: User

Reprint: http://blog.csdn.net/huxueyan521/article/details/8954844

When you add a view to a window by Windowmananger, you need to set the alert parameter and add alert permissions

New windowmanager.layoutparams (layoutparams.wrap_content,                layoutparams.wrap_content, LayoutParams.TYPE_ System_alert,                layoutparams.flag_not_focusable, pixelformat.transparent);         = Gravity.left | Gravity.top;

Permissions:

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

dialog is dependent on activity. However, in the app, it is often necessary to do some background operations in the Service and display a dialog to inform the user when a critical condition is met.

At this point dialog cannot start directly from the service and will report an error android.view.windowmanager$badtokenexception:unable to add window.

There are two ways to resolve this:

1. Displays the dialog of a system boundary, that is, the dialog of the global nature. This dialog can be bounced out of any interface. However, this dialog does not correspond to the home key and the return key, which forces the user to dialog after the action is made.

The use method is that the type of window that is set dialog before the Dialog.show () statement is the system alert type. As follows:

    Dialog.getwindow (). SetType (WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);      dialog.show ();  

You also need to add permissions in Androidmanifest.xml:

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

2. Add a transparent activity to the back of the dialog. That is, a transparent activity is displayed first, and the dialog is displayed in the context using activity. It should be noted that activity in the destroy must be dialog to dismiss off, otherwise the activity disappears but dialog still, will crash.

Transparent activity is created by adding theme to the Androidmanifest.xml:

<activity        android:name= "com.a.b.activity"           android:theme= "@android: style/ Theme.translucent ">  </activity>

In addition, you need to set the activity without Actionbar, and the Setup method is added in the activity's OnCreate method:

    Requestwindowfeature (Window.feature_no_title);   

The [Android Pro] Service displays a dialog or displays the view via Windowmanage

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.