1

In my Android application I first get the users location using either GPS, the GSM network or a text value that is manually input via a Settings screen.

At first I used the Geocoder class to get the users locale with latitude and longitude provided by GPS or GSM but it seemed unreliable, now I used Google's web API to get the locale by making a HTTP request and parsing the XML document returned. This is using the following URL:

http://maps.googleapis.com/maps/api/geocode/xmllatlng=blahblahlat,blahblahlong&sensor=true

After getting the users locale I then send another HTTP request to free.worldweatheronline.com's weather API and parse the XML returned. With a stable internet connection the application runs fine, however my house has a rubbish signal and even worse mobile data connection.

I am aware there is a simple method to check if the device has a mobile data connection, however what I want to know is whether there is a way of measuring the signal strength as the problem arises when there is a connection, but it is too bad to successfully run. For example, if there is a way to get mobile data signal strength which returns a value from 0 (no signal) to 100 (full signal), I can then only carry out the location and weather retrieval if signal strength is above a certain amount.

Would it be best to just surround the location and weather retrieval code with a try/catch so it doesn't cause a runtime exception, allowing it to just fail gracefully then update when a better signal is acquired? Any ideas on how to accomplish this, or any other suggestions to make my app more friendly for users with bad signal?

Thanks in advance!

William Stewart
  • 841
  • 1
  • 14
  • 27

2 Answers2

1

You can check cell service signal strength by creating a PhoneStateListener and handle the onSignalStrengthChanged callback. For more info look at this question How to get cell service signal strength in Android?

EDIT : You can pause PhoneStateListener by calling telephony.listen(listener, PhoneStateListener.LISTEN_NONE); to not receive updates when you don't need them.

Community
  • 1
  • 1
Roman Nazarevych
  • 7,513
  • 4
  • 62
  • 67
  • How would you suggest I use this. When I need to get location or weather (10+ minute interval) should I register the listener, get the strength then unregister it? Or would I register the listener at app start and in the onSignalStrengthChanged() method I check the strength set a boolean variable which I then check in my Service's call to grab location and weather? Or am I on the complete wrong page here? – William Stewart Jan 17 '13 at 12:45
  • @WilliamStewart I believe you should decide which signal strength is sufficient for you, Then register PhoneStateListener on start of your app and in method onSignalStrengthChanged() check your signal strength. Then maybe as you said store in static boolean variable state of signal strength which you can check when you need to send HTTP request. – Roman Nazarevych Jan 17 '13 at 13:48
  • I've tested out the PhoneStateListener and it works fine, my only problem is that the onSignalStrengthsChanged method is called very frequently, for something I want to do every 10 minutes it seems quite inefficient to keep the listener registered. Is there a one-off signal grabbing method you are aware of? – William Stewart Jan 18 '13 at 19:08
  • @WilliamStewart you can limatate frequency which `onSignalStrengthsChanged` method is called by pausing `PhoneStateListener` with `telephony.listen(listener, PhoneStateListener.LISTEN_NONE);`. You can schedule it with `Timer` – Roman Nazarevych Jan 19 '13 at 13:53
1

You can check it but remember to handle, in any case, drop of connections.

In my apps I had some problems while writing files or stuff like that with an open connection, so just remember that a fail may always occur (for example switching from gprs to wifi).

If you can easily check the signal strength I would do both.

Give a look at the SignalStrength class (never used it btw). Here a nice explanation.

Enrichman
  • 11,157
  • 11
  • 67
  • 101
  • So you are suggesting doing a signal strength test, and catching any exceptions too just in case? – William Stewart Jan 17 '13 at 12:36
  • Yes, exactly. That's because you can lose connection also in case of a enough signal strength. You should do some test with your device and drop the connection while exchanging data (just turn off the 3G with a widget). – Enrichman Jan 17 '13 at 12:53