【问题标题】:Change fuse location setting while service is running在服务运行时更改保险丝位置设置
【发布时间】:2015-08-07 17:04:33
【问题描述】:

您好,我正在我的应用程序中集成定位服务。为此,我正在使用 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_ACCURACY 更改为LocationRequest.HIGH_ACCURACY,当应用程序再次处于后台时将其更改为PRIORITY_BALANCED_POWER_ACCURACY。更新间隔也一样。如果应用程序正在运行,它应该经常更新位置,当它在后台时,它应该在一段时间后更新。

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

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

【问题讨论】:

    标签: android service android-location location-services android-fusedlocation


    【解决方案1】:

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

    【讨论】:

    • 您能详细说明一下吗?
    【解决方案2】:

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

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

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

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

    在服务中 - 您可以从 onStartCommand() 中的 Intent Extras 中获取值。

    【讨论】:

    • 如果用户从应用程序托盘中销毁应用程序怎么办?
    • onPause() 无论活动是暂停还是被销毁,都会被调用。你应该在这里阅读活动生命周期文档developer.android.com/reference/android/app/…
    • 亲身体验。实现所有活动生命周期相关的方法并添加 Log.e();对他们的陈述。然后暂停或销毁您的应用程序。什么叫 first 和 last 很明显。
    • 我的意思是我们正在活动 A 中启动服务。如果用户在活动 B 中,那么它会破坏应用程序。它不会调用活动 A 的 onPause。
    • 片段!使用片段和单个活动。
    【解决方案3】:

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

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多