I using service from GooglePlay to get location. But after apply service, battery gone so fast.
Who can help me improve battery when using google-play-service.
My code locationservice.
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(...)
}
And code call service in activity
if (!isLocationServiceRunning() && isGooglePlayServiceAvailable()) {
Intent service = new Intent(GlobalInfo.getInstance()
.getAppContext(), LocationService.class);
startService(service);
}
The trick is here in your code
private static final LocationRequest REQUEST = LocationRequest.create()
.setInterval(180 * 1000) // 1 phut
.setFastestInterval(60 * 1000) // 15 seconds
.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
change the priority to not high accuracy, also, update interval to longer
look at this link. will give you some ideas. Its a trade between battery and accuracy in the end