Android:断开互联网后立即显示AlertDialog

时间:2015-02-25 10:38:00

标签: android broadcastreceiver android-alertdialog

我有一个必须连接互联网的应用程序。所以,我想要的是,每当用户从Internet(Wifi或移动设备)断开连接时,Activity应该能够检测到网络状态,并且应该显示AlertDialog提示用户检查其连接。我尝试使用广播接收器实现这一点,但我的应用程序正在崩溃。以下是相同的代码段:

以下代码位于onCreate()方法

  c = this.getApplicationContext();
  broadcastReceiver = new BroadcastReceiver() {

    AlertDialog.Builder builder;

    @Override
    public void onReceive(Context context, Intent intent) {

        ConnectivityManager connectivityManager = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo activeNetInfo = connectivityManager
                .getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
        NetworkInfo activeNetWifi = connectivityManager
                .getNetworkInfo(ConnectivityManager.TYPE_WIFI);
        boolean isConnectedMobile = activeNetInfo != null
                && activeNetInfo.isConnectedOrConnecting();
        boolean isConnectedWifi = activeNetWifi != null
                && activeNetWifi.isConnectedOrConnecting();
        AlertDialog alert = alertNoNetwork();
        if (isConnectedMobile || isConnectedWifi) {
            if (alert != null && alert.isShowing()) {
                alert.dismiss();
            }
        } else {
            if (alert != null && !alert.isShowing()) {
                alert.show();
            }
        }

    }

    public AlertDialog alertNoNetwork() {
        builder = new AlertDialog.Builder(c);
        builder.setMessage(R.string.err_network_failure_title)
                .setMessage(R.string.err_network_failure_message)
                .setCancelable(false)
                .setPositiveButton("Quit",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int id) {

                            }
                        });
        return builder.create();
    }
};

cbroadcastReceiver声明为:

private Context c;  
private BroadcastReceiver broadcastReceiver ;

并在broadcastReceiver方法中注册onResume()

registerReceiver(broadcastReceiver, new IntentFilter(android.net.ConnectivityManager.CONNECTIVITY_ACTION));

我的错误日志是:

  

02-25 15:53:35.836:E / AndroidRuntime(24768):java.lang.RuntimeException:在com中接收广播Intent {act = android.net.conn.CONNECTIVITY_CHANGE flg = 0x4000010(has extras)}时出错。 example.ActivityCapturImg$1@1bca6ea7   02-25 15:53:35.836:E / AndroidRuntime(24768):在android.app.LoadedApk $ ReceiverDispatcher $ Args.run(LoadedApk.java:880)   02-25 15:53:35.836:E / AndroidRuntime(24768):在android.os.Handler.handleCallback(Handler.java:739)   02-25 15:53:35.836:E / AndroidRuntime(24768):在android.os.Handler.dispatchMessage(Handler.java:95)   02-25 15:53:35.836:E / AndroidRuntime(24768):在android.os.Looper.loop(Looper.java:135)   02-25 15:53:35.836:E / AndroidRuntime(24768):在android.app.ActivityThread.main(ActivityThread.java:5312)   02-25 15:53:35.836:E / AndroidRuntime(24768):at java.lang.reflect.Method.invoke(Native Method)   02-25 15:53:35.836:E / AndroidRuntime(24768):at java.lang.reflect.Method.invoke(Method.java:372)   02-25 15:53:35.836:E / AndroidRuntime(24768):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:901)   02-25 15:53:35.836:E / AndroidRuntime(24768):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:696)   02-25 15:53:35.836:E / AndroidRuntime(24768):引起:android.view.WindowManager $ BadTokenException:无法添加窗口 - 令牌null不适用于应用程序   02-25 15:53:35.836:E / AndroidRuntime(24768):在android.view.ViewRootImpl.setView(ViewRootImpl.java:583)   02-25 15:53:35.836:E / AndroidRuntime(24768):在android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:272)   02-25 15:53:35.836:E / AndroidRuntime(24768):在android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69)   02-25 15:53:35.836:E / AndroidRuntime(24768):在android.app.Dialog.show(Dialog.java:298)   02-25 15:53:35.836:E / AndroidRuntime(24768):at com.swachhmap.ActivityCapturImg $ 1.onReceive(ActivityCapturImg.java:235)   02-25 15:53:35.836:E / AndroidRuntime(24768):在android.app.LoadedApk $ ReceiverDispatcher $ Args.run(LoadedApk.java:870)   02-25 15:53:35.836:E / AndroidRuntime(24768):... 8更多

请帮助。

谢谢, Arpit

编辑Context c传递的AlertDialog.Builder应该是

 c = ActivityName.this;

而不是getApplicationContext()

1 个答案:

答案 0 :(得分:1)

  

android.view.WindowManager $ BadTokenException:无法添加窗口 -   token null不适用于应用程序

因为在getApplicationContext()

中传递了AlertDialog.Builder

AlertDialog.Builder需要当前活动组件上下文而不是应用程序上下文。

使用调用Dialog.show()来创建AlertDialog.Builder对象的活动上下文:

builder = new AlertDialog.Builder(CurrentActivityName.this);