Advertisement
enkuso

Android - Интернэт холболт байгаа эсэхийг шалгах with Dialog

Jan 15th, 2014
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.81 KB | None | 0 0
  1.  
  2.     /**
  3.      * Интернэт холболт байгаа эсэхийг шалгах
  4.      */
  5.     protected boolean hasInternetConnection() {
  6.         try {
  7.  
  8.             ConnectivityManager nInfo = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
  9.             nInfo.getActiveNetworkInfo().isConnectedOrConnecting();
  10.  
  11.             ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
  12.             NetworkInfo netInfo = cm.getActiveNetworkInfo();
  13.             if (netInfo != null && netInfo.isConnectedOrConnecting()) {
  14.                 return true;
  15.             } else {
  16.                 AlertDialog alertDialog = new AlertDialog.Builder(this).create();
  17.                 alertDialog.setTitle(R.string.no_internet);
  18.                 alertDialog.setMessage(getString(R.string.require_internet));
  19.                 alertDialog.setIcon(R.drawable.ic_warning);
  20.                 alertDialog.setButton(DialogInterface.BUTTON_POSITIVE, "OK", new DialogInterface.OnClickListener() {
  21.                     public void onClick(DialogInterface dialog, int which) {
  22.                     }
  23.                 });
  24.                 alertDialog.show();
  25.                 return false;
  26.             }
  27.  
  28.         } catch (Exception e) {
  29.             AlertDialog alertDialog = new AlertDialog.Builder(this).create();
  30.             alertDialog.setTitle(R.string.no_internet);
  31.             alertDialog.setMessage(getString(R.string.require_internet));
  32.             alertDialog.setIcon(R.drawable.ic_warning);
  33.             alertDialog.setButton(DialogInterface.BUTTON_POSITIVE, "OK", new DialogInterface.OnClickListener() {
  34.                 public void onClick(DialogInterface dialog, int which) {
  35.                 }
  36.             });
  37.             alertDialog.show();
  38.             return false;
  39.         }
  40.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement