Service.startForeground() not allowed due to mAllowStartForeground false on Android 14

83 views Asked by At

My app gets this error since Android 14. I succed to reproduce it by trying to start the app from android Studio when the device is power off (only the screen).

Here is a description of my code: I subscribe to periodic GPS location using foreground service

        <service
            android:name=".Service.MyLocationService"
            android:foregroundServiceType="location"
            android:label="Location receiver" />

I start the service once in onResume() of the main activity.

 locationIntent = new Intent(this, MyLocationService.class);
 startForegroundService(locationIntent);

I ensure elsewhere that these permissions are granted : ACCESS_FINE_LOCATION & ACCESS_COARSE_LOCATION

And my service looks like this:

public class MyLocationService extends Service {

    private Notification myNotification = ...;

    private FusedLocationProviderClient mFusedLocationClient;

    public MyLocationService() {
        super();
    }

    @Override
    public void onCreate() {
        super.onCreate();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            startForeground(1, myNotification);
        }
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        initFunction(this);
        return Service.START_REDELIVER_INTENT;
    }

    @Override
    public void onDestroy() {
        destroyFunction();
        super.onDestroy();
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    public void initFunction(Context context) {

        mFusedLocationClient = LocationServices.getFusedLocationProviderClient(context);

        mLocationCallback = new LocationCallback() {
            @Override
            public void onLocationResult(LocationResult locationResult) {
                super.onLocationResult(locationResult);
                if (locationResult == null) {
                    return;
                }
       
                // Here I use the received location

            }
        };


        mLocationRequest = new LocationRequest.Builder(Priority.PRIORITY_HIGH_ACCURACY, 1000)
                .setWaitForAccurateLocation(true)
                .setMinUpdateIntervalMillis(1000)
                .setMaxUpdateDelayMillis(1000)
                .build();

        mFusedLocationClient.requestLocationUpdates(mLocationRequest, mLocationCallback, Looper.myLooper());

    }

   private void destroyFunction() {
        if (mFusedLocationClient != null) {
            mFusedLocationClient.removeLocationUpdates(mLocationCallback);
            mFusedLocationClient = null;
        }
    }

}

I have tried to substitute service by worker but with no success. How should I proceed now with android 14? An idea?

1

There are 1 answers

0
Fred Bro On

I have updated my code in this way with no benefic effect

@Override
public void onCreate() {
    super.onCreate();
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
        startForeground(1, myNotification,

ServiceInfo.FOREGROUND_SERVICE_TYPE_LOCATION); } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { startForeground(1, myNotification); } }