I want to check if my Android app is connected to the Internet. I copied the code I read in a book, here it is:
ConnectivityManager cm = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = cm.getActiveNetworkInfo();
int networkType = networkInfo.getType();
android.net.NetworkInfo.State networkState = networkInfo.getState();
if (networkState.compareTo(State.CONNECTED)==0)
{
//We are connected!!!
}
I've also given my app the permission to access network state, but Eclipse says this next to State.CONNECTED:
CONNECTED cannot be resolved or is not a field.
Even books are wrong, now? x(
Thanks in advance.
you have imported the wrong state
change
if (networkState.compareTo(State.CONNECTED)==0)
to
if (networkState.compareTo(android.net.NetworkInfo.State.CONNECTED)==0)
Related
I am trying use isNetworkSupported(int networkType) to check hardware is support wifi only mode. But it giving error like "The method isNetworkSupported(int) is undefined for the type ConnectivityManager
Following is my code:
ConnectivityManager cm = (ConnectivityManager)this.getSystemService(Context.CONNECTIVITY_SERVICE);
boolean checkStatus = cm.isNetworkSupported(ConnectivityManager.TYPE_MOBILE);
Please tell me how we can access this isNetworkSupported method inside our activity.
Thanks.
As per the document, isNetworkSupported is not the method for the class ConnectivityManager.
if you want check internet connection status check this
ConnectivityManager cm =
(ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
boolean isConnected = activeNetwork != null &&
activeNetwork.isConnectedOrConnecting();
But I am able to see this method in https://android.googlesource.com/platform/frameworks/base.git/+/android-4.3_r2.1/core/java/android/net/ConnectivityManager.java and in android studio also it is showing this method.
Thanks guys!
After some research and with my colleagues helps I used java reflections to solve this issue
like below.
ConnectivityManager cm = (ConnectivityManager)
this.getSystemService(Context.CONNECTIVITY_SERVICE);
final Class<?> cmlClass = cm.getClass();
String status = "";
try {
final Method wifiCheckMethod = cmlClass.getMethod("isNetworkSupported", int.class);
boolean hasMobileNetwork = (Boolean) wifiCheckMethod.invoke(cm, ConnectivityManager.TYPE_MOBILE);
status = hasMobileNetwork ? "This device has mobile support model" : "This is wifi only model";
Log.i(getClass().getSimpleName(), "The network status is..."+hasMobileNetwork);
} catch (Exception ex) {
ex.printStackTrace();
status = "Error while getting device support model";
}
Toast.makeText(MainActivity.this, "Network support message.."+status, Toast.LENGTH_SHORT).show();
I would like to know if the mobile network is enabled or disabled.
My application is designed to help the user when he receives a phone call, and to do this I need Internet access. Thus, I would like to display an information box when the user access the application for the first time if Wi-Fi has a sleep policy and Mobile network is disabled. (I need Internet within milliseconds after the phone start ringing).
I found Settings.System.WIFI_SLEEP_POLICY, but I can't find any information on how to check if mobile network is disabled (when Wi-Fi is on and working).
Any help would be appreciated!
Edit:
The problem is that I want to know if mobile network is turned of by the user (while the phone could have WiFi access at the time).
I finally found a solution. Apparently not all phones have this option:
Home > Menu > Settings > Wireless & networks > Mobile network (checkbox)
However, for those who do, this method will work:
/**
* #return null if unconfirmed
*/
public Boolean isMobileDataEnabled(){
Object connectivityService = getSystemService(CONNECTIVITY_SERVICE);
ConnectivityManager cm = (ConnectivityManager) connectivityService;
try {
Class<?> c = Class.forName(cm.getClass().getName());
Method m = c.getDeclaredMethod("getMobileDataEnabled");
m.setAccessible(true);
return (Boolean)m.invoke(cm);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
apparently there is an alternative, more clean solution then the Reflection approach:
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". and networkInfo.isConnected() will tell you whether or not you have an active connection.
UPDATE FOR ANDROID 5.0+ (API 21+)
Calling getMobileDataEnabled via the reflection leads to NoSuchMethodException on Android 5.0+ on some devices. So in addition to accepted answer you may wanna do second check if NoSuchMethodException is thrown.
...
catch(NoSuchMethodException e)
{
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
{
isDataEnabled = Settings.Global.getInt(context.getContentResolver(), "mobile_data", 0) == 1;
}
}
you can use below code this is working for all API versions:
ConnectivityManager cm =
(ConnectivityManager)mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
boolean isConnected = activeNetwork != null &&
activeNetwork.isConnectedOrConnecting();
if(isConnected)
{
if(activeNetwork.getType()==ConnectivityManager.TYPE_MOBILE)
return true;
else
return false;
}
else
return false;
PackageManager pm = context.getPackageManager();
boolean hasTelephony = pm.hasSystemFeature(PackageManager.FEATURE_TELEPHONY);
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
In my application I have checked whether ineternet connection is there using the below code
private boolean isNetworkAvailable() {
ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
return activeNetworkInfo != null; }
Now the problem is that how can i check whether conncetion to one remote computer Ip is exist or not...I need to connect to an external IP webservice.....Need to check connetion to that .Suppose the link is like as given below
http://192.168.1.158/VisionEPODWebservice/Manifest.asmx
InetAddress.getByName(ip).isReachable(timeout);
see documentation of InetAddress
Is there any reference about settings.db database file?
I see mostly obviously variables but there is not enough for confidence.
On a rooted device, you can pull the file from /data/data/com.android.providers.settings/databases/settings.db (you will need to do adb root before)
Then you can open it a with a sqlite database explorer:
The names are self-explanatory. If one confuses you, a simple google search should help.
There is hardly any reference. You should be using the Settings.System class to be access the system settings in Android. Using the Settings.System class, you are able to both read and write to the system settings.
Checking Mobile Network:
ConnectivityManager nInfo = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
nInfo.getActiveNetworkInfo().isConnectedOrConnecting();
Log.d(tag, "Net avail:"
+ nInfo.getActiveNetworkInfo().isConnectedOrConnecting());
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = cm.getActiveNetworkInfo();
if (netInfo != null && netInfo.isConnectedOrConnecting()) {
Log.d(tag, "Network available:true");
return true;
} else {
Log.d(tag, "Network available:false");
return false;
}