Restricting Android syncing to WIFi - android

I am new to Android and am trying to write a SyncAdapter - is it possible to restrict the SyncAdapter to only perform a sync when the network is WiFi (and not 3G etc.)?

you can make check for wifi as
ConnectivityManager cm = (ConnectivityManager) YourActivity.this.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo info = cm.getActiveNetworkInfo();
if(info.getType()==ConnectivityManager.TYPE_WIFI)
{
System.out.println("WiFi Connected");
}
else
{
System.out.println("WiFi not connected");
}
and also add permission in manifest as
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

The answer above is good, but I would add a HttpHead-Request (HTTPGet is also possible but you receive more data than needed) that is calling e.g. google.com so you can be sure that you are really connected to the internet.
The above posted solution just checks if you have network access but does not guarantee that you have inernet access.

Related

detect if device connect to which network and then send data from server depends on which network connected

I want to check if my android device connected to which network (either wifi, home network, or mobile network) and then I want to use different the url link (use to send data to server) depends on which network the device connected. I'm new to android, and I already googled it but doesn't seems to find the answer or because of my limited vocabulary. Can anybody help me?
try to use this mate
final ConnectivityManager connMgr = (ConnectivityManager)
getContext().getSystemService(Context.CONNECTIVITY_SERVICE);
final android.net.NetworkInfo wifi = connMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
final android.net.NetworkInfo mobile = connMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
if (wifi.isConnected()) {
//do what you want
} else if (mobile.isConnected()) {
//do what you want
}
and dont forget to put this on your AndroidManifest.xml
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>

Android - force network requests go through wifi instead of mobile network

I have an app which connects to a hardware device Wi-Fi hotspot.
It seems that Android forward requests over other networks (3G/4G for example) instead the hotspot, since my hot spot has no internet connection.
Is there any way to force network stream to work on the wifi?
I've come across the following function, but it's deprecated:
https://developer.android.com/reference/android/net/ConnectivityManager.html#setNetworkPreference(int)
Per the Connecting your App to a Wi-Fi Device blog post:
To direct all the network requests from your app to an external Wi-Fi device, call ConnectivityManager#setProcessDefaultNetwork on Lollipop devices, and on Marshmallow call ConnectivityManager#bindProcessToNetwork instead, which is a direct API replacement.
Use the ConnectivityManager to get the state of the Wifi adapter and then you can check if it is connected or even available using NetworkInfo.
ConnectivityManager connManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo wifi = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
if (wifi.isConnected()) {
// Continue with logic
}
In case he is not connected to wifi then don't proceed else continue you flow.
Add the following permission in you Manifest file
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

How to awake the Wifi or 3G of an Android device by app?

I'm developing a server websocket based application for Android but usually doesn't works correctly. When it fails and I try to ping the device from my PC, I can't access it. I must reset the device or turn On the flight mode and turn it off again. Then, I can reach the device with a ping command and my server works well.
Any suggestion?
Thanks.
Edited:
I found the solution here:
http://www.framentos.com/en/android-tutorial/2012/02/27/how-to-prevent-wi-fi-sleep-on-android/
import android.net.wifi.WifiManager;
import android.net.wifi.WifiManager.WifiLock;
WifiManager wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
WifiLock lock = wifiManager.createWifiLock(WifiManager.WIFI_MODE_FULL_HIGH_PERF, "LockTag");
lock.acquire();
Edited again...
Nop, I thaught that it will work but it doesn't...
You may want the application to ensure that Wifi is enabled on the device. You cannot enable it programmatically but you can open the wireless settings menu so the user can enable it. To detect if there is a network connection you can use the following code:
ConnectivityManager connectivityManager
= (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
if(activeNetworkInfo != null && activeNetworkInfo.isConnected()) {
// There is a connection
...
}
else {
// No connection; open the wireless settings menu
startActivity(new Intent(android.provider.Settings.ACTION_WIRELESS_SETTINGS));
...
}
You will also need the following permissions in your manifest:
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />

Using Robotiumin how to check or make a test case to check the network connection availability in android?

I am new in android testing I am using Robotium for testing purpose and I have a scenario where I need to check is the network (WiFi, 3G, 2G)connection is available or not, so how can i write a test case for this. Please help to solve this..
Thanks.
Source: How to check internet access on Android? InetAddress never times out
public boolean isOnline() {
ConnectivityManager cm =
(ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
return cm.getActiveNetworkInfo().isConnectedOrConnecting();
}
You'll need these in your manifest
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
I personally had to run a specific check to see if the user could access my server (Possibility of firewall blocking, or the user having only a local connection)
Good Luck!!

Android terminate wifi ability within application

One of the major requirments of my application is using cellular internet connection only.
In case of detecting wifi connection, the application have to disable the wifi ability and connecting through the cellualr internet connection.
How it can be done?
Thanks,
Eyal.
Checking WIFI connection
You can use the following code to check if the active network (if any - do some null checks here) is of type WIFI.
ConnectivityManager connManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = connManager.getActiveNetworkInfo();
if (netInfo!=null) {
// detect type using netInfo.getType()
}
There's a TYPE_WIFI int constant in ConnectivityManager that you can use for your check. (getType returns an int to identify the network type).
More info on the Activity Manager can be found on the Android dev site.
Disabling WIFI
n order to disable the WiFi state, you have to grant the following permission in the application manifest
android.permission.CHANGE_WIFI_STATE
You can then use the WifiManager to set enable/disable the WIFI.
WifiManager wifiManager = (WifiManager)getBaseContext().getSystemService(Context.WIFI_SERVICE);
wifiManager.setWifiEnabled(false);
More info on http://developer.android.com/reference/android/net/wifi/WifiManager.html
For a complete sample, check the following post : http://android-er.blogspot.com/2011/01/turn-wifi-onoff-using.html

Categories

Resources