1

I am fetching latitude and longitude of current location but onLocationChanged returns same location on mobile data I am using PRIORITY_BALANCED_POWER_ACCURACY mode.

Please tell me where is it misbehaving here's my code

public class Latlng_Service extends Service implements
        com.google.android.gms.location.LocationListener,
        GoogleApiClient.ConnectionCallbacks,
        GoogleApiClient.OnConnectionFailedListener {


    public static String STOP_USER_SERVICE = "STOP_USER_SERVICE";
    private static final long INTERVAL = 3000 * 10;
    private static final long FASTEST_INTERVAL = 1000 * 5;
    private WakeLock wl;


    int distance = 0;
    List<Address> address_list;
    String add_String,cat_id,module_id = "3";
    LocationRequest locationRequest;
    GoogleApiClient googleApiClient;
    Location mloLocation, lastlocation;
    String lat, lng, accuracy, name;
    String battery_level;
    String signal_strength;
    Handler  fivemiuntesHandler,thirtysecHandler;
    Start_latlng_service start_latlng_service;
    String login_id, gcm_id, url, response,responseArray[];
    boolean flag = false;

    List<LatLng> lisLatLngs = new ArrayList<LatLng>();
    // public static final String BROADCAST_ACTION =
    // "com.xsinfosol.smart_crm.attndance.location";
    Intent intent;

    String count;

    @SuppressWarnings("deprecation")
    @Override
    public void onCreate() {
        // TODO Auto-generated method stub
        super.onCreate();
        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy);

        createLocationRequest();
        flag = false;
        googleApiClient = new GoogleApiClient.Builder(this)
                .addApi(LocationServices.API).addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this).build();

        googleApiClient.connect();

        start_latlng_service = new Start_latlng_service();
        this.registerReceiver(this.broadcastReceiver, new IntentFilter(
                Intent.ACTION_BATTERY_CHANGED));
        this.registerReceiver(this.stop_service_at_time, new IntentFilter(
                STOP_USER_SERVICE));


    }

    @Override
    public IBinder onBind(Intent arg0) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        // TODO Auto-generated method stub



        PowerManager pm = (PowerManager)getSystemService(Context.POWER_SERVICE);
            wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP, "whatever");
            wl.acquire();

        return START_STICKY;
    }

    @Override
    public void onLocationChanged(Location location) {
        // TODO Auto-generated method stub
        mloLocation = location;
        lat = String.valueOf(location.getLatitude());
        lng = String.valueOf(location.getLongitude());
        accuracy = String.valueOf(location.getAccuracy());
        String currentDateTimeString = DateFormat.getDateTimeInstance().format(new Date());

        myConnection.logs_onlocation_insert(currentDateTimeString, String.valueOf(location.getLatitude()), String.valueOf(location.getLongitude()));



        AtendenceAutoManualFunction(location);

    }

    @Override
    public void onConnectionFailed(ConnectionResult arg0) {
        // TODO Auto-generated method stub

        System.out.println("Connection Falied " + arg0.getErrorCode());
    }

    @Override
    public void onConnected(Bundle arg0) {
        // TODO Auto-generated method stub
        startLocationUpdates();
        this.registerReceiver(this.broadcastReceiver, new IntentFilter(
                Intent.ACTION_BATTERY_CHANGED));
    }

    @Override
    public void onConnectionSuspended(int arg0) {
        // TODO Auto-generated method stub

    }

    protected void createLocationRequest() {
        locationRequest = new LocationRequest();

        locationRequest
                .setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY);

        locationRequest.setInterval(INTERVAL).setFastestInterval(
                FASTEST_INTERVAL);
        // .setSmallestDisplacement(10);
        // .setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY);

    }

    protected void startLocationUpdates() {
        @SuppressWarnings("unused")
        PendingResult<Status> pendingResult = LocationServices.FusedLocationApi
                .requestLocationUpdates(googleApiClient, locationRequest, this);
        Log.d(TAG, "Location update started ..............: ");

        if (fivemiuntesHandler == null) {

            Log.d(TAG, "Location update started five minutes ..............: ");
            fivemiuntesHandler = new Handler();

            fivemiuntesHandler.postDelayed(fiveminutes, 300000);

        }

        if (thirtysecHandler == null) {
            Log.d(TAG, "Location update started five minutes ..............: "); 
            thirtysecHandler = new Handler();
            thirtysecHandler.postDelayed(thirtySec, 0);

        }

    }

    protected void stopLocationUpdates() {
        if (googleApiClient.isConnected()) {
            LocationServices.FusedLocationApi.removeLocationUpdates(
                    googleApiClient, this);
        }
        Log.d(TAG, "Location update stopped .......................");
    }

    @SuppressWarnings("deprecation")
    @Override
    public void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();
        stopLocationUpdates();

        googleApiClient.disconnect();

        unregisterReceiver(broadcastReceiver);
        unregisterReceiver(stop_service_at_time);
        sharedPreferences = getSharedPreferences("user_detail",
                MODE_WORLD_WRITEABLE);
        login_id = sharedPreferences.getString("login_id", "");
        gcm_id = sharedPreferences.getString("reg_id", "");

        if (!login_id.contentEquals("") && !gcm_id.contentEquals("")
                && flag == false) {
            sendBroadcast(new Intent("CantKillMe"));

        } else {
            wl.release();
            stopSelf();
        }
    }
}
Jordi Castilla
  • 26,609
  • 8
  • 70
  • 109
Shivam Nagpal
  • 763
  • 2
  • 9
  • 21
  • You already asked the same question in [http://stackoverflow.com/questions/33007571/on-location-chnaged-method-some-time-returns-same-latitude-and-longitude-in-fuse](http://stackoverflow.com/questions/33007571/on-location-chnaged-method-some-time-returns-same-latitude-and-longitude-in-fuse). – Markus Kauppinen Oct 08 '15 at 08:50
  • @MarkusKauppinen Yeah but i didn't get any revert on that question that's why asked another one – Shivam Nagpal Oct 08 '15 at 08:52
  • Well, at least delete the older one then. And this might be just a feature of the Fused Location API. At least in low power consumption mode it just might not use the GPS at all and the cellular network or WiFi base station based positioning just isn't very accurate. You could search for older discussion [like this for example](http://stackoverflow.com/questions/30437144/fused-location-api-gives-highly-inaccurate-location-after-closing-google-maps) on Stackoverflow to find more info. I don't know how exactly the fused location works. – Markus Kauppinen Oct 08 '15 at 09:03

0 Answers0