I am trying to get the highest possible location accuracy, which I have learned is from GPS provider. I only need to get the location once, so I am not concerned about the effect on battery life. In my code, which is functional, the location seems to alternate between totally inaccurate (1+km) to very accurate (within 10m). Throughout the testing of my app, I am enabling/disabling location on my phone, turning on/off wifi, turning on/off data. This is just to experiment and see how each affects the accuracy and the app in general.
The following code is my onCreate method from the location activity:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.map_layout);
mMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map_fragment))
.getMap();
mMap.setMyLocationEnabled(true);
locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
Criteria criteria = new Criteria();
//criteria.setAccuracy(Criteria.ACCURACY_HIGH);
provider = locationManager.getBestProvider(criteria, true);
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
// Create the LocationRequest object
mLocationRequest = LocationRequest.create()
.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY)
.setInterval(2 * 1000) // 10 seconds, in milliseconds
.setFastestInterval(1 * 1000); // 1 second, in milliseconds
}
My app crashes however when I add the criteria.setAccuracy(Criteria.ACCURACY_HIGH); line.
What is going on?
EDIT: this is the logcat error:
E/AndroidRuntime: FATAL EXCEPTION: main
E/AndroidRuntime: Process: com.locationget, PID: 16663
E/AndroidRuntime: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.locationget/com.locationget.MapActivity}: java.lang.IllegalArgumentException: accuracy=3
E/AndroidRuntime: at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2212)
E/AndroidRuntime: at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2271
E/AndroidRuntime: at android.app.ActivityThread.access$800(ActivityThread.java:144)
E/AndroidRuntime: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1205)
E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:102)
E/AndroidRuntime: at android.os.Looper.loop(Looper.java:136)
E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:5146)
E/AndroidRuntime: at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime: at java.lang.reflect.Method.invoke(Method.java:515)
E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:732)
E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:566)
E/AndroidRuntime: at dalvik.system.NativeStart.main(Native Method)
E/AndroidRuntime: Caused by: java.lang.IllegalArgumentException: accuracy=3
E/AndroidRuntime: at android.location.Criteria.setAccuracy(Criteria.java:223)
E/AndroidRuntime: at com.getlocation.MapActivity.onCreate(MapActivity.java:47)
E/AndroidRuntime: at android.app.Activity.performCreate(Activity.java:5231)
E/AndroidRuntime: at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
E/AndroidRuntime: at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2169)
E/AndroidRuntime: at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2271
E/AndroidRuntime: at android.app.ActivityThread.access$800(ActivityThread.java:144)
E/AndroidRuntime: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1205)
E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:102)
E/AndroidRuntime: at android.os.Looper.loop(Looper.java:136)
E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:5146)
E/AndroidRuntime: at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime: at java.lang.reflect.Method.invoke(Method.java:515)
E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:732)
E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:566)
E/AndroidRuntime: at dalvik.system.NativeStart.main(Native Method)
I'm relatively new to android, not really sure what is the relevant part.
This line is the real error:
E/AndroidRuntime: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.locationget/com.locationget.MapActivity}:java.lang.IllegalArgumentException: accuracy=3
This is what I found after some searchs:
The android Criteria has two separate groups of accuracy constants. ACCURACY_FINE and ACCURACY_COARSE are for general location, while ACCURACY_LOW, ACCURACY_MEDIUM, and ACCURACY_HIGH "may be used for horizontal, altitude, speed or bearing accuracy."
So in your case, don't use ACCURACY_HIGH or you'll get an error. Use ACCURACY_COARSE or ACCURACY_FINE instead.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With