I have a service which polls every hour attempts to sync data automatically to the web from the device.
I have it working but I need to find a way to establish an internet connection if there is not one available.
For example - If my phone is in standby mode (screen locked for a period of time) the internet connection is dropped and it is unable to sync when it needs to.
I would like to attempt to connect via wifi if available, then using 3G if required.
Is there a reliable way to do this?
UPDATE -
I found an article online which uses this code to attempt connection via 3G if there is no wifi available :
int resultInt = connectivityManager.startUsingNetworkFeature(ConnectivityManager.TYPE_MOBILE, "enableHIPRI");
unfortunately it doesn't seem to work on all devices, maybe I need to try other options than "enableHIPRI" ??
Any ideas?
OK, although I haven't been able to solve this problem I have discovered that the main cause for the delay in between sync attempts is because the cpu gets stopped during sleep mode, this means my timer task was paused..
My workaround was to rewrite the service using a wakeful intent to get around the timer issue.
This makes my solution much more reliable so I am closing this question! :)
You should be able to do most of that using ConnectivityManager. It allows you to query the available types of networks and using requestRouteToHost you should be able to ensure there's a connection set up that you need for syncing.
I think you cannot rely on such thing on mobile phone, as in any embedded device you cannot be sure if the device will connect and stay connected when you want. You can only leave a message for the user that data was not synchronized or try to minimize it by checking if there is connection and then synchronizing not just in one hour.
Related
You can use the TYPE_MOBILE_HIPRI network type in order to force connections to a particular host to go over the mobile connection rather than wifi, as described in Rainbowbreeze's answer here: How to use 3G Connection in Android Application instead of Wi-fi?. Unfortunately, the system enforces a timeout on these connections which is too short for my application, meaning it is effectively useless to me. Is there some way of cancelling this timeout, or another way of achieving the same results? If it helps, I am happy to restrict my application to running on rooted phones only.
I got around this issue by refreshing hipri connection after every 50 sec.The system time out for a hipri connection is 1 min.So call 'enableHipri' after every 50sec.
This strange thing just happens: whenever a startDiscovery() is called, the application is (as you would expect) still responding during the next initial 10-12 seconds. But whenever a Http Request is fired during this period it is stalled (no data could be requested or downloaded on a http connection).
I use the DefaultHttpClient.execute() method to request data from over the wifi network.
I not only see this happening in my own application. I also see this when the application is doing bluetooth discovery in the background and I want to download something off the Google Market place.
Had someone experienced something similar?
UPDATE 18-aug-'11:
When listening to a stream on the internet: I noticed the stream was dropping out on me whenever discovery was activated. I looked at the active network traffic and indeed I could confirm the two drops. The things that wonders me, as this was the network traffic from my computer and not my android device. Soo it seems the android devices somehow conflicts with the active wifi network?
Anybody familiar with this?
Like many apps my app depends on WIFI and Http Connection to operate. What I am not clear on is how I can time a wait until WIFI is enabled AND device is attached to WIFI network is. What is the best way to delay on application startup long enough for this to happen? during this time I would for example keep buttons deactivated. Also is there any way to make the device attempt to connect to its preferred network? Thanks
Take a look at ConnectivityManager and WiFiManager. Using these you can get events when network state changes
Use WifiManager to get the state of the connection.
Generally you will have to try. A Wifi connection can show as connected but not be able to actually send/receive because the signal is too weak. Once the connection is up, the app should try connecting and only when it succeeds, the buttons should be activated and so forth.
By default, Wifi sleep policy is "Sleep on screen idle".
With this policy, is it possible for a Background Service at a later time to wake up Wifi using some API?
Am trying the following, but does not work:
When my Background Service wakes up, it calls "ConnectivityManager.getActiveNetworkInfo()" to get active network.
Since, the wireless is off on idle, I tried waking it up using "WifiManager.startScan" on a previously used Wifi connection.
But still dont get Wifi connectivity.
Any ideas?
I preferably do not want to change my sleep policy to "Never".
Thanks
Hemant
There are no real simple solutions for this. To with a high probability ensure you have WIFI connectivity when the phone/screen goes to sleep the best way is to turn it off. Look here for a lot of details - http://wififixer.wordpress.com/
It is important to realize that in sleep mode the Wifi enters a low power mode. This will become tricky then to programmatically check as it might have connectivity to the Wifi but the Wifi connection is too weak or too slow to complete the HTTP request and hence it times out. This would force you to also check the speed of the Wifi connectivity as well as you will have an active network but a pretty lousy one.
Proper handling of the escaping when timeout occurs for the HTTP call you make makes it ok to use but ultimately the only way to have a background thread constantly running to get data is only doable when you have the Wifi mode to never sleep.
It is tricky and not the best way I know. :-( It is however the only path I have found which is reliable enough.
I am testing a database storage after retrieving data from the internet, I would like to be able to start the emulator with internet working, and then, while it is running, stop internet access to force it using the database as a source to display data.
Is that possible?
You can also use F8 to set the cell network on/off.
See here
Disconnect your development machine from the network.
To test offline functionality in my Android emulator, I use the phone's settings to put the emulator in Airplane Mode, or shut off both cellular and WiFi.
Taking one step further the line of reasoning of disconnecting the development machine.
I've found, when faced with the same intent as the original question (test data persistence) that the easiest approach was to make my development server error out.
This approach has the extra benefit off making you think about dealing with server errors, since from the client standpoint being offline because of poor cellular network reception may be the same as i.e. the server being unreachable because of DNS configuration issues.
Of course it will not technically resemble airplane mode, or allow for any other complex connectivity testing based behaviour, but it may fit the same purpose.