ConnectivityManager do not work on Motorola xoom (honeycomb 3.2) - android

I have a method call isNetworkAvailable()
to check if the user have enable wap/wifi/wimax this works for Android 2.1 to 2.3+.
But now a user of the app which uses honeycomb 3.2 on a Motorola xoom
rapport to me that he canĀ“t open the app.
In my android developer web-interface I can see this log-error: http://paste.ubuntu.com/811881/
private boolean isNetworkAvailable()
{
ConnectivityManager connec = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo mobileInfo = connec.getNetworkInfo(0);
NetworkInfo wifiInfo = connec.getNetworkInfo(1);
NetworkInfo wimaxInfo = connec.getNetworkInfo(6);
if (wimaxInfo!=null) {
return mobileInfo.isConnected() || wifiInfo.isConnected()|| wimaxInfo.isConnected();
}
else {
return mobileInfo.isConnected() || wifiInfo.isConnected();
}
}
See the whole Class/Activity here(line 276):
https://github.com/voidcode/Diaspora-Webclient/blob/master/src/com/voidcode/diasporawebclient/MainActivity.java

mobileInfo or wifiInfo could be null. On a wifi-only device I wouldn't be surprised if mobileInfo (ConnectivityManager.TYPE_MOBILE) is null.

I have faced same problem with Motorola Xoom, because it does not have connectivity support for ConnectivityManager.TYPE_MOBILE.
The following code is working fine for me:
ConnectivityManager connMngr = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
try {
return connMngr.getActiveNetworkInfo().isConnected();
} catch (NullPointerException npe) {
return false;
}

Related

Unable to access isNetworkSupported in android ConnectivityManager class

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();

how to check internet connection in Google nexus 7 (android 4.2)

I used following code for checking internet connection.
public static boolean checkNetworkConnection(Context context) {
boolean connected = true;
ConnectivityManager connectivityManager = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
if (connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE)
.getState() == android.net.NetworkInfo.State.CONNECTED
|| connectivityManager.getNetworkInfo(
ConnectivityManager.TYPE_WIFI).getState() == android.net.NetworkInfo.State.CONNECTED) {
connected = true;
} else
connected = false;
}
This code is working fine in below mobile. But it is not working in Google nexus 7 (android 4.2).
When I test this code in Google nexus 7 (android 4.2). I got error.
Null pointer exception in connection manager
For me it work:
public static boolean isInternetEnabled() {
ConnectivityManager conMgr = (ConnectivityManager) YourApp.context
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = conMgr.getActiveNetworkInfo();
if (netInfo != null && netInfo.isConnectedOrConnecting())
return true;
else
return false;
}
You will need this permission in your manifest:
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
Check TYPE_MOBILE is null or not using below code.
connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE) != null

android tablet has no phone and code gives error when checking

I have an app that works fine on a android phone , but when I try to run it on the Nexus7 which has no phone the code fails with a force stop at the location indicated. What is the solution? How do I check to see if the feature is there and what should I do to solve this?
ConnectivityManager connMgr = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
boolean isWifiConn = networkInfo.isConnected();
printi("oopsA",6);
networkInfo = connMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
printi("oopsB",6);
boolean isMobileConn = networkInfo.isConnected(); //<<<<FAILS HERE ON NEXUS 7
Your networkInfo is probably null. You have to test that before. This means you can't access to this type of connectivityManager.
Try this:
networkInfo = connMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
boolean isMobileConn = false;
if(networkInfo != null)
isMobileConn = networkInfo.isConnected();
I have faced same problem with Motorola Xoom, because it does not have connectivity support for ConnectivityManager.TYPE_MOBILE.
Following code is working fine for me :
ConnectivityManager connMngr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
try {
return connMngr.getActiveNetworkInfo().isConnected();
}
catch (NullPointerException npe) {
return false;
}
Check permissions in AndroidManifest.xml
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<user-permission android:name="android.permission.ACCESS_WIFI_STATE" />
Corrected Code as follows:
IsAPhone=0;
try{
boolean isMobileConn = false;
if(networkInfo != null){ isMobileConn = networkInfo.isConnected();IsAPhone=1;}
}
catch (Exception e) {}

