How to get android internet connection status for HTC One app development?
I'm new to android develpment and i'm going to develop an application with internet. so I need to get the interent status. Any help appreciated.
How to get android internet connection status for HTC One app development?
I'm new to android develpment and i'm going to develop an application with internet. so I need to get the interent status. Any help appreciated.
Try this way and Read more
public static String getConnectivityStatusString(Context context) {
int conn = NetworkUtil.getConnectivityStatus(context);
String status = null;
if (conn == NetworkUtil.TYPE_WIFI) {
status = "Wifi enabled";
} else if (conn == NetworkUtil.TYPE_MOBILE) {
status = "Mobile data enabled";
} else if (conn == NetworkUtil.TYPE_NOT_CONNECTED) {
status = "Not connected to Internet";
}
return status;
}
Or another simple way to achieve the task
private boolean haveNetworkConnection() {
boolean haveConnectedWifi = false;
boolean haveConnectedMobile = false;
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo[] netInfo = cm.getAllNetworkInfo();
for (NetworkInfo ni : netInfo) {
if (ni.getTypeName().equalsIgnoreCase("WIFI"))
if (ni.isConnected())
haveConnectedWifi = true;
if (ni.getTypeName().equalsIgnoreCase("MOBILE"))
if (ni.isConnected())
haveConnectedMobile = true;
}
return haveConnectedWifi || haveConnectedMobile;
}
And in your android manifest:
`<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />`
`<uses-permission android:name="android.permission.INTERNET" />`
`<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />`
Use this code : This code will work in all android version..
public static boolean isInternetOn(Context context)
{
boolean haveConnectedWifi = false;
boolean haveConnectedMobile = false;
ConnectivityManager cm = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo[] netInfo = cm.getAllNetworkInfo();
for (NetworkInfo ni : netInfo)
{
if (ni.getTypeName().equalsIgnoreCase("WIFI"))
if (ni.isConnected())
haveConnectedWifi = true;
if (ni.getTypeName().equalsIgnoreCase("MOBILE"))
if (ni.isConnected())
haveConnectedMobile = true;
}
return haveConnectedWifi || haveConnectedMobile;
}
It returns the true if internet is on and false when internet is not available.
You can use following code to get Connection status.
public abstract class NetworkUtils
{
public static boolean isNetworkConnected(Context context)
{
ConnectivityManager connectivityManager = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
return networkInfo != null && networkInfo.isConnected();
}
}
You need to define following permissions in the AndroidManifest.xml file
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
Now you can call above method using following lines of code in any activity you want,
if (!NetworkUtils.isNetworkConnected(getApplicationContext()))
{
// msg to display.
}
public static boolean isNetworkAvailable(Context context)
{
return ((ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE)).getActiveNetworkInfo() != null;
}
or
public static boolean isNetworkAvailable(Context context) {
boolean outcome = false;
if (context != null) {
ConnectivityManager cm = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo[] networkInfos = cm.getAllNetworkInfo();
for (NetworkInfo tempNetworkInfo : networkInfos) {
/**
* Can also check if the user is in roaming
*/
if (tempNetworkInfo.isConnected()) {
outcome = true;
break;
}
}
}
return outcome;
}
Don't forget to add to manifest file this line:
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
Hi make a class ConnectionDetector as follows
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
public class ConnectionDetector {
private Context _context;
public ConnectionDetector(Context context){
this._context = context;
}
public boolean isConnectingToInternet(){
ConnectivityManager connectivity = (ConnectivityManager) _context.getSystemService(Context.CONNECTIVITY_SERVICE);
if (connectivity != null)
{
NetworkInfo[] info = connectivity.getAllNetworkInfo();
if (info != null)
for (int i = 0; i < info.length; i++)
if (info[i].getState() == NetworkInfo.State.CONNECTED)
{
return true;
}
}
return false;
}
}
you can use this class in given following way
ConnectionDetector detector= new ConnectionDetector(getApplicationContext());
Boolean isInternetPresent = detector.isConnectingToInternet(); // true or false
with this boolean isInternetPresent you can proceed further in development.
if (isInternetPresent) {
// Internet Connection is Present
} else {
// Internet connection is not present
}
in support of all this need to add permission into your manifest.xml
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />