Handle properly background task life cycle

This commit is contained in:
Mieszko Wrzeszczyński 2018-11-05 10:37:25 +01:00
parent 03c38a8f23
commit 03235ce18b
4 changed files with 42 additions and 21 deletions

View File

@ -34,6 +34,8 @@ import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;
import static com.uam.wmi.findmytutor.utils.PrefUtils.storeBackgroundLocationStatus;
public abstract class BaseActivity public abstract class BaseActivity
extends AppCompatActivity extends AppCompatActivity
@ -85,9 +87,13 @@ public abstract class BaseActivity
startActivity(launchIntent); startActivity(launchIntent);
} else if (itemName.equals(getResources().getString(R.string.navigation_item_logout))) { } else if (itemName.equals(getResources().getString(R.string.navigation_item_logout))) {
if(PrefUtils.isBackgroundLocationServiceRunning(getApplicationContext())) {
stopBackgroundLocalizationTask(); stopBackgroundLocalizationTask();
}
storeBackgroundLocationStatus(getApplication(),false);
PrefUtils.storeIsLoggedIn(getApplicationContext(),false);
PrefUtils.cleanUserLocalStorage(getApplicationContext());
Intent i = getBaseContext().getPackageManager() Intent i = getBaseContext().getPackageManager()
.getLaunchIntentForPackage(getBaseContext().getPackageName()); .getLaunchIntentForPackage(getBaseContext().getPackageName());
if (i != null) { if (i != null) {
@ -178,9 +184,10 @@ public abstract class BaseActivity
} }
public void handleBackgroundTaskLifeCycle() { public void handleBackgroundTaskLifeCycle() {
if (PrefUtils.isEnableSharingLocalization(getApplicationContext())) { if (PrefUtils.isEnableSharingLocalization(getApplicationContext())
&& !PrefUtils.isBackgroundLocationServiceRunning(getApplicationContext())) {
startBackgroundLocalizationTask(); startBackgroundLocalizationTask();
} else { } else if(PrefUtils.isBackgroundLocationServiceRunning(getApplicationContext())) {
stopBackgroundLocalizationTask(); stopBackgroundLocalizationTask();
} }
} }

View File

@ -125,7 +125,7 @@ public class MapActivity extends BaseActivity
mapView.getMapAsync(this); mapView.getMapAsync(this);
//start background task //start background task
startBackgroundLocalizationTask(); handleBackgroundTaskLifeCycle();
currentLanguage = getIntent().getStringExtra(currentLang); 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 // 300000 = 5mins
if ((System.currentTimeMillis() - coordinate.getTimeStamp()) > (long) 300000) { if ((System.currentTimeMillis() - coordinate.getTimeStamp()) > (long) 300000) {
String id = coordinate.getUserId(); String id = coordinate.getUserId();
@ -358,7 +358,7 @@ public class MapActivity extends BaseActivity
mapboxMap.removeMarker(markerToRemove); mapboxMap.removeMarker(markerToRemove);
} }
} }*/
} }

View File

@ -10,7 +10,6 @@ import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.content.pm.PackageManager; import android.content.pm.PackageManager;
import android.graphics.Color; import android.graphics.Color;
import android.location.Criteria;
import android.location.Location; import android.location.Location;
import android.location.LocationManager; import android.location.LocationManager;
import android.os.AsyncTask; import android.os.AsyncTask;
@ -40,6 +39,8 @@ import io.reactivex.schedulers.Schedulers;
import okhttp3.ResponseBody; import okhttp3.ResponseBody;
import timber.log.Timber; import timber.log.Timber;
import static com.uam.wmi.findmytutor.utils.PrefUtils.storeBackgroundLocationStatus;
public class BackgroundLocalizationService extends Service { public class BackgroundLocalizationService extends Service {
private static final String TAG = "MyLocationService"; private static final String TAG = "MyLocationService";
@ -48,6 +49,7 @@ public class BackgroundLocalizationService extends Service {
public static String str_receiver = "background.location.broadcast"; public static String str_receiver = "background.location.broadcast";
private static long notify_interval = 10000; private static long notify_interval = 10000;
Location mLastLocation; Location mLastLocation;
Boolean stopService = false;
ArrayList<String> providers = new ArrayList<String>(); ArrayList<String> providers = new ArrayList<String>();
LocationListener[] mLocationListeners; LocationListener[] mLocationListeners;
@ -79,13 +81,11 @@ public class BackgroundLocalizationService extends Service {
Log.e(TAG, "onStartCommand"); Log.e(TAG, "onStartCommand");
super.onStartCommand(intent, flags, startId); super.onStartCommand(intent, flags, startId);
//call startForeground first
boolean stopService = false;
if (intent != null) { if (intent != null) {
stopService = intent.getBooleanExtra("request_stop", false); stopService = intent.getBooleanExtra("request_stop", false);
} }
if (stopService) { if (stopService) {
storeBackgroundLocationStatus(getApplication(),false);
stopForeground(true); stopForeground(true);
stopSelf(); stopSelf();
return START_STICKY; return START_STICKY;
@ -97,7 +97,7 @@ public class BackgroundLocalizationService extends Service {
@Override @Override
public void onCreate() { public void onCreate() {
Log.e(TAG, "onCreate"); Log.e(TAG, "onCreate");
storeBackgroundLocationStatus(getApplication(),true);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
startMyOwnForeground(); startMyOwnForeground();
@ -129,6 +129,7 @@ public class BackgroundLocalizationService extends Service {
providerIndex++; providerIndex++;
} }
if(!stopService){
mStatusChecker = () -> { mStatusChecker = () -> {
try { try {
fn_getlocation(); fn_getlocation();
@ -140,6 +141,8 @@ public class BackgroundLocalizationService extends Service {
AsyncTask.execute(mStatusChecker); AsyncTask.execute(mStatusChecker);
} }
}
@RequiresApi(api = Build.VERSION_CODES.O) @RequiresApi(api = Build.VERSION_CODES.O)
private void startMyOwnForeground() { private void startMyOwnForeground() {
@ -206,7 +209,6 @@ public class BackgroundLocalizationService extends Service {
super.onDestroy(); super.onDestroy();
mHandler.removeCallbacks(mStatusChecker); mHandler.removeCallbacks(mStatusChecker);
if (mLocationManager != null) { if (mLocationManager != null) {
for (LocationListener listener : mLocationListeners) { for (LocationListener listener : mLocationListeners) {
try { try {

View File

@ -33,6 +33,9 @@ public class PrefUtils {
editor.clear().apply(); editor.clear().apply();
} }
public static void storeApiKey(Context context, String apiKey) { public static void storeApiKey(Context context, String apiKey) {
SharedPreferences.Editor editor = getSharedPreferences(context).edit(); SharedPreferences.Editor editor = getSharedPreferences(context).edit();
editor.putString("API_KEY", apiKey); editor.putString("API_KEY", apiKey);
@ -124,5 +127,14 @@ public class PrefUtils {
return getSharedPreferences(context).getString("LOCALE", "pl"); 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();
}
} }