Resolves an issue where alertdialog (dialog box) cannot be added (android.view.windowmanager$badtokenexception) in Broadcastreceiver (broadcast recipient)

Source: Internet
Author: User

In Broadcastreceiver, when we create a alertdialog and show out, the following error occurs:

12-24 14:10:57.025:e/androidruntime (17600): java.lang.RuntimeException:Unable to start receiver com.ustc.broadcastreceiver.forceofflinereceiver:android.view.windowmanager$badtokenexception:unable to add window --Token null is a application

The following methods can be resolved:

The first step:

Before calling the Show method, set the following properties:

Dialog.getwindow (). SetType (WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
Run again, the Discovery program hangs again:

12-24 14:15:22.793:e/androidruntime (17979): java.lang.RuntimeException:Unable to start receiver com.ustc.broadcastreceiver.forceofflinereceiver:android.view.windowmanager$badtokenexception:unable to add window [Email protected]--Permission denied for this window type

This is a look to know, permission denied, need to add permissions:

Step Two:

To add permissions in the manifest file:

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


This runs OK, and my code is as follows:

Package Com.ustc.broadcastreceiver;import Android.app.alertdialog;import Android.app.alertdialog.builder;import Android.content.broadcastreceiver;import Android.content.context;import Android.content.dialoginterface;import Android.content.dialoginterface.onclicklistener;import Android.content.intent;import Android.view.WindowManager; public class Forceofflinereceiver extends Broadcastreceiver {@Overridepublic void OnReceive (Final context context, Intent Intent) {alertdialog.builder builder = new Builder (context); Builder.settitle ("hint"); Builder.setmessage ("Forced downline") ; builder.setcancelable (false); Builder.setpositivebutton ("OK", new Onclicklistener () {@Overridepublic void OnClick ( Dialoginterface dialog, int which) {activitycollector.finishall (); Intent Intent = new Intent (context, Loginactivity.class); Intent.addflags (Intent.flag_activity_new_task); context.startactivity (intent);}}); Alertdialog dialog = Builder.create ();d Ialog.getwindow (). SetType (WindowManager.LayoutParams.TYPE_SYSTEM_ALERT); DialoG.show ();}} 


Resolves an issue where alertdialog (dialog box) cannot be added (android.view.windowmanager$badtokenexception) in Broadcastreceiver (broadcast recipient)

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.