1

嗨,我正在我的应用程序中集成位置服务。为此,我正在使用 Google 的 fuse 定位服务。

我的实现如下:

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

我想要实现的是当我的应用程序运行时我想更改LocationRequest.PRIORITY_BALANCED_POWER_ACCURACYLocationRequest.HIGH_ACCURACY,当应用程序在后台再次更改为PRIORITY_BALANCED_POWER_ACCURACY. 更新间隔也一样。如果应用程序正在运行,它应该经常更新位置,当它在后台时,它应该在一段时间后更新。

所以简而言之,这是一项服务,我想在服务运行时更改保险丝位置设置而不重新启动服务。

我完全可以让应用程序处于前台或后台。唯一的问题是切换优先级。

4

3 回答 3

0

您可以通过意图与您的服务交互来实现这一点。并以所需的准确性取消注册/注册位置请求。

于 2015-05-26T12:34:01.820 回答
0

您可以onPause()在您的活动中实现方法来检测用户何时离开您的应用程序并执行某些操作。

onPause() 是处理用户离开活动的地方。最重要的是,此时用户所做的任何更改都应提交

在这种方法中,从这里与您的服务通信(发送应用程序不再运行的信息)为此,您可以使用starService(intent)

如果此时服务没有运行,它将启动服务但是如果服务正在运行,它不会重新启动,它只会调用onStartCommand()您的服务。

在服务中 - 您可以从onStartCommand().

于 2015-05-27T14:06:14.027 回答
0

我通过在 onStartCommand() 中创建新的 GoogleApiCLient 和新的 LocationRequest 解决了我的问题。

当我的应用程序进入后台时,我将再次启动我的服务并检查应用程序是否在后台并根据它更改我的设置。

@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);
}
于 2015-06-10T09:27:10.787 回答