0

作为java中的新手,我无法链接这两个代码片段:

如果没有互联网连接:

     public boolean isOnline() {
        ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo netInfo = cm.getActiveNetworkInfo();
        if (netInfo != null && netInfo.isConnected()) {
            return true;
        }
        return false;
    }

然后,弹出一个带有通知和设置/取消按钮的对话框。

      public void showNoConnectionDialog(Context ctx1) {
      final Context ctx = ctx1;
      AlertDialog.Builder builder = new AlertDialog.Builder(ctx);
      builder.setCancelable(true);
      builder.setMessage(R.string.no_connection);
      builder.setTitle(R.string.no_connection_title);
      builder.setPositiveButton(R.string.settings, new DialogInterface.OnClickListener() {
          public void onClick(DialogInterface dialog, int which) {
              ctx.startActivity(new Intent(Settings.ACTION_WIRELESS_SETTINGS));
          }
      });
      builder.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
          public void onClick(DialogInterface dialog, int which) {
              return;
          }
      });
      builder.setOnCancelListener(new DialogInterface.OnCancelListener() {
          public void onCancel(DialogInterface dialog) {
              return;
          }
      });

      builder.show();
  }

谁能给点方向。

4

1 回答 1

2

像这样使用。

if(!isOnline()) {
   showNoConnectionDialog(MainActivity.this);
}
于 2013-03-17T15:02:11.300 回答