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;
}
Related
The code i am using to check internet connectivity so far is
private boolean isOnline() {
boolean haveConnectedWifi = false;
boolean haveConnectedMobile = false;
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;
}
return haveConnectedWifi || haveConnectedMobile;
}
my code is working fine till android 5.0 but in android 6.0 the app crashes can anyone help me regarding this issue and provide me a better code that can test internet connectivity
Thanks
Regards
getAllNetworkInfo is deprecated
you need to use: getActiveNetworkInfo()
code:
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
if (activeNetwork != null) { // connected to the internet
if (activeNetwork.getType() == ConnectivityManager.TYPE_WIFI) {
// connected to wifi
Toast.makeText(context, activeNetwork.getTypeName(), Toast.LENGTH_SHORT).show();
} else if (activeNetwork.getType() == ConnectivityManager.TYPE_MOBILE) {
//connected to Data
Toast.makeText(context, activeNetwork.getTypeName(), Toast.LENGTH_SHORT).show();
}
} else {
// not connected to the internet
}
From the ConnectivityManager documentation:
NetworkInfo[] getAllNetworkInfo ()
This method was deprecated in API level 23.
This method does not support multiple connected networks of the same type. Use getAllNetworks() and getNetworkInfo(android.net.Network) instead.
Bear in mind getAllNetworks() was introduced in API Level 21, so you may need to run two separate approaches based on the user's OS version.
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP){
// Use getAllNetworks()
} else{
// Use getAllNetworkInfo()
}
I have implemented code for connect internet through WifiManager.java. but i am not able to find any api to connect internet through Ethernet. Can any one help me.
Not that I am aware of.
Here is a method that detected WIFI and Data separately.
Perhaps you can alter this and put a prompt in there forcing user to turn off data and turn on WIFI.
public static boolean isNetworkConnected(Context context) {
boolean isConnectedWifi = false;
boolean isConnectedMobile = false;
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo[] netInfo = cm.getAllNetworkInfo();
for (NetworkInfo ni : netInfo) {
if (ni.getTypeName().equalsIgnoreCase("WIFI"))
if (ni.isConnected())
isConnectedWifi = true;
if (ni.getTypeName().equalsIgnoreCase("MOBILE"))
if (ni.isConnected())
isConnectedMobile = true;
}
return isConnectedWifi || isConnectedMobile;
}
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;
}
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
My question is about detecting that my cellphone wifi connection is bound to a network and is operationnal.
I Can see such a line in the LogCat (tagged "WifiMonitor")
VERBOSE/WifiMonitor(93): Event [CTRL-EVENT-CONNECTED - Connection to c4:3d:c7:89:cf:c0 completed (auth) [id=8 id_str=]]
That did let me hope that event was catchable... But How ?
Thank your for attention.
You should have a look at the ConnectivityManager: ConnectivityManager
And
public boolean isWifiOnline() {
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
if (netInfo != null && netInfo.isConnectedOrConnecting()) {
return true;
}
return false;
}