0

i'm developing a running application with GPS. My problem is that when i'm not walking my GPS returns junk values and my distance counter is incremented, furthermore my activity sometimes is slowed. This is my "onCreate" code:

locManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locListener = new MyLocationListener();
HandlerThread thread = new HandlerThread("MyHandlerThread");
thread.start();

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

locManager.requestLocationUpdates(locManager.getBestProvider(criteria,false), 0, 0, locListener, thread.getLooper());

And this is my listener:

@Override
public void onLocationChanged(Location loc) {

    cityName = null;
    Geocoder gcd = new Geocoder(getBaseContext(), Locale.getDefault());
    List<Address> addresses;
    try {
        addresses = gcd.getFromLocation(loc.getLatitude(),
                loc.getLongitude(), 1);
        if (addresses.size() > 0) {
            System.out.println(addresses.get(0).getLocality());
            cityName = addresses.get(0).getLocality();
        }
    } catch (IOException e) {
        e.printStackTrace();
    }

    speed = loc.getSpeed() * 3.6; //in km/h

    lat = loc.getLatitude();
    lon = loc.getLongitude();

    if (starting_point) {
        last_long = lon;
        last_lat = lat;
        starting_point = false;
    } else {
        current_long = lon;
        current_lat = lat;

        Location locationA = new Location("Previous");
        locationA.setLatitude(last_lat);
        locationA.setLongitude(last_long);

        double distanceMeters = locationA.distanceTo(loc);

        double distanceKm = distanceMeters / 1000f;
        current_distance = current_distance + distanceKm;

        last_long = current_long;
        last_lat = current_lat;

    }

    //send to UI
    if (my_handler != null) {
        Message msg_ui = my_handler.obtainMessage(GPSActivity.UI_CODE);
        Bundle msg_ui_bundle = new Bundle();
        msg_ui_bundle.putDouble("LAT", lat);
        msg_ui_bundle.putDouble("LON", lon);
        msg_ui_bundle.putDouble("SPEED", speed);
        msg_ui_bundle.putString("CITY", cityName);
        msg_ui_bundle.putDouble("DIST", current_distance);
        msg_ui.setData(msg_ui_bundle);
        my_handler.sendMessage(msg_ui);
    }
}

And this is my UI updating handler:

private static Handler serviceHandler = new Handler(Looper.getMainLooper()) {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case UI_CODE:
                    lat= msg.getData().getDouble("LAT");
                    lon = msg.getData().getDouble("LON");
                    speed = msg.getData().getDouble("SPEED");
                    city_name = msg.getData().getString("CITY");
                    distance = msg.getData().getDouble("DIST");

                    tvlat.setText("Lat: "+lat );
                    tvlon.setText("Lon: "+lon);
                    tvspeed.setText("Speed: "+speed );
                    tvcity.setText("City: "+city_name);
                    tvdistance.setText("Distance: "+distance );

                    break;
            }
        }
    };
niranjan_b
  • 132
  • 5
RiccardoB
  • 141
  • 15
  • locManager.requestLocationUpdates(locManager.getBestProvider(criteria,false), 0, 0, locListener, thread.getLooper()); in this line you are requesting locations to be updated after 0 time and 0 m, try replacing it with some values like 50,10 – Sharad Chauhan May 17 '16 at 04:40
  • Ok my listener now bypasses junk values, but my application not responding, whats the problem ? I need to create a background service ? (i need to find location, speed, etc and display values on my UI, I use handler) – RiccardoB May 17 '16 at 11:10
  • yes, for that you need to create a service. In that service you can do all the background processing. – Sharad Chauhan May 17 '16 at 11:23
  • refer this one http://stackoverflow.com/questions/28535703/best-way-to-get-user-gps-location-in-background-in-android – Sharad Chauhan May 17 '16 at 11:25
  • And i start it with: GPS_BackgroundService.registerHandler(serviceHandler); intent = new Intent(this, GPS_BackgroundService.class); startService(intent); and I update UI with handler ? (handler is passed with registerHandler method) – RiccardoB May 17 '16 at 12:59

0 Answers0