Android Service Dialog pop-up box

Source: Internet
Author: User
Tags what interface

Android Service Dialog pop-up box

I believe that the first time you implement the Alertdialog popup in the service, you will encounter the application flash back and then report this exception:

Caused by:android.view.windowmanager$badtokenexception:

The following is why this exception, the reason is very simple, because the alertdialog display is dependent on a certain Activity class , so you want to the implementation of the Service to bounce out, need to do the following configuration:

1, after the installation of the general Alertdialog function block, in the alertobj . Show () statement before adding:

  Alertobj.getwindow (). SetType (WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);

For example:

Private voidShowhostonlinealert () {FinalAlertdialog dialog =NewAlertdialog.builder (Backgroudservice. This). Create (); Dialog.setcanceledontouchoutside (false);        Dialog.requestwindowfeature (Window.feature_no_title); Dialog.getwindow (). Setbackgrounddrawable (NewColordrawable (color.transparent));//set background was transparent Dialog.getwindow (). SetType (WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);//statements that need to be added dialog.show    (); }

2. Add permissions in Androidmanifest.xml:

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

Summary: The above practice is to declare that we want to pop up this prompt box is a system prompt box, that is, the global nature of the prompt box, so as long as the phone is turned on, no matter what interface it is now, as long as the call Alterobj.show (), will pop up the prompt box.

Android Service Dialog pop-up box

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.