没有数据连接时弹出对话框 - Android

时间:2016-12-22 20:33:42

标签: android dialog connection

我正在尝试在没有互联网连接时尝试加载webview时显示警告对话框。我把这个类放在了OnCreate下面,但我没有运气让对话框显示出来。所有建议将不胜感激。谢谢!

public void open(View view) {
    AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
    alertDialogBuilder.setMessage("No data connection available");
    alertDialogBuilder.setPositiveButton("OK",
            new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface arg0, int arg1) {
                    Toast.makeText(RepActivity.this, "Check connection settings", Toast.LENGTH_LONG).show();
                }
            });

    alertDialogBuilder.setNegativeButton("No", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            finish();
        }

    });
    ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    android.net.NetworkInfo wifi = cm
            .getNetworkInfo(ConnectivityManager.TYPE_WIFI);
    android.net.NetworkInfo datac = cm
            .getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
    if ((wifi != null & datac != null)
            && (wifi.isConnected() | datac.isConnected())) {
        setContentView(R.layout.activity_rep);
    } else {
        //no connection
        AlertDialog.Builder alertDialog = new AlertDialog.Builder(RepActivity.this);
        alertDialog.show();
    }
}

1 个答案:

答案 0 :(得分:0)

Decalare:

private AlertDialog internetDialog;

然后在open()

中检查网络状态
ConnectivityManager connectivityManager = (ConnectivityManager)
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo activeNetInfo = connectivityManager
                .getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
        NetworkInfo activeWIFIInfo = connectivityManager
                .getNetworkInfo(connectivityManager.TYPE_WIFI);

        if (activeWIFIInfo.isConnected() || activeNetInfo.isConnected()) {
            removeInternetDialog();
        } else {
            showInternetDialog();
        }

然后使用

显示无数据连接对话框
private void showInternetDialog() {
    AlertDialog.Builder internetBuilder = new AlertDialog.Builder(
            Activity_name.this);
    internetBuilder.setCancelable(false);
    LayoutInflater inflater = Activity_name.this.getLayoutInflater();
    View dialogView = inflater.inflate(R.layout.dialog_layout, null);
    internetBuilder.setView(dialogView);
    TextView title = (TextView) dialogView.findViewById(R.id.tvDialogTitle);
    TextView message = (TextView) dialogView.findViewById(R.id.tvDialogMessage);

    title.setText("No internet");
    message.setText("You have no active internet connection");

    Button btnEnableWifi = (Button) dialogView.findViewById(R.id.btnEnableWifi);
    Button btnExit = (Button) dialogView.findViewById(R.id.btnExitDialog);

    btnEnableWifi.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            startActivity(new Intent(
                    Settings.ACTION_WIFI_SETTINGS));
            removeInternetDialog();
        }
    });

    btnExit.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            removeInternetDialog();
            finish();
        }
    });
    internetDialog = internetBuilder.create();
    internetDialog.show();
}

 private void removeInternetDialog() {
    if (internetDialog != null && internetDialog.isShowing()) {
        internetDialog.dismiss();
    }
}