#571 Keep location service after reboot
This commit is contained in:
parent
2ec549af7e
commit
f39dbe3b24
@ -5,33 +5,19 @@ import android.content.ComponentName;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.ServiceConnection;
|
||||
import android.os.Build;
|
||||
import android.os.IBinder;
|
||||
|
||||
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) {
|
||||
if (/*Utils.requestingLocationUpdates(context) && */Intent.ACTION_BOOT_COMPLETED.equalsIgnoreCase(intent.getAction())) {
|
||||
context.getApplicationContext().bindService(new Intent(context, LocationUpdatesService.class), mServiceConnection,
|
||||
Context.BIND_AUTO_CREATE);
|
||||
result = goAsync();
|
||||
if (Utils.requestingLocationUpdates(context) && Intent.ACTION_BOOT_COMPLETED.equalsIgnoreCase(intent.getAction())) {
|
||||
Intent serviceIntent = new Intent(context, LocationUpdatesService.class);
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
||||
context.startForegroundService(serviceIntent);
|
||||
} else {
|
||||
context.startService(serviceIntent);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -46,7 +46,7 @@ public class LocationUpdatesService extends Service {
|
||||
private static final String EXTRA_STARTED_FROM_NOTIFICATION = PACKAGE_NAME +
|
||||
".started_from_notification";
|
||||
|
||||
private final IBinder mBinder = new LocalBinder();
|
||||
//private final IBinder mBinder = new LocalBinder();
|
||||
|
||||
private static final int NOTIFICATION_ID = 954311;
|
||||
|
||||
@ -104,61 +104,43 @@ public class LocationUpdatesService extends Service {
|
||||
|
||||
// We got here because the user decided to remove location updates from the notification.
|
||||
if (startedFromNotification) {
|
||||
removeLocationUpdates();
|
||||
stopSelf();
|
||||
} else {
|
||||
requestLocationUpdates();
|
||||
}
|
||||
|
||||
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
|
||||
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 {
|
||||
mFusedLocationClient.removeLocationUpdates(mLocationCallback);
|
||||
} catch (SecurityException unlikely) {
|
||||
//When we lost permission
|
||||
Log.i(TAG, "No location permission");
|
||||
}
|
||||
Utils.setRequestingLocationUpdates(this, false);
|
||||
stopSelf();
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
||||
private Notification getNotification() {
|
||||
@ -236,10 +218,4 @@ public class LocationUpdatesService extends Service {
|
||||
.getInstance(getApplicationContext())
|
||||
.enqueueUniqueWork("SendLocationUpdate", ExistingWorkPolicy.REPLACE, uploadWorkRequest);
|
||||
}
|
||||
|
||||
public class LocalBinder extends Binder {
|
||||
LocationUpdatesService getService() {
|
||||
return LocationUpdatesService.this;
|
||||
}
|
||||
}
|
||||
}
|
@ -33,26 +33,6 @@ public class MainActivity extends FlutterActivity {
|
||||
|
||||
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
|
||||
public void configureFlutterEngine(@NonNull FlutterEngine flutterEngine) {
|
||||
GeneratedPluginRegistrant.registerWith(flutterEngine);
|
||||
@ -84,15 +64,17 @@ public class MainActivity extends FlutterActivity {
|
||||
}
|
||||
break;
|
||||
case "startLocationService":
|
||||
Utils.setRequestingLocationUpdates(this, true);
|
||||
if (isNoLocationPermissions()) {
|
||||
requestLocationPermissions();
|
||||
} else {
|
||||
mService.requestLocationUpdates();
|
||||
startLocationService();
|
||||
}
|
||||
result.success("");
|
||||
break;
|
||||
case "stopLocationService":
|
||||
mService.removeLocationUpdates();
|
||||
Utils.setRequestingLocationUpdates(this, false);
|
||||
stopLocationService();
|
||||
result.success("");
|
||||
break;
|
||||
}
|
||||
@ -104,6 +86,16 @@ public class MainActivity extends FlutterActivity {
|
||||
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
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
@ -117,8 +109,6 @@ public class MainActivity extends FlutterActivity {
|
||||
@Override
|
||||
protected void onStart() {
|
||||
super.onStart();
|
||||
bindService(new Intent(this, LocationUpdatesService.class), mServiceConnection,
|
||||
Context.BIND_AUTO_CREATE);
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -133,10 +123,6 @@ public class MainActivity extends FlutterActivity {
|
||||
|
||||
@Override
|
||||
protected void onStop() {
|
||||
if (mBound) {
|
||||
unbindService(mServiceConnection);
|
||||
mBound = false;
|
||||
}
|
||||
super.onStop();
|
||||
}
|
||||
|
||||
@ -156,7 +142,7 @@ public class MainActivity extends FlutterActivity {
|
||||
@NonNull int[] grantResults) {
|
||||
if (requestCode == REQUEST_PERMISSIONS_REQUEST_CODE) {
|
||||
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
|
||||
mService.requestLocationUpdates();
|
||||
startLocationService();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -33,6 +33,6 @@ class Utils {
|
||||
}
|
||||
|
||||
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()));
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user