Reputation: 389
Hello my app is connected to a WIFI without access to internet. How can I get the return of isconnectedtoprovisioningnetwork()? I want to get if it's true or false and relate it in my code.
Here is my code:
public static boolean isConnected(Context context) {
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 && info[i] != null && info[i].isAvailable() && info[i].isConnected() )
return true;
}
return false;
}
Upvotes: 0
Views: 894
Reputation: 1371
This code will tell you either you are connected to WIFI or not.
public boolean isConnection(Context ctx) {
ConnectivityManager connManager = (ConnectivityManager)ctx.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo mWifi = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
if (mWifi!=null && mWifi.isConnected()) {
return true;
}
return false;
}
Do not forget to add permission.
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
Upvotes: 1
Reputation: 1213
public static boolean checkInternetConnection(Context context) {
ConnectivityManager conMgr = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
// ARE WE CONNECTED TO THE NET
if (conMgr.getActiveNetworkInfo() != null
&& conMgr.getActiveNetworkInfo().isAvailable()
&& conMgr.getActiveNetworkInfo().isConnected()) {
return true;
} else {
return false;
}
}
Upvotes: 1