无法显示警告对话框-android.view.WindowManager $ BadTokenException:无法添加窗口 - 令牌null不适用于应用程序

时间:2016-06-02 04:48:57

标签: android alertdialog android-internet

我正在尝试检查来自广播接收器的互联网连接,如果没有互联网想要显示警告对话框,但在这里我收到了由于AlertDialog.Builder(context)而被提及的错误。但如果我将其更改为AlertDialog.Builder(this)AlertDialog.Builder(NetworkChangeReceiver.this),则会出现编译错误。构造函数AlertDialog.Builder(NetworkChangeReceiver)未定义

NetworkChangeReceiver.java

package com.example;


public class NetworkChangeReceiver extends BroadcastReceiver {

@SuppressWarnings("deprecation")
@Override
public void onReceive(final Context context, final Intent intent) {
    Log.d("Inside Network change receiver", Background.isActivityVisible()+"------------------------");
    if(Background.isActivityVisible()==true){
        Boolean status = NetworkUtil.getConnectivityStatusString(context);
        Log.d("NetworkChangeReceiver", status+"");
        if(status){
            Log.d("NetworkChangeReceiver", "User Comes online");
        }
        else{
            Intent i = new Intent(context, Offline.class);
            i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(i);
            try{
            final AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(context);

            // set title
            alertDialogBuilder.setTitle("Lost Internet Connectivity");

            // set dialog message
            alertDialogBuilder
            .setMessage("Do you want to retry!")
            .setCancelable(false)
            .setPositiveButton("Yes",new DialogInterface.OnClickListener()
            {
                public void onClick(DialogInterface dialog,int id) {
                    // if this button is clicked, close
                    // current activity
                    Boolean status = NetworkUtil.getConnectivityStatusString(context);  
                    if(status){
                        Intent i = new Intent(context, MainActivity.class);
                        i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        context.startActivity(i);
                    }

                    else{
                        // create alert dialog
                        AlertDialog alertDialog = alertDialogBuilder.create();

                        // show it
                        alertDialog.show();
                    }

                }
            })
            .setNegativeButton("No",new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog,int id) {
                    Intent intent = new Intent(Intent.ACTION_MAIN);
                    intent.addCategory(Intent.CATEGORY_HOME);
                    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                  context.  startActivity(intent);
                }
            });

            // create alert dialog
            AlertDialog alertDialog = alertDialogBuilder.create();

            // show it
            alertDialog.show();
        }   catch(Exception e){
            e.printStackTrace();
        }     
        }
    }
}}

3 个答案:

答案 0 :(得分:1)

无法在广播接收器中显示警告对话框。请检查thisthis

答案 1 :(得分:0)

试试这个,

YourAlertDialog dialog = new YourAlertDialog(mContext);
dialog.getWindow()
    .setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
dialog.show();

mainfest.xml 中添加系统提醒权限:

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

这可能会对你有所帮助。

答案 2 :(得分:0)

  

无法显示提醒   dialog-android.view.WindowManager $ BadTokenException:无法添加   window - 令牌null不适用于应用程序

由于:

window.onload = function () {
  setTimeout(function () {
    document.getElementsByTagName('input')[0].focus();
    console.log(document.activeElement);
  }, 0);
};

线。因为AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(context); 需要像AlertDialog.Builder这样的可见应用程序组件,而不是ActivityService等。

目前将BroadcastReceiver传递给context这是AlertDialog.Builder方法的第一个参数,无法显示UI元素的上下文。

显示来自onReceive的警告:

1。BroadcastReceiver主题开启一个活动 2。Theme.Translucent.NoTitleBar的{​​{1}}中显示AlertDialog