1

Hi I am integrating location service in my application. And for that I am using Google's fuse location service.

My implementation is as follows :

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

    protected void createLocationRequest() {
        mLocationRequest = new LocationRequest();
        mLocationRequest.setInterval(INTERVAL);
        mLocationRequest.setFastestInterval(FASTEST_INTERVAL);
        mLocationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY);
    }

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

    @Override
    public void onCreate() {

        //show error dialog if GoolglePlayServices not available
        if (isGooglePlayServicesAvailable()) {
            createLocationRequest();

            mGoogleApiClient = new GoogleApiClient.Builder(this)
                    .addApi(LocationServices.API)
                    .addConnectionCallbacks(this)
                    .addOnConnectionFailedListener(this)
                    .build();
             mGoogleApiClient.connect();
        }
    }

    @Override
    public void onConnected(Bundle bundle) {
        Log.d(TAG, "onConnected - isConnected ...............: " + mGoogleApiClient.isConnected());
        startLocationUpdates();
    }
}

What I want to achieve is when my application is running I want to change LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY to LocationRequest.HIGH_ACCURACY and when application is in background again change it to PRIORITY_BALANCED_POWER_ACCURACY. Same thing with update interval also. If application running it should update location frequently and when it is in background it should update after some time.

So in short this is a service, and I want to change fuse location setting while service is running without restarting the service.

I am perfectly getting application is in foreground or in background. Only problem is to switch priority.

Kunu
  • 5,078
  • 6
  • 33
  • 61

3 Answers3

0

you can chive that by interacting with your service via intents. and unregister/register the location request with the required accuracy.

Ohad Zadok
  • 3,452
  • 1
  • 22
  • 26
0

You can implement onPause() method in your activity to detect when user is leaving your app and do something.

onPause() is where you deal with the user leaving your activity. Most importantly, any changes made by the user should at this point be committed

In this method, communicate with your service from here (send info that app is not running anymore) To do this, you can use starService(intent)

If service is not running at this point, it will start service BUT if service is running, it will not restart, it will just call onStartCommand() in your service.

In service - you can get values from intent extras in your onStartCommand().

Caqtus
  • 489
  • 4
  • 4
  • What if user destroy the app from applications tray? – Kunu May 27 '15 at 14:08
  • onPause() is always called whether activity is paused or being destroyed. You should definetely read Activity Lifecycle docs here http://developer.android.com/reference/android/app/Activity.html#ActivityLifecycle – Caqtus May 27 '15 at 14:12
  • experimet by yourself. Implement all activity lifcycle related methods and add Log.e(); statements to them. Then pause or destroy your app. It will be obvious what is called first and last. – Caqtus May 27 '15 at 14:13
  • What I am saying is we are starting service in activity A. And if user is in activity B then it destroyed the app. It won't call onPause of activity A. – Kunu May 27 '15 at 14:19
  • Fragments! Use fragments and single activity. – Caqtus May 27 '15 at 14:30
  • Sometimes you need more than a one activity if it is a big project. – Kunu May 27 '15 at 14:31
  • Thete is always other way - (periodically check if activity is running from service). – Caqtus May 27 '15 at 14:35
  • But as a challenge for this solution, add one more variabl that will keep info - if user caused onPause() by opening other activity (by button or something else) or it was vaused by system - minimize kill. – Caqtus May 27 '15 at 14:37
0

I solved my problem by creating new GoogleApiCLient and new LocationRequest in onStartCommand().

When my app going to background I am starting my service again and checking there app is in background or not and changing my setting according to it.

@Override
public int onStartCommand(Intent intent, int flags, int startId) {

    if (isGooglePlayServicesAvailable()) {

        if (!isAppIsInBackground(this)) {

            //stopLocationUpdates();

            mLocationRequest.setInterval(FOREGROUND_INTERVAL);
            mLocationRequest.setFastestInterval(FOREGROUND_INTERVAL);
            mLocationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY);

        } else {
            mLocationRequest.setInterval(BACKGROUND_INTERVAL);
            mLocationRequest.setFastestInterval(BACKGROUND_INTERVAL);
            mLocationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY);

        }

        mGoogleApiClient = new GoogleApiClient.Builder(this)
                .addApi(LocationServices.API)
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .build();

        mGoogleApiClient.connect();
    }

    return super.onStartCommand(intent, flags, startId);
}
Kunu
  • 5,078
  • 6
  • 33
  • 61