我使用GooglePlay提供的服务来获取位置信息。 但在申请服务后,电池消失得如此之快。
使用google-play-service时,谁可以帮助我改善电池。
我的代码位置服务。
public class LocationService extends Service implements
GooglePlayServicesClient.ConnectionCallbacks,
GooglePlayServicesClient.OnConnectionFailedListener, LocationListener {
private static final String TAG = LocationService.class.getSimpleName();
public static final long TIME_LOC_PERIOD = 180000;
private LocationClient mLocationClient;
private static final LocationRequest REQUEST = LocationRequest.create()
.setInterval(180 * 1000) // 1 phut
.setFastestInterval(60 * 1000) // 15 seconds
.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
@Override
public void onCreate() {
Log.d(TAG, "Creating..");
mLocationClient = new LocationClient(this, this, this);
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.d(TAG, "Starting..");
if (!mLocationClient.isConnected() || !mLocationClient.isConnecting()) {
Log.d(TAG, "Connecting location client..");
mLocationClient.connect();
}
return START_STICKY;
}
@Override
public void onConnectionFailed(ConnectionResult result) {
Log.d(TAG, "Connection failed..");
stopSelf();
}
@Override
public void onConnected(Bundle bundle) {
mLocationClient.requestLocationUpdates(REQUEST, this);
}
@Override
public void onDisconnected() {
Log.d(TAG, "Disconnected..");
stopSelf();
}
@Override
public IBinder onBind(Intent arg0) {
throw new UnsupportedOperationException("Not yet implemented");
}
@Override
public void onLocationChanged(Location location) {
updatePosition(location.getLongitude(), location.getLatitude(), location);
}
@Override
public void onDestroy() {
Log.d(TAG, "Destroying..");
mLocationClient.removeLocationUpdates(this);
}
private void updatePosition(...)
}
并在活动中调用服务
if (!isLocationServiceRunning() && isGooglePlayServiceAvailable()) {
Intent service = new Intent(GlobalInfo.getInstance()
.getAppContext(), LocationService.class);
startService(service);
}
答案 0 :(得分:1)
诀窍就在你的代码中
private static final LocationRequest REQUEST = LocationRequest.create()
.setInterval(180 * 1000) // 1 phut
.setFastestInterval(60 * 1000) // 15 seconds
.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
将优先级更改为不高精度,同时将更新间隔更改为更长
看看这个链接。会给你一些想法。它最终是电池和精度之间的交易