Handle properly background task life cycle
This commit is contained in:
parent
03c38a8f23
commit
03235ce18b
@ -34,6 +34,8 @@ import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import static com.uam.wmi.findmytutor.utils.PrefUtils.storeBackgroundLocationStatus;
|
||||
|
||||
|
||||
public abstract class BaseActivity
|
||||
extends AppCompatActivity
|
||||
@ -85,9 +87,13 @@ public abstract class BaseActivity
|
||||
startActivity(launchIntent);
|
||||
|
||||
} else if (itemName.equals(getResources().getString(R.string.navigation_item_logout))) {
|
||||
stopBackgroundLocalizationTask();
|
||||
if(PrefUtils.isBackgroundLocationServiceRunning(getApplicationContext())) {
|
||||
stopBackgroundLocalizationTask();
|
||||
}
|
||||
|
||||
storeBackgroundLocationStatus(getApplication(),false);
|
||||
PrefUtils.storeIsLoggedIn(getApplicationContext(),false);
|
||||
|
||||
PrefUtils.cleanUserLocalStorage(getApplicationContext());
|
||||
Intent i = getBaseContext().getPackageManager()
|
||||
.getLaunchIntentForPackage(getBaseContext().getPackageName());
|
||||
if (i != null) {
|
||||
@ -178,9 +184,10 @@ public abstract class BaseActivity
|
||||
}
|
||||
|
||||
public void handleBackgroundTaskLifeCycle() {
|
||||
if (PrefUtils.isEnableSharingLocalization(getApplicationContext())) {
|
||||
if (PrefUtils.isEnableSharingLocalization(getApplicationContext())
|
||||
&& !PrefUtils.isBackgroundLocationServiceRunning(getApplicationContext())) {
|
||||
startBackgroundLocalizationTask();
|
||||
} else {
|
||||
} else if(PrefUtils.isBackgroundLocationServiceRunning(getApplicationContext())) {
|
||||
stopBackgroundLocalizationTask();
|
||||
}
|
||||
}
|
||||
|
@ -125,7 +125,7 @@ public class MapActivity extends BaseActivity
|
||||
mapView.getMapAsync(this);
|
||||
|
||||
//start background task
|
||||
startBackgroundLocalizationTask();
|
||||
handleBackgroundTaskLifeCycle();
|
||||
|
||||
currentLanguage = getIntent().getStringExtra(currentLang);
|
||||
}
|
||||
@ -348,7 +348,7 @@ public class MapActivity extends BaseActivity
|
||||
}
|
||||
}
|
||||
|
||||
for (Coordinate coordinate : coordsMap.values()) {
|
||||
/* for (Coordinate coordinate : coordsMap.values()) {
|
||||
// 300000 = 5mins
|
||||
if ((System.currentTimeMillis() - coordinate.getTimeStamp()) > (long) 300000) {
|
||||
String id = coordinate.getUserId();
|
||||
@ -358,7 +358,7 @@ public class MapActivity extends BaseActivity
|
||||
mapboxMap.removeMarker(markerToRemove);
|
||||
}
|
||||
|
||||
}
|
||||
}*/
|
||||
|
||||
|
||||
}
|
||||
|
@ -10,7 +10,6 @@ import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.graphics.Color;
|
||||
import android.location.Criteria;
|
||||
import android.location.Location;
|
||||
import android.location.LocationManager;
|
||||
import android.os.AsyncTask;
|
||||
@ -40,6 +39,8 @@ import io.reactivex.schedulers.Schedulers;
|
||||
import okhttp3.ResponseBody;
|
||||
import timber.log.Timber;
|
||||
|
||||
import static com.uam.wmi.findmytutor.utils.PrefUtils.storeBackgroundLocationStatus;
|
||||
|
||||
public class BackgroundLocalizationService extends Service {
|
||||
|
||||
private static final String TAG = "MyLocationService";
|
||||
@ -48,6 +49,7 @@ public class BackgroundLocalizationService extends Service {
|
||||
public static String str_receiver = "background.location.broadcast";
|
||||
private static long notify_interval = 10000;
|
||||
Location mLastLocation;
|
||||
Boolean stopService = false;
|
||||
|
||||
ArrayList<String> providers = new ArrayList<String>();
|
||||
LocationListener[] mLocationListeners;
|
||||
@ -79,13 +81,11 @@ public class BackgroundLocalizationService extends Service {
|
||||
Log.e(TAG, "onStartCommand");
|
||||
super.onStartCommand(intent, flags, startId);
|
||||
|
||||
//call startForeground first
|
||||
boolean stopService = false;
|
||||
|
||||
if (intent != null) {
|
||||
stopService = intent.getBooleanExtra("request_stop", false);
|
||||
}
|
||||
if (stopService) {
|
||||
storeBackgroundLocationStatus(getApplication(),false);
|
||||
stopForeground(true);
|
||||
stopSelf();
|
||||
return START_STICKY;
|
||||
@ -97,7 +97,7 @@ public class BackgroundLocalizationService extends Service {
|
||||
@Override
|
||||
public void onCreate() {
|
||||
Log.e(TAG, "onCreate");
|
||||
|
||||
storeBackgroundLocationStatus(getApplication(),true);
|
||||
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
|
||||
startMyOwnForeground();
|
||||
@ -129,15 +129,18 @@ public class BackgroundLocalizationService extends Service {
|
||||
providerIndex++;
|
||||
}
|
||||
|
||||
mStatusChecker = () -> {
|
||||
try {
|
||||
fn_getlocation();
|
||||
} finally {
|
||||
mHandler.postDelayed(mStatusChecker, notify_interval);
|
||||
}
|
||||
};
|
||||
if(!stopService){
|
||||
mStatusChecker = () -> {
|
||||
try {
|
||||
fn_getlocation();
|
||||
} finally {
|
||||
mHandler.postDelayed(mStatusChecker, notify_interval);
|
||||
}
|
||||
};
|
||||
|
||||
AsyncTask.execute(mStatusChecker);
|
||||
}
|
||||
|
||||
AsyncTask.execute(mStatusChecker);
|
||||
}
|
||||
|
||||
@RequiresApi(api = Build.VERSION_CODES.O)
|
||||
@ -206,7 +209,6 @@ public class BackgroundLocalizationService extends Service {
|
||||
super.onDestroy();
|
||||
mHandler.removeCallbacks(mStatusChecker);
|
||||
|
||||
|
||||
if (mLocationManager != null) {
|
||||
for (LocationListener listener : mLocationListeners) {
|
||||
try {
|
||||
|
@ -33,6 +33,9 @@ public class PrefUtils {
|
||||
editor.clear().apply();
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
public static void storeApiKey(Context context, String apiKey) {
|
||||
SharedPreferences.Editor editor = getSharedPreferences(context).edit();
|
||||
editor.putString("API_KEY", apiKey);
|
||||
@ -124,5 +127,14 @@ public class PrefUtils {
|
||||
return getSharedPreferences(context).getString("LOCALE", "pl");
|
||||
}
|
||||
|
||||
public static Boolean isBackgroundLocationServiceRunning(Context context) {
|
||||
return getSharedPreferences(context).getBoolean("BACKGROUND_SERVICE_STATUS", false);
|
||||
}
|
||||
|
||||
public static void storeBackgroundLocationStatus(Context context, Boolean status) {
|
||||
SharedPreferences.Editor editor = getSharedPreferences(context).edit();
|
||||
editor.putBoolean("BACKGROUND_SERVICE_STATUS", status);
|
||||
editor.apply();
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user