Android:在设备未连接互联网时显示警报对话框

问题描述 投票:0回答:1

我想检查互联网是否已连接,如果没有则显示

dialog
。这是我的代码:

network_class.java

 public class network_class {
    public static boolean isConnectedToInternet(Context context){
        ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
        if (activeNetwork != null) {
            if (activeNetwork.getType() == ConnectivityManager.TYPE_WIFI) {
                return true;
            }
            else if(activeNetwork.getType() == ConnectivityManager.TYPE_MOBILE) {
                return true;
            }
        }
        return false;
    }
}

network_detector.java

public class network_detector extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        View alertdialog_1 = LayoutInflater.from(context).inflate(R.layout.network_dialog,null);
        AlertDialog.Builder dialog_1 = new AlertDialog.Builder(context);
        dialog_1.setView(alertdialog_1);

        AlertDialog dialog = dialog_1.create();
        dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));

        dialog.setCancelable(false);
        if(network_class.isConnectedToInternet(context)){
            if(dialog.isShowing())
                dialog.cancel();
        }
        else
            dialog.show();
    }
}

在那之后,我在我的

MainActivity
中使用接收器:

network_detector detector = new network_detector();
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    //some irrelevant code here
}

@Override
protected void onStart() {
    IntentFilter intentFilter = new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION);
    registerReceiver(detector,intentFilter);
    super.onStart();
}

@Override
protected void onStop() {
    unregisterReceiver(detector);
    super.onStop();
}

当应用程序在我的设备上运行时,当我关闭互联网时,对话框显示(这是正确的),然后当我再次打开互联网时,奇怪的是,对话框仍然继续显示。这是为什么?我哪里错了?

编辑: 这些权限在我的

manifest

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
java android broadcastreceiver
1个回答
0
投票

它不会消失,因为你每次进入

AlertDialog
时都会创建一个不同的
onReceive
所以你应该在
NetworkDetector
类中存储一个变量。

public class network_detector extends BroadcastReceiver {

    private AlertDialog dialog;

    @Override
    public void onReceive(Context context, Intent intent) {
         if (dialog != null) {
            boolean isConnected = networkClass.isConnectedToInternet(context);
            if (isConnected) {
                dialog.dismiss();
                dialog = null;
            } else if (!dialog.isShowing()) {
                showNoInternetDialog(context);
            }
        } else {
            showNoInternetDialog(context);
        }

}


    private void showNoInternetDialog(Context context) {
        View alertdialogView = LayoutInflater.from(context).inflate(R.layout.network_dialog, null);
        AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(context);
        dialogBuilder.setView(alertdialogView);

        dialog = dialogBuilder.create();
        dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));

        dialog.setCancelable(false);
        dialog.show();
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.