Detect internet connection
First)
Add permission to manifest.xml
Make a class ConnectDetect
Make a object of ConnectDetect and call funtion
First)
Add permission to manifest.xml
<!-- Internet Permissions --> <uses-permission android:name="android.permission.INTERNET" /> <!-- Network State Permissions -->
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
Second)
Make a class ConnectDetect
public class ConnectDetect { private Context context; public ConnectDetect(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; } }
Third)
Make a object of ConnectDetect and call funtion
isConnectingToInternet
to check
internet status
ConnectDetect cd =
new
ConnectDetect(getApplicationContext());
Boolean isInternetPresent = cd.isConnectingToInternet();
//Returns true if connected to internet ,false if not connected to internet
No comments:
Post a Comment