#571 Keep location service after reboot

This commit is contained in:
estevez-dev 2020-07-07 00:26:38 +03:00
parent 2ec549af7e
commit f39dbe3b24
4 changed files with 48 additions and 100 deletions

View File

@ -5,33 +5,19 @@ import android.content.ComponentName;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.content.ServiceConnection; import android.content.ServiceConnection;
import android.os.Build;
import android.os.IBinder; import android.os.IBinder;
public class Autostart extends BroadcastReceiver { public class Autostart extends BroadcastReceiver {
private PendingResult result;
private final ServiceConnection mServiceConnection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
LocationUpdatesService.LocalBinder binder = (LocationUpdatesService.LocalBinder) service;
binder.getService().requestLocationUpdates();
result.finish();
}
@Override
public void onServiceDisconnected(ComponentName componentName) {
}
};
public void onReceive(Context context, Intent intent) { public void onReceive(Context context, Intent intent) {
if (/*Utils.requestingLocationUpdates(context) && */Intent.ACTION_BOOT_COMPLETED.equalsIgnoreCase(intent.getAction())) { if (Utils.requestingLocationUpdates(context) && Intent.ACTION_BOOT_COMPLETED.equalsIgnoreCase(intent.getAction())) {
context.getApplicationContext().bindService(new Intent(context, LocationUpdatesService.class), mServiceConnection, Intent serviceIntent = new Intent(context, LocationUpdatesService.class);
Context.BIND_AUTO_CREATE); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
result = goAsync(); context.startForegroundService(serviceIntent);
} else {
context.startService(serviceIntent);
}
} }
} }
} }

View File

@ -46,7 +46,7 @@ public class LocationUpdatesService extends Service {
private static final String EXTRA_STARTED_FROM_NOTIFICATION = PACKAGE_NAME + private static final String EXTRA_STARTED_FROM_NOTIFICATION = PACKAGE_NAME +
".started_from_notification"; ".started_from_notification";
private final IBinder mBinder = new LocalBinder(); //private final IBinder mBinder = new LocalBinder();
private static final int NOTIFICATION_ID = 954311; private static final int NOTIFICATION_ID = 954311;
@ -104,62 +104,44 @@ public class LocationUpdatesService extends Service {
// We got here because the user decided to remove location updates from the notification. // We got here because the user decided to remove location updates from the notification.
if (startedFromNotification) { if (startedFromNotification) {
removeLocationUpdates();
stopSelf(); stopSelf();
} else {
requestLocationUpdates();
} }
return START_STICKY; return START_STICKY;
} }
@Nullable
@Override
public IBinder onBind(Intent intent) {
return mBinder;
}
@Override
public void onRebind(Intent intent) {
super.onRebind(intent);
}
@Override
public boolean onUnbind(Intent intent) {
return true;
}
@Override @Override
public void onDestroy() { public void onDestroy() {
mServiceHandler.removeCallbacksAndMessages(null);
}
public void requestLocationUpdates() {
long requestInterval = Utils.getLocationUpdateIntervals(getApplicationContext());
Log.i(TAG, "Requesting location updates. Interval is " + requestInterval);
mLocationRequest.setInterval(requestInterval);
mLocationRequest.setFastestInterval(requestInterval);
Utils.setRequestingLocationUpdates(this, true);
startService(new Intent(getApplicationContext(), LocationUpdatesService.class));
startForeground(NOTIFICATION_ID, getNotification());
try {
mFusedLocationClient.requestLocationUpdates(mLocationRequest,
mLocationCallback, Looper.myLooper());
} catch (SecurityException unlikely) {
//When we lost permission
removeLocationUpdates();
}
}
public void removeLocationUpdates() {
Log.i(TAG, "Removing location updates");
try { try {
mFusedLocationClient.removeLocationUpdates(mLocationCallback); mFusedLocationClient.removeLocationUpdates(mLocationCallback);
} catch (SecurityException unlikely) { } catch (SecurityException unlikely) {
//When we lost permission //When we lost permission
Log.i(TAG, "No location permission"); Log.i(TAG, "No location permission");
} }
Utils.setRequestingLocationUpdates(this, false); mServiceHandler.removeCallbacksAndMessages(null);
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
private void requestLocationUpdates() {
long requestInterval = Utils.getLocationUpdateIntervals(getApplicationContext());
Log.i(TAG, "Requesting location updates. Interval is " + requestInterval);
mLocationRequest.setInterval(requestInterval);
mLocationRequest.setFastestInterval(requestInterval);
startForeground(NOTIFICATION_ID, getNotification());
try {
mFusedLocationClient.requestLocationUpdates(mLocationRequest,
mLocationCallback, Looper.myLooper());
} catch (SecurityException unlikely) {
stopSelf(); stopSelf();
} }
}
private Notification getNotification() { private Notification getNotification() {
Intent intent = new Intent(this, LocationUpdatesService.class); Intent intent = new Intent(this, LocationUpdatesService.class);
@ -236,10 +218,4 @@ public class LocationUpdatesService extends Service {
.getInstance(getApplicationContext()) .getInstance(getApplicationContext())
.enqueueUniqueWork("SendLocationUpdate", ExistingWorkPolicy.REPLACE, uploadWorkRequest); .enqueueUniqueWork("SendLocationUpdate", ExistingWorkPolicy.REPLACE, uploadWorkRequest);
} }
public class LocalBinder extends Binder {
LocationUpdatesService getService() {
return LocationUpdatesService.this;
}
}
} }

