esantiago
esantiago

Reputation: 161

Android:best practice to implement LocationManager

I am making an application sends the coordinates of latitude and longitude, so I'm extending a service and implemented LocationManager. This is part of my code:

public class LocationServiceTracking extends Service implements LocationListener{
....
 @Override
    public void onCreate() {
        super.onCreate();
        mLocationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

            if (isGPSEnabled && provider.equals(LocationManager.GPS_PROVIDER)) {
                try {
                    mLocationManager.requestLocationUpdates(3000, 0, criteria, this, null);
                    Log.d("GPS Enabled", "GPS Enabled");
                } catch (SecurityException se) {
                    se.printStackTrace();
                    Crashlytics.logException(se);
                }
            }
    }


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

}

My question is ¿What would be the best practice to do this if you do it in onCreate () as is now or change it to onStartCommand ()?

Upvotes: 2

Views: 991

Answers (4)

Janki Gadhiya
Janki Gadhiya

Reputation: 4510

It should be in onStartCommand()

Reason : onCreate() will be called whenever the service is created But onStartCommand() will be called when you manually start the service by calling obj.startService().

So if you are putting that code in onCreate() you will start receiving updates BEFORE you start the service which is not a good practice. The code written in Service should be enabled only when it is started or bound.

For e.g.

MyService serviceObj = new MyService(); // this will call onCreate().

serviceObj.startService(); // this will call `onStartCommand()`.

serviceObj.bindService(); // this will call onBind(). (Not your case).

Hope this helps.

Upvotes: 0

eoinzy
eoinzy

Reputation: 2242

Create a field in your class called mGoogleApiClient.

Then in onCreate do this:

// Create an instance of GoogleAPIClient.
if (mGoogleApiClient == null) {
    mGoogleApiClient = new GoogleApiClient.Builder(getActivity())
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .addApi(LocationServices.API)
            .build();
}

When the class loads and stops, handle the lifecycle of this field:

public void onStart() {
    mGoogleApiClient.connect();
    super.onStart();
}

public void onStop() {
    mGoogleApiClient.disconnect();
    super.onStop();
}

Now implement the folliwing interfaces:

GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener

and add their methods:

@Override
public void onConnected(Bundle connectionHint) {
    Log.d(TAG, "Google Maps API::onConnected");
    //For Marshmallow 6.0, make sure to check Permissions
    mLastLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
}

@Override
public void onConnectionSuspended(int i) {
    Log.d(TAG, "Google Maps API::onConnectionSuspended");
}

@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
    Log.d(TAG, "Google Maps API::onConnectionFailed");
}

Upvotes: 0

3mpty
3mpty

Reputation: 1385

Use Fused location api for brevity.

About your question - it's better to do it in onCreate() because onStartCommand() will be called as many times as you triggered start service and onCreate only when service is actually created.

Calling start service

Service lifecycle

Upvotes: 1

ashwinbhy
ashwinbhy

Reputation: 600

Read about FusedLocationProvider api for android.

It will handle most of the complications.If gps is not enabled it takes the co-ordinates from the network provider etc. Read it here:

https://developers.google.com/android/reference/com/google/android/gms/location/FusedLocationProviderApi

Upvotes: 1

Related Questions