问题描述
我在我的应用程序整合位置服务。而对于我使用谷歌的保险丝位置服务。
Hi I am integrating location service in my application. And for that I am using Google's fuse location service.
我的实现如下:
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
。与更新间隔也同样的事情。如果应用程序正在运行,应该经常更新的位置和当它在后台应在一段时间后进行更新。
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.
推荐答案
我通过创造新的GoogleApiCLient和新LocationRequest解决我的问题
在onStartCommand()。
I solved my problem by creating new GoogleApiCLient and new LocationRequestin 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);
}
这篇关于而服务运行更改保险丝的位置设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!