Reputation: 2707
I have a Service called ServiceTechnicianTrackingService that tracks location with foreground notification of the people using phone.
public class ServiceTechnicianTrackingService extends Service {
private FusedLocationProviderClient fusedLocationProviderClient;
private LocationCallback locationCallback;
@Inject ServiceTechnicianLocationManager serviceTechnicianCoordinateManager;
@Inject NetworkManager networkManager;
public static final String ACTION_STOP_SERVICE = "action_stop_service";
private boolean isStarted = false;
@Nullable @Override public IBinder onBind(Intent intent) {
return null;
}
@Override public void onCreate() {
super.onCreate();
injector().inject(this);
fusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(this);
locationCallback = new LocationCallback() {
@Override public void onLocationResult(@NotNull LocationResult locationResult) {
if(locationResult != null && locationResult.getLastLocation() != null) {
super.onLocationResult(locationResult);
String lat = String.valueOf(locationResult.getLastLocation().getLatitude());
String lng = String.valueOf(locationResult.getLastLocation().getLongitude());
Timber.d("Lat is: " + lat + " lng is: " + lng);
if(networkManager.isNetworkAvailable()) {
serviceTechnicianCoordinateManager.updateCurrentLocation(new Coordinate(lat, lng));
}
}
}
};
}
@Override public int onStartCommand(Intent intent, int flags, int startId) {
// Call startForeground early to prevent the exception
if(!isStarted) {
startForegroundAndDisplayNotification();
isStarted = true;
}
if(intent != null) {
String action = intent.getAction();
if(ACTION_STOP_SERVICE.equalsIgnoreCase(action)) {
stopForeground(true);
stopSelf();
} else {
requestLocationUpdates();
}
}
return START_STICKY;
}
@Override public void onDestroy() {
super.onDestroy();
stopLocationUpdates();
isStarted = false;
}
private void startForegroundAndDisplayNotification() {
if (Build.VERSION.SDK_INT >= 26) {
Notification notification = createNotification();
startForeground(1, notification);
}
}
private Notification createNotification() {
// code to create notification
}
}
And this service is called from MainActivity like this:
@Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
injector().inject(this);
presenter.setView(this);
trackServiceTechnicianIfNeeded();
}
private void trackServiceTechnicianIfNeeded() {
if (presenter.isTrackable()) {
if (ViewUtil.isPlayServiceAvailable(this)) {
if (isForegroundLocationPermissionGranted() && presenter.isTrackTechnicianRouteAllowed()) {
tryToStartForegroundLocationService();
presenter.saveThatUserAskedForPermission();
} else {
if (!presenter.isUserAskedForLocationPermission()) {
askForLocationPermission();
presenter.saveThatUserAskedForPermission();
}
}
}
} else {
stopServiceTechnicianLocationService();
}
}
private void tryToStartForegroundLocationService() {
if(Utils.isPlayServiceAvailable(this)) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
if(isPostNotificationsPermissionGranted()) {
startForegroundService();
} else {
askForPostNotificationsPermission();
}
} else {
startForegroundService();
}
}
}
private void startForegroundService() {
Intent i = new Intent(this, ServiceTechnicianTrackingService.class);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
startForegroundService(i);
} else {
startService(i);
}
}
@Override public void stopServiceTechnicianLocationService() {
stopService(new Intent(this, ServiceTechnicianTrackingService.class));
}
but I get this exception:
Caused by android.app.ForegroundServiceStartNotAllowedException: Service.startForeground() not allowed due to mAllowStartForeground false: service com.anstar.fieldworkhq/.coordinates.ServiceTechnicianTrackingService
at android.app.ForegroundServiceStartNotAllowedException$1.createFromParcel(ForegroundServiceStartNotAllowedException.java:54)
at android.app.ForegroundServiceStartNotAllowedException$1.createFromParcel(ForegroundServiceStartNotAllowedException.java:50)
at android.os.Parcel.readParcelableInternal(Parcel.java:4882)
at android.os.Parcel.readParcelable(Parcel.java:4864)
at android.os.Parcel.createExceptionOrNull(Parcel.java:3064)
at android.os.Parcel.createException(Parcel.java:3053)
at android.os.Parcel.readException(Parcel.java:3036)
at android.os.Parcel.readException(Parcel.java:2978)
at android.app.IActivityManager$Stub$Proxy.setServiceForeground(IActivityManager.java:7234)
at android.app.Service.startForeground(Service.java:775)
at com.anstar.fieldworkhq.coordinates.ServiceTechnicianTrackingService.startForegroundAndDisplayNotification(ServiceTechnicianTrackingService.java:97)
at com.anstar.fieldworkhq.coordinates.ServiceTechnicianTrackingService.onStartCommand(ServiceTechnicianTrackingService.java:72)
at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:5268)
at android.app.ActivityThread.-$$Nest$mhandleServiceArgs()
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2531)
Upvotes: 1
Views: 33