(Latest Code ) Android check internet connectivity Check with broadcast.
Download demo from here:- https://drive.google.com/open?id=1SbrzrDvEvW2OsGHV-LpY4TQXPAetDpjV
Note:- extent base activity where require internet connectivity functionality. and implement below override methods from base actrivity
@Overridepublic void onNetworkConnectionChanged(boolean isConnected) { if(isConnected) { Toast.makeText(MainActivity.this, "Got Internet connectivity", Toast.LENGTH_SHORT).show(); }else{ Toast.makeText(MainActivity.this, "Lost Internet connectivity", Toast.LENGTH_SHORT).show(); } }
Android check internet connection (Simple Oldtype Demo)
CheckIetConnection cic = new CheckInternetConnection(context);
if(cic.checkInternet(0)) { // available} else{ // not available}//classpublic class CheckInternetConnection { public Context context = null; public Activity mActivity; Dialog dialog; int TYPE_DIALOG = 0; // TYPE_DIALOG = 0 : no dialog (Default) // TYPE_DIALOG = 1 : toast // TYPE_DIALOG = 2 : dialog display //if(checkInternet(TYPE_DIALOG)) public CheckInternetConnection(Context context) { this.context = context; } public CheckInternetConnection(Activity context) { this.TYPE_DIALOG = 1; this.context = context; this.mActivity = context; initAlertDialog(context, context.getResources().getString(R.string.msg_nointernet)); } public boolean checkInternet() { this.TYPE_DIALOG = 0; return isOnline(); } public boolean checkInternet(int checkType) { this.TYPE_DIALOG = checkType; return isOnline(); } public Boolean isOnline() { if (isNetAvailable(context)) return true; else { try { URL url = new URL("http://www.google.com"); HttpURLConnection urlc = (HttpURLConnection) url .openConnection(); urlc.setRequestProperty("User-Agent", "Test"); urlc.setRequestProperty("Connection", "close"); urlc.setConnectTimeout(3000); // This is time limit if the // connection time limit urlc.connect(); if (urlc.getResponseCode() == 200) { return true; } } catch (MalformedURLException e1) { e1.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } } if (TYPE_DIALOG == 0) { } else if (TYPE_DIALOG == 1) { showToast(context, context.getResources().getString(R.string.msg_nointernet)); } else if (TYPE_DIALOG == 2) { showDialog(); } return false; } public void showToast(Context context, String message) { Toast.makeText(context, message, Toast.LENGTH_SHORT).show(); } public synchronized static boolean isNetAvailable(Context context) { try { boolean isNetAvailable = false; if (context != null) { ConnectivityManager mgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); if (mgr != null) { boolean mobileNetwork = false; boolean wifiNetwork = false; boolean wiMaxNetwork = false; boolean mobileNetworkConnecetd = false; boolean wifiNetworkConnecetd = false; boolean wiMaxNetworkConnected = false; NetworkInfo mobileInfo = mgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE); NetworkInfo wifiInfo = mgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI); NetworkInfo wiMaxInfo = mgr.getNetworkInfo(ConnectivityManager.TYPE_WIMAX); if (mobileInfo != null) mobileNetwork = mobileInfo.isAvailable(); if (wifiInfo != null) wifiNetwork = wifiInfo.isAvailable(); if (wiMaxInfo != null) wiMaxNetwork = wiMaxInfo.isAvailable(); if (wifiNetwork == true) wifiNetworkConnecetd = wifiInfo.isConnectedOrConnecting(); if (mobileNetwork == true) mobileNetworkConnecetd = mobileInfo.isConnectedOrConnecting(); if (wiMaxNetwork == true) wiMaxNetworkConnected = wiMaxInfo.isConnectedOrConnecting(); isNetAvailable = (mobileNetworkConnecetd || wifiNetworkConnecetd || wiMaxNetworkConnected); } } return isNetAvailable; } catch (NullPointerException e) { return false; } catch (Exception e) { return false; } } public void showDialog() { try { if (dialog != null && (!dialog.isShowing())) { Log.d("TAG", "SHOW DIALOG"); dialog.show(); } } catch (Exception e) { e.printStackTrace(); } } public void initAlertDialog(Context context, String message) { AlertDialog.Builder builder = new AlertDialog.Builder(context); builder.setTitle(context.getResources().getString(R.string.app_name)); builder.setMessage(message); builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }); dialog = builder.create(); } }
Wednesday, 23 March 2016
Subscribe to:
Post Comments (Atom)
Run/install/debug Android applications over Wi-Fi ?
Open Teminal/cmd --------------- Below steps is for Android 10 or lower Step 1 - Connect the device via USB and make sure debugging is work...
-
This is use for enerate static map images easily. Download here. https://github.com/sohel274/static-map
-
Open Teminal/cmd --------------- Below steps is for Android 10 or lower Step 1 - Connect the device via USB and make sure debugging is work...
-
Play GIF file in android very smoothly, Download Demo here : https://drive.google.com/open?id=0B7De_mE7QNdOd2FVRlpTM0tYcUU
No comments:
Post a Comment