View File

@ -33,26 +33,6 @@ public class MainActivity extends FlutterActivity {
private static final int REQUEST_PERMISSIONS_REQUEST_CODE = 34; private static final int REQUEST_PERMISSIONS_REQUEST_CODE = 34;
private LocationUpdatesService mService = null;
private boolean mBound = false;
private final ServiceConnection mServiceConnection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
LocationUpdatesService.LocalBinder binder = (LocationUpdatesService.LocalBinder) service;
mService = binder.getService();
mBound = true;
}
@Override
public void onServiceDisconnected(ComponentName name) {
mService = null;
mBound = false;
}
};
@Override @Override
public void configureFlutterEngine(@NonNull FlutterEngine flutterEngine) { public void configureFlutterEngine(@NonNull FlutterEngine flutterEngine) {
GeneratedPluginRegistrant.registerWith(flutterEngine); GeneratedPluginRegistrant.registerWith(flutterEngine);
@ -84,15 +64,17 @@ public class MainActivity extends FlutterActivity {
} }
break; break;
case "startLocationService": case "startLocationService":
Utils.setRequestingLocationUpdates(this, true);
if (isNoLocationPermissions()) { if (isNoLocationPermissions()) {
requestLocationPermissions(); requestLocationPermissions();
} else { } else {
mService.requestLocationUpdates(); startLocationService();
} }
result.success(""); result.success("");
break; break;
case "stopLocationService": case "stopLocationService":
mService.removeLocationUpdates(); Utils.setRequestingLocationUpdates(this, false);
stopLocationService();
result.success(""); result.success("");
break; break;
} }
@ -104,6 +86,16 @@ public class MainActivity extends FlutterActivity {
return (GoogleApiAvailability.getInstance().isGooglePlayServicesAvailable(this) == ConnectionResult.SUCCESS); return (GoogleApiAvailability.getInstance().isGooglePlayServicesAvailable(this) == ConnectionResult.SUCCESS);
} }
private void startLocationService() {
Intent myService = new Intent(MainActivity.this, LocationUpdatesService.class);
startService(myService);
}
private void stopLocationService() {
Intent myService = new Intent(MainActivity.this, LocationUpdatesService.class);
stopService(myService);
}
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
@ -117,8 +109,6 @@ public class MainActivity extends FlutterActivity {
@Override @Override
protected void onStart() { protected void onStart() {
super.onStart(); super.onStart();
bindService(new Intent(this, LocationUpdatesService.class), mServiceConnection,
Context.BIND_AUTO_CREATE);
} }
@Override @Override
@ -133,10 +123,6 @@ public class MainActivity extends FlutterActivity {
@Override @Override
protected void onStop() { protected void onStop() {
if (mBound) {
unbindService(mServiceConnection);
mBound = false;
}
super.onStop(); super.onStop();
} }
@ -156,7 +142,7 @@ public class MainActivity extends FlutterActivity {
@NonNull int[] grantResults) { @NonNull int[] grantResults) {
if (requestCode == REQUEST_PERMISSIONS_REQUEST_CODE) { if (requestCode == REQUEST_PERMISSIONS_REQUEST_CODE) {
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) { if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
mService.requestLocationUpdates(); startLocationService();
} }
} }
} }

View File

@ -33,6 +33,6 @@ class Utils {
} }
static String getLocationTitle(Location location) { static String getLocationTitle(Location location) {
return "Location updated at " + DateFormat.getDateTimeInstance().format(new Date(location.getTime())); return location == null ? "Requesting location..." : "Location updated at " + DateFormat.getDateTimeInstance().format(new Date(location.getTime()));
} }
} }