fetching location in background while the device is locked in android

Viewed 643

We are creating a solution for the fleet industry. The app required the device location to be fetched and send all the time, even if the device is locked. we tried the following approaches

  • we have enabled the following permissions
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
    <uses-permission android:name="android.permission.INTERNET" />
  <uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" />
  • On app start created a foreground service and pushed it into the notification panel.
  • in the forground service we are calling locManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 3000, 0, this);
  • on the OnLocationChanged we are updating the notification panel with current location to check it is working or not.

The app works fine when the device is active. but when the device went to sleep after screen lock. the application it selef was terminated by the OS. We tested it in android 9. Is this the correct approch ? Am I missing anything? Please suggest.

0 Answers
Related