Checking connectivity and network information in Android

/* Network & Dataflow is a key thing in mobile devices */ // Here is code snippet for checking network information… // The manifest permissions required for network connectivity are. <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> // The classes used for checking network connectivity are ConnectivityManager & NetworkInfo /* Checking Network Information */ public void onClick(View v) { ConnectivityManager connMgr = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo networkInfo = connMgr.getActiveNetworkInfo(); if (networkInfo != null && networkInfo.isConnected()) { Toast.makeText(this, "Network connection available.", Toast.LENGTH_SHORT).show(); } else { Toast.makeText(this, "No network connection available.", Toast.LENGTH_SHORT).show(); } } }); /* Checking wi-fi information */ public void onClick(View v) { ConnectivityManager connMgr = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo networkInfo = connMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI); boolean isWifiConn = networkInfo.isConnected(); if (isWifiConn ) { Toast.makeText(this, "Network connection available.", Toast.LENGTH_SHORT).show(); } else { Toast.makeText(this, "No network connection available.", Toast.LENGTH_SHORT).show(); } } }); /* Checking mobile Connection through sim Information */ public void onClick(View v) { ConnectivityManager connMgr = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE); networkInfo = connMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE); boolean isMobileConn = networkInfo.isConnected(); if (isMobileConn) { Toast.makeText(this, "Network connection available.", Toast.LENGTH_SHORT).show(); } else { Toast.makeText(this, "No network connection available.", Toast.LENGTH_SHORT).show(); } } });
Deadful important hints for checking connectivity and network Information in Android using code.

#connectivity #network #networkInfo #hints #simCard #android

#cesarnog

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.