0

I have created the following class

package com.salvo.weather.android.geolocation;

import android.location.Location;
import android.os.Bundle;


import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.common.api.GoogleApiClient.ConnectionCallbacks;
import com.google.android.gms.common.api.GoogleApiClient.OnConnectionFailedListener;
import com.google.android.gms.location.LocationServices;
import com.salvo.weather.android.entity.CurrentGeolocationEntity;

/**
 * Created by mazzy on 30/05/15.
 */
public class CurrentGeolocation implements ConnectionCallbacks, OnConnectionFailedListener {

    private GoogleApiClient mGoogleApiClient;

    private CurrentGeolocationEntity mCurrentGeolocationEntity;

    public CurrentGeolocation() {
        buildGoogleApiClient();
        this.mCurrentGeolocationEntity = new CurrentGeolocationEntity();
    }

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

    public GoogleApiClient getmGoogleApiClient() {
        return mGoogleApiClient;
    }

    @Override
    public void onConnected(Bundle bundle) {
        this.mCurrentGeolocationEntity.setmLastLocation(LocationServices
                .FusedLocationApi
                .getLastLocation(this.mGoogleApiClient));

        Location mLastLocation = this.mCurrentGeolocationEntity.getmLastLocation();

        if (mLastLocation != null) {
            this.mCurrentGeolocationEntity.setmLongitude(mLastLocation.getLongitude());
            this.mCurrentGeolocationEntity.setmLatitude(mLastLocation.getLatitude());
        } else {
            // TODO: Add toast
        }
    }

    @Override
    public void onConnectionSuspended(int i) {
        // The connection to Google Play services was lost for some reason. We call connect() to
        // attempt to re-establish the connection.
    }

    @Override
    public void onConnectionFailed(ConnectionResult connectionResult) {
        // Refer to the javadoc for ConnectionResult to see what error codes might be returned in
        // onConnectionFailed.
    }
}

and I need to retrieve the context in order to pass it inside the GoogleApiClient. What do you say about? This is a good way? I'm a beginner in Android programming

Mazzy
  • 13,354
  • 43
  • 126
  • 207
  • Ok I have just found the answer http://stackoverflow.com/questions/17917968/get-context-in-non-activity-class-android – Mazzy May 30 '15 at 14:10
  • What I'm doing is it considered to be a good design pattern? – Mazzy May 30 '15 at 14:10

1 Answers1

2

Define a global variable :

 Context context = null;

Change Constructor to:

public CurrentGeolocation(Context context) { 
        buildGoogleApiClient(); 
        this.context = context;
        this.mCurrentGeolocationEntity = new CurrentGeolocationEntity();
    } 

Now in calling Activity :

  CurrentGeolocation obj = new CurrentGeolocation(this);

or Fragment :

 CurrentGeolocation obj = new CurrentGeolocation(getActivity());
Karan
  • 2,120
  • 15
  • 27
  • 1
    one and only! Passing Activity's context via constructor is highly recommended and a good practice. Accept it answer if it works for you. – Karan May 30 '15 at 14:17
  • 2
    Good practice as long as your class is instantiated from your Activity and dies with it when the Activity is destroyed. – ci_ May 30 '15 at 15:06