Hi I have Service which reads Info about connectivity
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
is it possible to mock the result of cm.getActiveNetworkInfo() just like it is possible for GPS?
Related
I tried to check Internet Connectivity in my App to handle if there is no internet with this code. It always returns true with Wi-Fi type.
ConnectivityManager connMgr = (ConnectivityManager)
getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
if (networkInfo != null && networkInfo.isConnected()) {
System.out.println( networkInfo.getTypeName());
Loading_News();
}
This is my code:
ConnectivityManager connMgr = (ConnectivityManager) getSystemService(Activity.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
but networkInfo is always null..
What can be the cause for this odd thing?
Docs say that getActiveNetworkInfo can return null if there is no default network set. "This may return null when there is no default network."
http://developer.android.com/reference/android/net/ConnectivityManager.html#getActiveNetworkInfo()
A better option might be to check the cell data or wifi connection.
Check your mobile connection.
ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo mobNetInfo = connectivityManage.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
Check your wifi connection.
ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo wifiNetInfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
Whether or not the device has 3g/data activated. Any idea of what's happening?
Thank you
My code:
public boolean isConnected3G(){
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo[] networks = cm.getAllNetworkInfo();
for (NetworkInfo ni : networks)
if ("MOBILE".equalsIgnoreCase(ni.getTypeName())){
Log.d(TAG,""+ni.isConnected());
if (ni.isConnected())
return true;
}
return false;
}
Try to use NetworkInfo ni = cm.getActiveNetworkInfo() instead of cm.getAllNetworkInfo();
and check the network information
I have an android app, in that there is a particular requirement where in I need to check if the Android device has a network provider or its just WiFi enabled.
Thank You.
This should do the trick.
ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
if(networkInfo != null && networkInfo.isConnected()){
//Your code here
}
final ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
final NetworkInfo networkInfo = cm.getActiveNetworkInfo();
int type = networkInfo.getType();
String typeName = networkInfo.getTypeName();
boolean connected = networkInfo.isConnected()
networkInfo.getType() will return 0 when connected to Mobile
network or 1 when connected trough WIFI.
networkInfo.getTypeName()
will return the strings mobile or WIFI.
networkInfo.isConnected()
will tell you whether or not you have an active connection.
You should take a look at the documentation.
isProviderEnabled(LocationManager.NETWORK_PROVIDER) is what you need
Following line will only not result in a nullpointer exception if I'm connected to a WLAN:
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
Otherwise I get a nullpointer-exception straight ahead? How can I fix this? I want my background service to only work, when it is connected to wlan. But that Nullpointerexception just kills the whole app...
Add to the manifest the following line:
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
Yes, I am late but in case someone is still looking,
ConnectivityManager cm = (ConnectivityManager) this.getSystemService(CONNECTIVITY_SERVICE);
NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
no need of Context in the getSystemService() argument.
ConnectivityManager cm = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);
NetworkInfo netInfo = cm.getActiveNetworkInfo();
if (netInfo != null && netInfo.getType() == 1) {
// CONNECTION_WIFI = true;
}