OnLocationChanged is not called sometimes using googleapiclient

196 views Asked by At

As I'm using googlemap in my location, every two mins I want to update the location, sometimes onlocationchanged method is not calling in my application. Please help me to fix my problem.

coding:

public class MyLocationService1 extends Service implements GoogleApiClient.ConnectionCallbacks,GoogleApiClient.OnConnectionFailedListener, LocationListener {

    LocationManager locationManager ;
    SharedPreferences sharedPreferences,startLesson_preference;
    SharedPreferences.Editor editor;

    String gs_var_userid,gs_var_usetoken;
    private GoogleApiClient mGoogleApiClient = null;

    private static final LocationRequest REQUEST = LocationRequest.create()
            .setInterval(5000)
            .setFastestInterval(5000)
            .setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);

    public static boolean isGPSEnabled = false;
    public static boolean isNetworkEnabled = false;

    private final int UPDATE_INTERVAL = 5000; // 5 sec
    private final int FASTEST_INTERVAL = 5000; // 1 sec
    private final int DISPLACEMENT = 0; // 10 meters

    LocationRequest mLocationRequest;
    public void createLocationRequest() {
        mLocationRequest = LocationRequest.create();
        mLocationRequest.setInterval(UPDATE_INTERVAL);
        mLocationRequest.setFastestInterval(FASTEST_INTERVAL);
        mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
        mLocationRequest.setSmallestDisplacement(DISPLACEMENT);
    }
    String provider;
    //to check gps is enabled or not
    @Override
    public void onCreate() {
        super.onCreate();
 locationManager = (LocationManager) getApplicationContext().getSystemService(LOCATION_SERVICE);
        isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
        isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
        LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        Criteria criteria = new Criteria();
        provider = locationManager.getBestProvider(criteria, false);

        Log.i("FG","onstartcommand");
        mGoogleApiClient = new GoogleApiClient.Builder(getApplicationContext())
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .addApi(LocationServices.API)
                .build();


        mGoogleApiClient.connect();

        createLocationRequest();
    }
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
 return START_STICKY;
    }
    @Override
    public void onLowMemory() {
        super.onLowMemory();

    }

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

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



    private synchronized void buildGoogleApiClient() {
        try {
            mGoogleApiClient = new GoogleApiClient.Builder(getApplicationContext())
                    .addConnectionCallbacks(this)
                    .addOnConnectionFailedListener(this)
                    .addApi(LocationServices.API)
                    .build();
            mGoogleApiClient.connect();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onConnected(@Nullable Bundle bundle) {
        try {

            Log.i("FG","----onConnected----service1");
            if (ActivityCompat.checkSelfPermission(getApplicationContext(), android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(getApplicationContext(), android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {

                return;
            }
            if (mGoogleApiClient != null) {
                mGoogleApiClient.connect();
                LocationRequest locationRequest = new LocationRequest();
                locationRequest.setInterval(3000);
                locationRequest.setFastestInterval(3000);
                locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);

                LocationServices.FusedLocationApi
                        .requestLocationUpdates(mGoogleApiClient, locationRequest, new LocationListener() {
                            @Override
                            public void onLocationChanged(Location location) {
                                Log.i("AA","--location : "+location.getLatitude()+","+location.getLongitude());

                            }

                        });


                Location mLastLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
                Log.i("AA","--LAT & LONG : "+mLastLocation.getLatitude()+","+mLastLocation.getLongitude());

            }
            else
            {
                buildGoogleApiClient();
                LocationServices.FusedLocationApi.requestLocationUpdates(
                        mGoogleApiClient,
                        REQUEST,
                        this);  // LocationListener
            }





        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onConnectionSuspended(int i) {

    }

    // to get current latitude and longitude
    // storing the updated latitde and longitude when user moving and reach destination
    @Override
    public void onLocationChanged(Location location) {
        try {
     Log.i("MS","service class********"+location);


        } catch (Exception e) {
            e.printStackTrace();
        }


    }


    @Override
    public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {

    }

Note:Onconnected method is executing and lastknownlocation also displaying but onlocationchanged method is not executing.its very important in my project to calculate distance for every 2 mins.Please help me

0

There are 0 answers