31 Dec 2010, 21:50
Generic-user-small

Dave Murray (28 posts)

Criteria criteria = new Criteria(); sets no preferences and always selects network as best. I’ve tried all sorts of things that all give errors to set a preference for fine so it will use the GPS. How do you do it? The Android Developers reference gives not example that I can find.

31 Dec 2010, 22:18
Generic-user-small

Dave Murray (28 posts)

I got it to accept: Criteria ACCURACY_FINE = new Criteria(); best = mgr.getBestProvider(ACCURACY_FINE, true); log(”\nBest provider is: ” + best);
without giving an error but it still gives network as best

I must seriously not understand this lesson.

03 Jan 2011, 01:57
Burnette_ed_pragsmall

Ed Burnette (1316 posts)

Here’s an example that should get you the GPS location. Note it make take a minute or two for the GPS to lock on.

  LocationManager lm = null;
  boolean enabledOnly = false;

  criteria = new Criteria();
  criteria.setAccuracy(Criteria.ACCURACY_FINE);

  lm = (LocationManager) ctx.getSystemService(Context.LOCATION_SERVICE);
  bestProvider = lm.getBestProvider(criteria, enabledOnly);

If this doesn’t do what you want, you might have to use the getProviders() or getAllProviders() method and do extra checks in your code to find the right provider according to your custom criteria.

For complex use cases you might find this helpful:

03 Jan 2011, 21:35
Generic-user-small

Dave Murray (28 posts)

Thank you. I haven’t figured out how to get past the errors yet, but I’m working on it.


      Criteria criteria = new Criteria();
  //    best = mgr.getBestProvider(criteria, true);
      LocationManager lm = null;
      boolean enabledOnly = false;

      criteria = new Criteria();
      criteria.setAccuracy(Criteria.ACCURACY_FINE);

      lm = (LocationManager) ctx.getSystemService(Context.LOCATION_SERVICE);
      best = lm.getBestProvider(criteria, enabledOnly);  // changed to best for example

      log("\nBest provider is: " + best);

2 Errors:

  Context cannot be resolved  LocationTest.java /LocationTest/src/org/example/locationtest  line 45  Java Problem

  ctx cannot be resolved          LocationTest.java /LocationTest/src/org/example/locationtest  line 45  Java Problem

I’m not a Java programmer so I must learn both Android and Java unfortunately.

03 Jan 2011, 22:46
Burnette_ed_pragsmall

Ed Burnette (1316 posts)

Try this:

   public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.main);

      mgr = (LocationManager) getSystemService(LOCATION_SERVICE);
      output = (TextView) findViewById(R.id.output);

      log("Location providers:");
      dumpProviders();

      Criteria criteria = new Criteria();
      criteria.setAccuracy(Criteria.ACCURACY_FINE);

      best = mgr.getBestProvider(criteria, true);
      log("\nBest provider is: " + best);

      log("\nLocations (starting with last known):");
      Location location = mgr.getLastKnownLocation(best);
      dumpLocation(location);
   }
04 Jan 2011, 02:39
Generic-user-small

Dave Murray (28 posts)

Very cool! Thank you. Started with last known report. If GPS off: best=network, if GPS on: best=GPS updated on changes as accuracy increased over time and degraded as I walked under a roof. Reported bearing when I walked. That’s what I hoped to see.

04 Jan 2011, 16:40
Burnette_ed_pragsmall

Ed Burnette (1316 posts)

Great. By the way this post talks about resources for beginning Java programmers:

And if you prefer C over Java, check out the Android NDK (Native Development Kit).

05 Jan 2011, 12:56
Generic-user-small

Dave Murray (28 posts)

Thanks again!

06 Jan 2011, 15:33
Burnette_ed_pragsmall

Ed Burnette (1316 posts)

No problem.

  You must be logged in to comment