Below is my Location service class, it continue update the location if device changed location.
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()");
}
}
In Manifest i have declared this service as below:
<service
android:name=".util.LocationService"
android:enabled="true" />
I am starting this every time when user launch the app in his/her device
if (!isMyServiceRunning(LocationService.class, context)) {
context.startService(new Intent(context, LocationService.class));
}
Note: I am not stopping service from anywhere.
Issues:
- Location update in all big brand devices Like google nexus 5, gionee, One+, Samsung....but in some device location not updating when internet or wifi not available like honor, xolo....
- Every time when device in outdoor or on driving
onLocationChanged()
method should be called and inWriteSharePrefrence()
new updated latitude and longitude should be changed but it is not updating frequently in some of devices. - Is there anything wrong in my service ? There is no battery draining matter in my app.
- Is there anything which is stopping my service ?
- How to continue running location update service in background and getting accurate location update from user device frequently ?