How to check if mobile network is enabled/disabled

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);

How to check whether a particular device supports 4G networks in Android?

I want to check if a particular device has hardware support for 4G networks.
I will elaborate the issue...
In the application we have a settings page where user can make selection and allow application to run only in selected networks.
Eg. User can select that app will run only in WiFi network or only in 3G network etc.
There are CheckBox preferences for all networks WiFi, 2G, 3G 4G etc.
Now if the device doesn't have the support for 4G network, I want to hide the 4G selection checkbox.
All the remaining functionality is complete. I am struck on just this issue that how to detect if device support 4G or not?
Please note that I want to detect hardware support for 4G on the device and NOT the 4G connection is connected or so.
Any help is greatly appreciated.
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo[] info = cm.getAllNetworkInfo();
for(int i=0; i <info.length; i++){
Log.i("netinfo"+i, info[i].getType()+"");
Log.i("netinfo"+i, info[i].getTypeName());
Log.i("netinfo"+i, info[i].getSubtype()+"");
Log.i("netinfo"+i, info[i].getSubtypeName());
}
Use this piece of code and get all available options on the device. I think you should be able to get all capabilities of the device.
and network type info is present in http://developer.android.com/reference/android/telephony/TelephonyManager.html.
I think network types added from API level 11 are for 4g. Check it yourself.
This might work. It should check for WiMax 4g:
private boolean is4gavailable() {
ConnectivityManager connec = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo mobileInfo = connec.getNetworkInfo(0);
NetworkInfo wifiInfo = connec.getNetworkInfo(1);
NetworkInfo wimaxInfo = connec.getNetworkInfo(6);
if (wimaxInfo!=null) {
return mobileInfo.isConnectedOrConnecting() || wifiInfo.isConnectedOrConnecting() || wimaxInfo.isConnectedOrConnecting();
}
else {
return mobileInfo.isConnectedOrConnecting() || wifiInfo.isConnectedOrConnecting();
}
}
Try looking at this for a little more clarification.
Context context = MainActivity.this;
public synchronized static boolean isNetAvailable(Context context){
boolean isNetAvailable=false;
if ( context != null ){
ConnectivityManager mgr = (ConnectivityManager)
context.getSystemService(Context.CONNECTIVITY_SERVICE);
if ( mgr != null )
{
boolean mobileNetwork = false;
boolean wifiNetwork = false;
boolean wiMaxNetwork = false;
boolean mobileNetworkConnecetd = false;
boolean wifiNetworkConnecetd = false;
boolean wiMaxNetworkConnected = false;
NetworkInfo mobileInfo = mgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
NetworkInfo wifiInfo = mgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
NetworkInfo wiMaxInfo = mgr.getNetworkInfo(ConnectivityManager.TYPE_WIMAX);
if ( mobileInfo != null )
mobileNetwork = mobileInfo.isAvailable();
if ( wifiInfo != null )
wifiNetwork = wifiInfo.isAvailable();
if(wiMaxInfo != null)
wiMaxNetwork = wiMaxInfo.isAvailable();
if(wifiNetwork == true || mobileNetwork == true || wiMaxNetwork == true){
mobileNetworkConnecetd = mobileInfo.isConnectedOrConnecting();
wifiNetworkConnecetd = wifiInfo.isConnectedOrConnecting();
wiMaxNetworkConnected = wiMaxInfo.isConnectedOrConnecting();
}
isNetAvailable = ( mobileNetworkConnecetd || wifiNetworkConnecetd || wiMaxNetworkConnected );
}
}
return isNetAvailable;
}
Check for value of isNetAvailable is true in all the 3 cases this works for me, wish work for u too
Note : 4G availability will be introduces API level 8 onwards.

Categories

Resources