Both wifi and data connection are enabled.
Since I need to use mobile data to send a http request to mobile carrier to get phone number, But android will use wifi as prior, so How can I use data connection instead of WIFI?
When I enable the wifi and mobile data int the device. I use getAllNetworks() method, but it always returns wifi. I don't know Why getAllNetworks just return wifi when I enable both wifi and mobile data?
When I just enable the mobile data, the getAllNetworks() return mobile data info.
ConnectivityManager connectivityManager = (ConnectivityManager) this.getSystemService(Context.CONNECTIVITY_SERVICE);
Network[] network = connectivityManager.getAllNetworks();
if(network != null && network.length >0 ){
for(int i = 0 ; i < network.length ; i++){
NetworkInfo networkInfo = connectivityManager.getNetworkInfo(network[i]);
int networkType = networkInfo.getType();
if(ConnectivityManager.TYPE_MOBILE == networkType ){
connectivityManager.bindProcessToNetwork(network[i]);
}
}
}
Does some one know how to use data connection instead of WIFI when both wifi and data connection are enabled?
You can use data connection instead of WIFI only if you are working on Android Lollipop.
And it seems you are trying to use Android Lollipop with target API 23 because you used bindProcessToNetwork instead of setProcessDefaultNetwork.
Android Lollipop allows the multi-network connection.
ConnectivityManager cm;
cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkRequest.Builder req = new NetworkRequest.Builder();
req.addCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET);
req.addTransportType(NetworkCapabilities.TRANSPORT_CELLULAR);
cm.requestNetwork(req.build(), new ConnectivityManager.NetworkCallback() {
#Override
public void onAvailable(Network network) {
//here you can use bindProcessToNetwork
}
});
Related
I have to use WiFi and lte at the same time on Android.
Using ConnectivityManager, I was able to send a request via lte while wifi was active
However, I never used the two at the same time.
I currently use these methods.
private static ConnectivityManager cm;
private static NetworkRequest.Builder req;
cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
req = new NetworkRequest.Builder();
req.addCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET);
req.addTransportType(NetworkCapabilities.TRANSPORT_CELLULAR);
cm.requestNetwork(req.build(), new ConnectivityManager.NetworkCallback() {
#Override
public void onAvailable(Network network) {
//here you can use bindProcessToNetwork
cm.bindProcessToNetwork(network);
}
});
Is there a way I can request wifi and lte at the same time?
You can use onAvailable() of NetworkCallback to check for network changes.
In addition, bindProcessToNetwork() declares that only certain networks will be used.
Use ConnectivityManager.getAllNetworks() with the my simple example below.
ConnectivityManager connectivityManager = (ConnectivityManager) getApplicationContext()
.getSystemService(Context.CONNECTIVITY_SERVICE);
Network []networks = connectivityManager.getAllNetworks();
for(int i = 0 ; i < networks.length ; i++) {
NetworkInfo info = connectivityManager.getNetworkInfo(networks[i]);
if(info.getTypeName().equals("MOBILE")) {
// This is mobile network (3G/LTE)
// Use networks[i].bindSocket(java.net.Socket) for low-level socket communication
// Use networks[i].openConnection(java.net.URL) for HTTP request
}
else if(info.getTypeName().equals("WIFI")) {
// This is WiFi network
// Use networks[i].bindSocket(java.net.Socket) for low-level socket communication
// Use networks[i].openConnection(java.net.URL) for HTTP request
}
else {
// This is another network (WIFI_P2P, Ethernet, Bluetooth, ...)
}
}
I am working on an app that saves your credentials and logs in to the university wifi automatically when connected to a particular ssid. Everything is working fine. But when mobile data is on and wifi gets connected, the login request is sent through the mobile data (since the wifi connection is not active).
I read on the documentation and tried this.
if(Build.VERSION.SDK_INT >= 21){
ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkRequest.Builder requestBuilder = new NetworkRequest.Builder();
requestBuilder.addTransportType(NetworkCapabilities.TRANSPORT_WIFI);
connectivityManager.registerNetworkCallback(requestBuilder.build(), new ConnectivityManager.NetworkCallback() {
#Override
public void onAvailable(Network network) {
ConnectivityManager.setProcessDefaultNetwork(network);
}
});
}
new LoginTask().execute();
This doesn't solve my problem.
Any suggestions?
I solved this problem myself.
ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
Network[] networks = connectivityManager.getAllNetworks();
NetworkInfo networkInfo;
Network network;
for (int i = 0; i < networks.length; i++){
network = networks[i];
networkInfo = connectivityManager.getNetworkInfo(network);
if ((networkInfo.getType() == ConnectivityManager.TYPE_WIFI) && (networkInfo.getState().equals(NetworkInfo.State.CONNECTED))) {
ConnectivityManager.setProcessDefaultNetwork(network);
break;
}
}
I need to connect my mobile by wifi to a connected device. This hot spot have not internet.
Before 5.0 I did like this to check if I was connected to the wifi
ConnectivityManager connManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo mWifi = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
if(mWifi.isConnected()&& wifiManager.getConnectionInfo().getNetworkId()==addedNetwork)
But now with the 5.0 version of android, when I check isConnected it always returns false because of the none connectivity to internet.
How can I manage this case ?
Regards
I haven't tried this out but as per the google docs this should work
ConnectivityManager cManager = (ConnectivityManager) getSystemService(Activity.CONNECTIVITY_SERVICE);
NetworkRequest.Builder builder = new Builder();
//Indicates that this network should be able to reach the internet.
builder.addCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET);
NetworkRequest build = builder.build();
cManager.requestNetwork(build, new NetworkCallback() {
#Override
public void onAvailable(Network network) {
super.onAvailable(network);
// do what you want to do here
}
});`
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);
First of all, Android phone is connected both 3G network and wifi.
At this time I'd like to send http request through 3G network without using wifi.
How can I do it?
I don't think you can do this because in Android only one Network is active at any point of time. So for that first you need to check which network is active and then if it is a Wi-Fi one, then disconnect it, then Android will fallback to other one which will be 3G (if there is no other wi-fi network available), then you can send your request which will go through 3G network.outline might look like this:ConnectivityManager cm = (ConnectivityManager)Context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo ni = cm.getActiveNetworkInfo();
if(ni == null)
//no connectivity, abort
if(ni.getType() == ConnectivityManager.TYPE_WIFI || ni.getType() == ConnectivityManager.TYPE_WIMAX) {
WifiManager wm = (WifiManager)Context.getSystemService(Context.WIFI_SERVICE);
if( wm != null)
wm.disconnect();
//this will force android to fallback to other available n/w which is 3G
}
while(true) {
NetworkInfo ni = cm.getActiveNetworkInfo();
if( ni != null && ni.getType() == ConnectivityManager.TYPE_MOBILE && ni.isConnected()) {
//send your http request
break;
}
//sleep for some time, so that android can connect you to other n/w
}
You might need to loop through all active n/w and disconnect them till you find 3G network. I am assuming that there is just one Wi-Fi network and one 3G network available.