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;
}
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);
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?
I need to communicate over htttp or https from an android, and I need a settings for my app "wifi only". It's a restriction for the communication. How to implement such a thing? I cannot find any information on AndroidHttpClient works only with Wifi - or 3G as well, and how to restrict it.
This code will check for wifi connection.
public static boolean hasWIFIConnection(Context context)
{
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo wifiNetwork = cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
if (wifiNetwork != null && wifiNetwork.isConnected())
{
return true;
}
return false;
}
I've heard about the setNetworkPreference(int preference) method in the ConnectivityManager service. Although it is poorly documented, it appears this method can be used with integer constants which represents a network type.
You can find references to such constants in the ConnectivityManager class :
TYPE_MOBILE
TYPE_WIFI
...
I would start digging this way.
EDIT :
You will need to be granted the folowing permissions :
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission>
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"></uses-permission>
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE"></uses-permission>
try like this
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;}
starting from 23 getNetworkInfo is deprecated, use getActiveNetworkInfo
public boolean hasWIFIConnection()
{
ConnectivityManager connMgr = (ConnectivityManager) ContextProvider.getInstance().getApplication().getSystemService(Activity.CONNECTIVITY_SERVICE);
NetworkInfo activeNetwork = connMgr.getActiveNetworkInfo();
if (activeNetwork != null && (activeNetwork.getType() == ConnectivityManager.TYPE_WIFI) && activeNetwork.isConnected())
{
return true;
}
return false;
}
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