【发布时间】:2016-07-22 11:03:04
【问题描述】:
以下是我的位置服务类,如果设备更改位置,它会继续更新位置。
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()");
}
}
在 Manifest 中,我已将这项服务声明如下:
<service
android:name=".util.LocationService"
android:enabled="true" />
每当用户在他/她的设备上启动应用程序时,我都会启动它
if (!isMyServiceRunning(LocationService.class, context)) {
context.startService(new Intent(context, LocationService.class));
}
注意:我不会从任何地方停止服务。
问题:
- 所有大品牌设备的位置更新,如 google nexus 5、gionee、One+、三星....但在某些设备位置不更新时,如 Honor、xolo....
- 每当设备在户外或行驶中时,应调用
onLocationChanged()方法,并在WriteSharePrefrence()中更改新更新的经纬度,但在某些设备中更新不频繁。 - 我的服务有什么问题吗?我的应用中没有任何耗电问题。
- 有什么东西阻止了我的服务?
- 如何继续在后台运行位置更新服务并频繁地从用户设备获取准确的位置更新?
【问题讨论】:
-
onDestroy 被调用了吗?如果设备内存不足,操作系统可能会终止您的服务。尝试添加标志-Service.START_STICKY
-
@X3Btel onDestrye 没有打电话。 Service.START_STICKY 发生了什么?
-
Start sticky 会在系统终止服务时重新启动服务。但是如果 onDestroy 没有被调用,它应该没有帮助。其他设备也开启了gps,精度高吗?
-
是的,所有设备都有相同的代码和逻辑。
标签: android location fusedlocationproviderapi