I am building an app where I am using service for locations. I am starting that service in onCreate on MainActivity, but if GPS is off my service will not look for latitude and longitude. Even if I turn on GPS after that, my service will not looking lat and long. Only when I turn on GPS before starting service(and MainActivity) then service will work fine and it will look for locations. What Should I change to set that my service don't bug when I turn on GPS after some time that I spent in app? This is my location service:
public class LocationUpdateService extends Service implements LocationListener, GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener {
protected static final String TAG = "LocationUpdateService";
public static final long UPDATE_INTERVAL_IN_MILLISECONDS = 30000;
public static final long FASTEST_UPDATE_INTERVAL_IN_MILLISECONDS = UPDATE_INTERVAL_IN_MILLISECONDS / 2;
public static Boolean mRequestingLocationUpdates;
protected String mLastUpdateTime;
protected String timeWhenStartLocationUpdates;
protected String timeWhenStopLocationUpdates;
protected String timeWhenServiceDestroyed;
protected String connectionFailed;
protected GoogleApiClient mGoogleApiClient;
protected LocationRequest mLocationRequest;
protected Location mCurrentLocation;
protected String timeWhenGetInGetLatLong;
protected String report_time;
protected String timeFromElse;
public static boolean isEnded = false;
Calendar calendar;
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onConnected(@Nullable Bundle bundle) {
startLocationUpdates();
}
@Override
public void onConnectionSuspended(int i) {
Log.i(TAG, "Connection suspended==");
mGoogleApiClient.connect();
}
@Override
public void onLocationChanged(Location location) {
mCurrentLocation = location;
mLastUpdateTime = DateFormat.getTimeInstance().format(new Date());
Log.d("mCurrentLocation", "Location is: " + location);
getLatLong();
SharedPreferences sharedPreferences = getApplicationContext().getSharedPreferences("user_location", MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString("TimeWhenCameToOnLocationChanged", mLastUpdateTime);
editor.apply();
Log.d("onLocationchanged", "OnLocationChangedInServiceLocation");
Log.d("Time ", "Time of lat and long from Service: " + mLastUpdateTime);
}
@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
connectionFailed = DateFormat.getTimeInstance().format(new Date());
SharedPreferences sharedPreferences = getApplicationContext().getSharedPreferences("user_location", MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString("connectionFailed", connectionFailed);
editor.apply();
Log.i(TAG, "Connection failed: ConnectionResult.getErrorCode() = " + connectionResult.getErrorCode());
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.d("LOC", "Service init...");
return Service.START_STICKY;
}
protected synchronized void buildGoogleApiClient() {
Log.i(TAG, "Building GoogleApiClient===");
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addApi(LocationServices.API)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
createLocationRequest();
}
public void getLatLong() {
timeWhenGetInGetLatLong = DateFormat.getTimeInstance().format(new Date());
if (mCurrentLocation != null && mLastUpdateTime != null) {
String lat = String.valueOf(mCurrentLocation.getLatitude());
String lng = String.valueOf(mCurrentLocation.getLongitude());
calendar = Calendar.getInstance();
@SuppressLint("SimpleDateFormat") SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.sss");
simpleDateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
report_time = simpleDateFormat.format(calendar.getTime());
Log.d("preferences", "Time stored in preferences: " + mLastUpdateTime);
SharedPreferences sharedPreferences = getApplicationContext().getSharedPreferences("user_location", MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString("user_latitude", lat);
editor.putString("user_longitude", lng);
editor.putString("getLatLongIf", "ok");
editor.putString("report_time_for_location_response", report_time);
editor.putString("time_when_comes_to_getLatLong", timeWhenGetInGetLatLong);
editor.apply();
}else{
timeFromElse = DateFormat.getTimeInstance().format(new Date());
SharedPreferences sharedPreferences = getApplicationContext().getSharedPreferences("user_location", MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString("getLatLongElse", timeFromElse);
editor.apply();
}
}
protected void createLocationRequest() {
mGoogleApiClient.connect();
mLocationRequest = new LocationRequest();
mLocationRequest.setInterval(UPDATE_INTERVAL_IN_MILLISECONDS);
mLocationRequest.setFastestInterval(FASTEST_UPDATE_INTERVAL_IN_MILLISECONDS);
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
}
protected void startLocationUpdates() {
timeWhenStartLocationUpdates = DateFormat.getTimeInstance().format(new Date());
if (!mRequestingLocationUpdates) {
mRequestingLocationUpdates = true;
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
return;
}
LocationServices.FusedLocationApi.requestLocationUpdates(
mGoogleApiClient, mLocationRequest, this);
SharedPreferences sharedPreferences = getApplicationContext().getSharedPreferences("user_location", MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString("startLocationUpdates", timeWhenStartLocationUpdates);
editor.apply();
Log.i("startUpdates", " startLocationUpdates===" + timeWhenStartLocationUpdates);
isEnded = true;
}
}
protected void stopLocationUpdates() {
if (mRequestingLocationUpdates) {
mRequestingLocationUpdates = false;
timeWhenStopLocationUpdates = DateFormat.getTimeInstance().format(new Date());
SharedPreferences sharedPreferences = getApplicationContext().getSharedPreferences("user_location", MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString("stopLocationUpdates", timeWhenStopLocationUpdates);
editor.apply();
Log.d("stopUpdates", "stopLocationUpdates();==" + timeWhenStopLocationUpdates);
LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this);
}
}
@Override
public void onDestroy() {
super.onDestroy();
timeWhenServiceDestroyed = DateFormat.getTimeInstance().format(new Date());
SharedPreferences sharedPreferences = getApplicationContext().getSharedPreferences("user_location", MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString("onDestroyService", timeWhenServiceDestroyed);
editor.apply();
stopLocationUpdates();
Log.d("onDestroyService", "onDestroy: + " + timeWhenServiceDestroyed);
}
@Override
public void onCreate() {
super.onCreate();
isEnded = false;
mRequestingLocationUpdates = false;
mLastUpdateTime = "";
buildGoogleApiClient();
Log.d("onCreateService", "onCreateService");
}
Again, I am starting my service in onCreate on Main activity... Could anyone helps me? Thanks in advance.