0
以下是我的位置服務類,如果設備更改位置,它會繼續更新位置。融合位置提供商GoogleApiClient有時不會更新後臺服務中的位置
public class LocationService extends Service
implements LocationListener,
GoogleApiClient.ConnectionCallbacks,
GoogleApiClient.OnConnectionFailedListener {
private static final long INTERVAL = 1000 * 60;
private static final long FASTEST_INTERVAL = 1000 * 5;
Location mLastLocation;
private GoogleApiClient mGoogleApiClient;
private LocationRequest mLocationRequest;
String lat, lon;
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.d("", "********************** onStartCommand()");
int permissionCheck = ContextCompat.checkSelfPermission(LocationService.this, Manifest.permission.ACCESS_FINE_LOCATION);
if (permissionCheck == PackageManager.PERMISSION_GRANTED) {
//Execute location service call if user has explicitly granted ACCESS_FINE_LOCATION..
buildGoogleApiClient();
}
return super.onStartCommand(intent, flags, startId);
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
Log.d("", "********************** onBind()");
return null;
}
@Override
public void onConnected(Bundle bundle) {
Log.d("", "********************** onConnected()");
// Util.appendLog("Location Service onConnected()");
mLocationRequest = LocationRequest.create();
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
mLocationRequest.setInterval(INTERVAL);
mLocationRequest.setFastestInterval(FASTEST_INTERVAL);
LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);
mLastLocation = LocationServices.FusedLocationApi.getLastLocation(
mGoogleApiClient);
if (mLastLocation != null) {
lat = String.valueOf(mLastLocation.getLatitude());
lon = String.valueOf(mLastLocation.getLongitude());
Util.WriteSharePrefrence(getApplicationContext(), "track_lat", "" + lat);
Util.WriteSharePrefrence(getApplicationContext(), "track_lng", "" + lon);
Util.appendLog("Location Service onConnected(): " + " Latitude: " + lat + " Longitude: " + lon);
}
}
@Override
public void onConnectionSuspended(int i) {
Util.appendLog("Location Service onConnectionSuspended()");
}
synchronized void buildGoogleApiClient() {
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
mGoogleApiClient.connect();
}
@Override
public void onLocationChanged(Location location) {
Log.d("", "********************** onLocationChanged()");
// Util.appendLog("Location updated Latitude: " + location.getLatitude() + " longitude: " + location.getLongitude());
// Toast.makeText(LocationService.this, "Latitude: " + location.getLatitude() + " Longitude: " + location.getLongitude(), Toast.LENGTH_SHORT).show();
if (location != null) {
Util.WriteSharePrefrence(getApplicationContext(), "track_lat", "" + location.getLatitude());
Util.WriteSharePrefrence(getApplicationContext(), "track_lng", "" + location.getLongitude());
}
// stopSelf();
}
@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
Util.appendLog("Location Service onConnectionFailed()");
buildGoogleApiClient();
}
@Override
public void onDestroy() {
super.onDestroy();
Util.appendLog("Location servcie destroyed()");
}
}
清單中我已經聲明如下這種服務:
<service
android:name=".util.LocationService"
android:enabled="true" />
我開始每次都是這樣,當用戶啓動應用程序在他/她的設備
if (!isMyServiceRunning(LocationService.class, context)) {
context.startService(new Intent(context, LocationService.class));
}
注:我我沒有停止從任何地方的服務。
問題:在所有的大品牌設備,如谷歌Nexus 5,金立,一+,三星....但在某些設備位置不更新時網絡或無線網絡不可用類似的榮譽,XOLO
- 位置更新...
- 每次當室外或駕車
onLocationChanged()
方法應該被調用,並在WriteSharePrefrence()
新的更新的經度和緯度應該改變,但它不是在一些設備頻繁更新。 - 我的服務有什麼問題嗎?我的應用中沒有電池耗盡事項。
- 有什麼是停止我的服務?
- 如何在後臺繼續運行位置更新服務並經常從用戶設備獲取準確的位置更新?
是onDestroy被調用?如果設備內存不足,操作系統可能會導致您的服務中斷。嘗試添加標誌 - Service.START_STICKY – X3Btel
@ X3Btel onDestrye不能呼叫。 Service.START_STICKY發生了什麼? – himCream
如果系統殺死它,啓動粘性重啓服務。但是,如果onDestroy沒有被調用,它應該沒有幫助。其他設備是否也打開gps,具有高精度? – X3Btel