3

I have following LocationService.java service class.

public class LocationService extends Service
        implements LocationListener,
        GoogleApiClient.ConnectionCallbacks,
        GoogleApiClient.OnConnectionFailedListener {
    private static final long INTERVAL = 1000 * 60;
    private static final long FASTEST_INTERVAL = 1000 * 5;
    Location mLastLocation;
    private GoogleApiClient mGoogleApiClient;
    private LocationRequest mLocationRequest;
    String lat, lon;


    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        mGoogleApiClient.connect();
        return START_STICKY;
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onConnected(Bundle bundle) {
        mLocationRequest = LocationRequest.create();
        mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
        mLocationRequest.setInterval(INTERVAL);
        mLocationRequest.setFastestInterval(FASTEST_INTERVAL);
        LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);

        mLastLocation = LocationServices.FusedLocationApi.getLastLocation(
                mGoogleApiClient);
        if (mLastLocation != null) {
            lat = String.valueOf(mLastLocation.getLatitude());
            lon = String.valueOf(mLastLocation.getLongitude());

            Util.WriteSharePrefrence(getApplicationContext(), "track_lat", "" + lat);
            Util.WriteSharePrefrence(getApplicationContext(), "track_lng", "" + lon);

        }
    }

    @Override
    public void onCreate() {
        int permissionCheck = ContextCompat.checkSelfPermission(LocationService.this, Manifest.permission.ACCESS_FINE_LOCATION);

        if (permissionCheck == PackageManager.PERMISSION_GRANTED) {
            //Execute location service call if user has explicitly granted ACCESS_FINE_LOCATION..
            buildGoogleApiClient();
        }
    }


    @Override
    public void onConnectionSuspended(int i) {
    }

    synchronized void buildGoogleApiClient() {
        mGoogleApiClient = new GoogleApiClient.Builder(this)
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .addApi(LocationServices.API)
                .build();
    }

    @Override
    public void onLocationChanged(Location location) {
        if (location != null) {
            Util.WriteSharePrefrence(getApplicationContext(), "track_lat", "" + location.getLatitude());
            Util.WriteSharePrefrence(getApplicationContext(), "track_lng", "" + location.getLongitude());
        }
    }

    @Override
    public void onConnectionFailed(ConnectionResult connectionResult) {
        buildGoogleApiClient();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }
}

In the above location service what I am doing is. 1. Set fastest interval to refresh the location every 5 sec. 2. Once location availabe onLocationChanged() will be take place. Inside onLocationChanged() method I am writing current latitude and longitude in the shared preference. 3. This service will be running 24 hours in the background to get current location.

I have Mentioned both location condition in my manifest like below:

<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

My Issue is :

Some times onLocationChanged() method taking wrong latitude longitude for example : user device in India and it taking USA address. The device version is Xolo - 4.2.1.(India)

Is it device specific issue? Or something I need to change in my code? What should I do to make my location service better ?

Himanshu
  • 861
  • 10
  • 25
  • Why are you not using the regular location providers? Like GPS or Network providers – Mohammad Haidar Aug 30 '16 at 07:25
  • @Haidar I used GPSTracker.java Class for getting current location but I felt it not frequently update the current location location ...so I used it. – Himanshu Aug 30 '16 at 08:00

1 Answers1

0

Use Network or GPS providers or Passive if you want

public void Request_LocationUpdate(String type)
{
    if (ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED)
    {
        ActivityCompat.requestPermissions(getActivity(),new String[]{Manifest.permission.ACCESS_FINE_LOCATION,Manifest.permission.ACCESS_COARSE_LOCATION}, 12);
    }
    else
    {
      LocationManager  locationManager = (LocationManager) getActivity().getSystemService(Context.LOCATION_SERVICE);

        switch (type)
        {
            case "Passive":
                locationManager.requestLocationUpdates(LocationManager.PASSIVE_PROVIDER,10000,10,locationListener);
                break;

            case "GPS":
                locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,10000,10,locationListener);
                break;

            case "Network":
                locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,10000,10,locationListener);
                break;
        }
    }


}
Mohammad Haidar
  • 1,109
  • 1
  • 16
  • 35
  • Are you sure by adding by adding PASSIVE_PROVIDER the problem solved ? – Himanshu Aug 30 '16 at 08:01
  • This method has 3 options either GPS provider which is the most accurate provider but it needs an open air which means not indoor and the Second one is Network provider which gets your location by your network as long as you have internet connection and if accuracy is not an issue for u this is the suitable one. Passive provider means that you want to get the location without requesting it if another app is already retrieving the location so you will have that location choose one as u see it suitable and use it – Mohammad Haidar Aug 30 '16 at 08:09
  • and you can control the update time and distance frequency by this stuff locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,time(ms),distance(meters),locationListener); – Mohammad Haidar Aug 30 '16 at 08:11
  • thank for your great explinations but I am already using fusedlocation provider ....Where can I set the above your method in my code ? Can you please guide me...? – Himanshu Aug 30 '16 at 10:09
  • just call it in the OnStartCommand() method and instead of "locationListener" write "this" since you are implementing location listener. If you want GPS provider pass "GPS" or Network provider pass "Network" – Mohammad Haidar Aug 30 '16 at 11:24
  • it is still not going good.....i think i need to find our the solution according to FusedLocation API.... – Himanshu Aug 31 '16 at 11:09