Change fuse location setting while service is running

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-23 03:01:12

问题


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.


回答1:


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




回答2:


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().




回答3:


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);
}


来源:https://stackoverflow.com/questions/30451503/change-fuse-location-setting-while-service-is-running

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!