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

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

Related

Check network connection with LocationManager in Android

I would like to check the network connection with the LocationManager in Android.
My code works fine with Galaxy SII and Version 4.0.3.
But it does not work with Galaxy S and Version 2.3.6.
The code:
isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
I am connected to a Wi-Fi network for sure. But the if statement
if (!isGPSEnabled && !isNetworkEnabled)
returns true. Are there known bugs and is there a reliable way to check it?
Try this out:
//If you only want to check your Internet connection available or not
public static boolean isNetworkAvailable(Context context) {
ConnectivityManager connectivityManager = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
NetworkInfo wifiInfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
return activeNetworkInfo != null && activeNetworkInfo.isConnected();}
Note: wifiInfo provide you status about your Wifi, still it will be there poor connection I think it will good if make HTTP reuest, for more details check link provided.
Edit:
For more information you can [refer this][1].
I am using below code in my every android project.
It will check for any active network that are used for outgoing connection.
public static boolean isNetworkAvailable(Context context)
{
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = cm.getActiveNetworkInfo();
if (networkInfo != null && networkInfo.isConnectedOrConnecting())
return true;
return false;
}
Use this code:
public static boolean checkConn(Context ctx)
{
ConnectivityManager conMgr = (ConnectivityManager)ctx.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo wifiInfo = conMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
NetworkInfo mobileInfo = conMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
NetworkInfo info = conMgr.getActiveNetworkInfo();
if(info != null && info.isConnected()) {
Log.v("NetworkInfo","Connected State");
return true;
}
return false;
}

Wifi communication only

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

how to check a network is connected to wifi in android [duplicate]

This question already has answers here:
Detect network connection type on Android
(14 answers)
Closed 9 years ago.
Now I am developing an application. I checked the internet connection in android using ConnectivManager. I checked for Mobile 3g and for WIFI.I turn off the WIFI connection and checked it works as what i want. Then I turn on the WIFI connection but disconnected all networks in WIFI. Now I checked but it shows the WIFI is connected but I want to check whether a network is connected to WIFI when WIFI is on or not connected to WIFI when WIFI is on. I do not know how to code it. Can anyone please help with the required code?
Thanks in advance
public boolean isWifiEnabled(){
ConnectivityManager cm = (ConnectivityManager) c.getSystemService(
Context.CONNECTIVITY_SERVICE);
NetworkInfo wifiNetwork = cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
if (wifiNetwork != null && wifiNetwork.isConnected()) {
return true;
}
return false;
}
Don't forget to use the following permission
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" >
</uses-permission>
To Check if internet is connected via WiFi or mobile
public static boolean hasInternet(Activity a) {
boolean hasConnectedWifi = false;
boolean hasConnectedMobile = false;
ConnectivityManager cm = (ConnectivityManager) a.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo[] netInfo = cm.getAllNetworkInfo();
for (NetworkInfo ni : netInfo) {
if (ni.getTypeName().equalsIgnoreCase("wifi"))
if (ni.isConnected())
hasConnectedWifi = true;
if (ni.getTypeName().equalsIgnoreCase("mobile"))
if (ni.isConnected())
hasConnectedMobile = true;
}
return hasConnectedWifi || hasConnectedMobile;
}
With this piece of code you should be able to use the ConnectivityManager. From there you can check if it is connected or even available.
ConnectivityManager connManager = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);
NetworkInfo mWifi = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
if (mWifi.isConnected()) {
// Do whatever
}
To check network connection
public class CheckNetwork {
private static final String TAG = CheckNetwork.class.getSimpleName();
public static boolean isInternetAvailable(Context context)
{
NetworkInfo info = (NetworkInfo) ((ConnectivityManager)
context.getSystemService(Context.CONNECTIVITY_SERVICE)).getActiveNetworkInfo();
if (info == null)
{
Log.d(TAG,"no internet connection");
return false;
}
else
{
if(info.isConnected())
{
Log.d(TAG," internet connection available...");
return true;
}
else
{
Log.d(TAG," internet connection");
return true;
}
}
}
}
In your activities
if(Checknetwork.isInternetAvailable(MainActivity.this)
{
// do something
}
Remember to add permission in manifest file
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
This also check's if you have connection to mobile network or wifi.

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 the network availability?

I have to connect my app with server using either wifi (if it is available),
or gprs (if wifi is not available).
Here is my code to check the connection availability
public static final boolean isConnectionAvailable(Activity a)
{
ConnectivityManager cm = (ConnectivityManager)a.getSystemService(Context.CONNECTIVITY_SERVICE);
State mobile = cm.getNetworkInfo(0).getState();
State wifi = cm.getNetworkInfo(1).getState();
if (wifi == NetworkInfo.State.CONNECTED || wifi == NetworkInfo.State.CONNECTING)
{
return true;
}
if (mobile == NetworkInfo.State.CONNECTED || mobile == NetworkInfo.State.CONNECTING)
{
return true;
}
return false;
}
Is this a correct way? Can anyone suggest me a better way?
The following very similar approach works, but has the added advantage of not caring what the underlying medium, since it looks as though there is support for more than just WiFi. Maybe these are also covered by mobile, but the docs aren't super clear:
// added as an instance method to an Activity
boolean isNetworkConnectionAvailable() {
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo info = cm.getActiveNetworkInfo();
if (info == null) return false;
State network = info.getState();
return (network == NetworkInfo.State.CONNECTED || network == NetworkInfo.State.CONNECTING);
}
Network Availability Check :
private boolean isNetworkAvailable() {
ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
return activeNetworkInfo != null && activeNetworkInfo.isConnected();
}
Or you could use some kotlin implementation
fun Context.isNetworkAvailable(): Boolean {
val cm = getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
val activeNetwork: NetworkInfo? = cm.activeNetworkInfo
return activeNetwork?.isConnectedOrConnecting == true
}

Categories

Resources