7

I've created a Service and grabs location data about the user so I can show my users nearby places. I want this Service to track by network in addition to GPS for 2 reasons: 1) GPS may not be enabled. 2) GPS takes quite a bit of time to lock on with my test phone.

When my Service executes the following line:

mNETEnabled = mLocationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);

I get the following error:

11-24 14:00:41.693: ERROR/LocationManagerService(51): isProviderEnabled got exception:
11-24 14:00:41.693: ERROR/LocationManagerService(51): java.lang.IllegalArgumentException: provider=network
11-24 14:00:41.693: ERROR/LocationManagerService(51):     at com.android.server.LocationManagerService._isProviderEnabledLocked(LocationManagerService.java:1385)
11-24 14:00:41.693: ERROR/LocationManagerService(51):     at com.android.server.LocationManagerService.isProviderEnabled(LocationManagerService.java:1359)

Why is this? Am I missing a permission from the Manifest? I have ACCESS_COARSE_LOCATION in my manifest.

Andrew
  • 20,756
  • 32
  • 99
  • 177

3 Answers3

14

are you running on emulator at api level 13 or greater? Android issue 19857

j03
  • 161
  • 1
  • 5
2

Use wireless networks setting in Security & location preference settings. Is it enabled?

techi.services
  • 8,473
  • 4
  • 39
  • 42
  • This happens on the phone, too; which is not connected to a wireless router. But yes, it is checked. – Andrew Nov 24 '10 at 20:23
  • Are you checking if the provider isProviderEnabled? – techi.services Nov 24 '10 at 20:29
  • Oh, whoops, I've made a mistake. The error occurs on the isProviderEnabled call. I'll update the main post to reflect this. – Andrew Nov 24 '10 at 20:30
  • Hehe, easily done. The API ref says that exception will occur if: if provider is null or doesn't exist – techi.services Nov 24 '10 at 20:33
  • I'm not sure what this means and why it wouldn't work on any actual devices. I have two Android phones (Samsung Moment, they're both the same model) and it doesn't work on either of them. I should also note that it USED to work. I just noticed that it no longer was and saw the error. – Andrew Nov 24 '10 at 20:35
  • Do they have cell access? ie not in flight mode, SIM card installed, connected to a provider. – techi.services Nov 24 '10 at 20:38
  • All I can suggest at the moment is to check the Android source and see what happens in _isProviderEnabledLocked. – techi.services Nov 24 '10 at 20:44
  • Oh, maybe you need Access Internet permission? TBH i put up with the GPS wait or stick the phone by the window to get a TTFF and then it doesn't take as long to get the satellites when I plug it back in to debug. – techi.services Nov 24 '10 at 20:50
  • It looks like, inside LocationManagerService, there is a HashMap of LocationProviderProxys, each with a String for a key. In _isProviderEnabledLocked it uses the string you passed in, in this case "network", to retrieve the LocationProviderProxy from the HashMap. It looks like the key "network" does not exist in that HashMap for some reason. At least this is what happens in the 1.6 API (I'm using 2.1) – Andrew Nov 24 '10 at 20:53
  • I have the INTERNET permission enabled as well – Andrew Nov 24 '10 at 20:53
  • I wonder if there is a bug in Android 2.1. I recently updated my SDK and both phones were recently updated as well (all had 2.1 previously, but it is my understanding that updates to 2.1 are still being made). – Andrew Nov 24 '10 at 20:58
  • I think the location info for stuff other than GPS doesn't come from location manager (in the Android source that is). I think it comes from Wifi or Telephony. Don't quote me though. This is what I am working on at present so this is why I am so interested in your problem. I only have a 1.6 phone ATM. – techi.services Nov 24 '10 at 21:03
  • Location tracking should work fine using the same mechanisms as GPS. You simple use a different parameter. There'a a few tutorials out there which are exactly the same as GPS except you pass in LocationManager.NETWORK_PROVIDER instead of LocationManager.GPS_PROVIDER. And like I said, at one point this was working. I'm not sure when it stopped working, but I recently updated the SDK, which included 2.1 updates. – Andrew Nov 24 '10 at 21:08
  • I was meaning at the underlaying level re location updates through wifi or cell, just because I see things like 11-24 20:00:56.265: DEBUG/GpsLocationProvider(94): state: DISCONNECTED apnName: null reason: null and 11-24 20:00:56.285: DEBUG/NetworkLocationProvider(94): onCellLocationChanged [20219,25367] in LogCat. Notice they all have PID of 94. – techi.services Nov 24 '10 at 21:16
  • So "gps" is the only provider in the LocationManager's HashMap of available providers. I should note that I AM testing on the emulator right now. I don't have either Android phone with me. – Andrew Nov 24 '10 at 21:17
  • ListenerTransport appears to be the link. Doesn't solve your problem though! – techi.services Nov 24 '10 at 21:33
  • Something is either wrong in the SDK or the emulator doesn't allow Network Providers (I don't have either of my Android devices on me at the moment). If you take your LocationManager and call getAllProviders, it will send you back a List of Strings, which is a list of all the Providers. This is a list of all Providers period, not just a list of accessible ones by your application. It only lists "gps" for me. So either there's a bug with adding the network provider to the LocationManager's HashMap of Providers or it simply doesn't do it for emulators. I don't see anything stopping it. – Andrew Nov 24 '10 at 21:41
  • F8 on the emulator enables/disables the cell data connection whether that will help get a network provider I don't know. Wifi definately doesn't work on the emulator. – techi.services Nov 24 '10 at 21:56
  • Interesting. Well I just tried toggling this and I receive the error no matter what. – Andrew Nov 24 '10 at 21:59
  • I did some Googling and apparently a lot of people are having this problem. It looks like it used to work until people started developing against 2.1+ – Andrew Nov 24 '10 at 22:01
  • Can you try an emulator running 1.6 or 2.0, assuming your code doesn't access API level 6 or above? – techi.services Nov 24 '10 at 22:07
  • I've just tested it on an emulator running 2.0 and I do not get the error – Andrew Nov 24 '10 at 22:22
  • Interesting... If somewhat annoying. – techi.services Nov 25 '10 at 08:15
0

Could be that the Moment doesn't have AGPS (which would be weird). But you can check with using LocationManager.getAllProviders() and see which ones come back. You could also just use LocationManager.getBestProvider (Criteria criteria, boolean enabledOnly)'.

CaseyB
  • 24,780
  • 14
  • 77
  • 112
  • My code used to work on my Moments; so I have to assume there's either a new bug in my code or a new bug in the SDK. I don't have my phones on me at the moment, but calling getAllProviders() on the emulator sends back a List only containing "gps". Whether or not this is normal, I don't know. – Andrew Nov 24 '10 at 21:57
  • I've tested on an emulator running 2.0 and I do not get the error (I was previously testing on 2.1) – Andrew Nov 24 '10 at 22:22