I am creating an application which is inspired by another app in which the application collect my location even if the application is closed I am able to achieve the similar behaviour by creating a broadcast receiver which collect my location but In my application when I collect the location I get warning form the Android Setting that this app access location in the background but for the other app form playstore I have not see any such warning till this time I am using that app for a while now.
How can I achieve similar behaviour.
One thing which I have notice is that when that other app collect the location the android location icon show in the status bar but in case when I collect the location I can't see that. May be this gives some hint.
I even tried to star a foreground service while collection the location while the app is closed but can't start the service form the background.
I get this error:
Failed to start service: startForegroundService() not allowed due to mAllowStartForeground false: service com.kgJr.safecircle/.ui_util.services.LocationUpdateService
So how can I achieve the behaviour I am looking for.
I will be glad if someone can help me on this. Thank You !!!
I am trying the collect the location in the background while avoiding the warning from android default system that <This> app access location in the background like a similar app I am using which also collect the location in the background even if the application is closed and I wont get any such warning.
current code for fetching the location
@RequiresPermission(allOf = [Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION])
fun getCurrentLocation(context: Context, callback: (Location?) -> Unit) {
if (!isLocationPermissionGranted(context)) {
callback(null)
return
}
val fusedLocationClient = getFusedLocationClient(context)
fusedLocationClient.lastLocation.addOnSuccessListener { location ->
callback(location)
}.addOnFailureListener {
callback(null)
}
}
all the permissions
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
<uses-permission android:name="android.permission.ACTIVITY_RECOGNITION" />
depending on how often you need to get the location - you can use an AlarmManager with BroadcastReceiver that will launch a WorkManager to get the current location, but for this scenario you'll need the ACCESS_BACKGROUND_LOCATION permission.
The other way around is to use a ForegroundService that can be started only when the app is in foreground and keep it running as long as needed, but I guess you don't want that.