Home » Android » Location updates in separate class

Location updates in separate class

Posted by: admin November 1, 2017 Leave a comment

Questions:

I am building an android app which handles location updates separate from MainActivity.java class

Here is my MainActvity.java snippet:

LocationHandler mLocationHandler;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mLocationHandler = new LocationHandler(MainActivity.this);


    }

I create an instance in MainActivity.java and that is it. All of the work is done inside a separate class named LocationHandler.java:

public class LocationHandler implements
        GoogleApiClient.ConnectionCallbacks,
        GoogleApiClient.OnConnectionFailedListener,
        LocationListener {

    //Declare location service variables
    public GoogleApiClient mGoogleApiClient;
    public LocationRequest mLocationRequest;
    public OnLocationUpdateListener mOnLocationUpdateListener;

    //Context variable for activity context
    public Context context;



    //Constructor for location services
    public LocationHandler(Context context) {
        this.context = context;
        //Get the Google API Client
        buildGoogleApiClient();
        //Get the Location Request
        buildLocationRequest();
    }



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

    public void startLocationUpdates(Context context){
        if (ActivityCompat.checkSelfPermission(context,
                android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED
                && ActivityCompat.checkSelfPermission(context,
                android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            return;
        }

        LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient,mLocationRequest, (com.google.android.gms.location.LocationListener) this);// 

    }

    private void stopLocationUpdates(Context context){
        LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, (com.google.android.gms.location.LocationListener) this);
    }


    public void buildLocationRequest(){
        mLocationRequest = new LocationRequest();
        mLocationRequest.setInterval(10000);
        mLocationRequest.setFastestInterval(5000);
        mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
    }

    @Override
    public void onConnected(@Nullable Bundle bundle) {
        startLocationUpdates(context);
    }



    @Override
    public void onLocationChanged(Location location) {
        if(mGoogleApiClient.isConnected() && mOnLocationUpdateListener != null){
            mOnLocationUpdateListener.onLocationChange(location);
        }
    }



    public interface OnLocationUpdateListener {
        void onLocationChange(Location location);

    }



}

Since I have no idea how to implement location updates in separate class, I have read some questions on StackOverflow and changed my code accordingly. Regardless of that, I still cannot get my head around how to get it working.

UDPATED – Deleted some empty methods on this question

Answers:

Value of mOnLocationUpdateListener in LocationHandler is not set. There fore you don’t get any events.

@Override
    public void onLocationChanged(Location location) {
        if(mGoogleApiClient.isConnected() && mOnLocationUpdateListener != null){
            mOnLocationUpdateListener.onLocationChange(location);
        }
    }

This specific method is responsible for location call back. However mOnLocationUpdateListener == null , there for you don’t get any callbacks. OR mGoogleApiClient.isConnected() returns false.