-
Notifications
You must be signed in to change notification settings - Fork 33
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
GPS Turned off, we do not receive location updates #2
Comments
may be you will mistake somewhere because In this code using FUSED LOCATION API . |
I agree with you, I'm also facing the same issue. As long as the gps is turned on, i'm getting location updates, but when gps is turned off, i'm not getting location updates. Of course, in the documentation it is mentioned that FusedLocationProviderClientApi will get location from either wifi or gps, but it is not working according to the documentation. Did you find any solution for this issue ?? |
Of course, in the documentation it is mentioned that FusedLocationProviderClientApi will get location from either wifi or gps, but it is not working according to the documentation. As long as the gps is turned on, i'm getting location updates, but when gps is turned off, i'm not getting location updates. Can we periodically check if location is off and notify user from a foreground service ?? |
hi Can we periodically check if location is off and notify user from a foreground service ?? |
Yes we can, i already implemented it and it is working |
Very good |
I'm using two approaches. First approach is registering broadcast receiver to detect GPS settings change in foreground service.
When GPS is turned off, i'm playing audio message to user to turn on the GPS. Here i'm using audio alert because user might not be looking at the app continuously as long as the foreground service is running.
Hope this helps. This is the best solution i could figure out. If you can find any other solution better than this. Please share it to me as well. |
I have reached a series of results that I am testing and if the result, I will definitely send it to youMaybe you would please send me the full project for location with service and ... in a zipped form, so you were confusedThanks for your kindness
On Saturday, October 24, 2020, 04:15:40 PM GMT+3:30, K Pradeep Kumar Reddy <notifications@github.com> wrote:
I'm using two approaches. First approach is registering broadcast receiver to detect GPS settings change in foreground service.
Following code is for Broadcast receiver
`class GpsSettingChangedBroadcastReceiver : BroadcastReceiver() {
override fun onReceive(context: Context?, intent: Intent?) {
try {
if (intent?.action == "android.location.PROVIDERS_CHANGED") {
Timber.d("onReceive(), intent.action == PROVIDERS_CHANGED")
val contentResolver: ContentResolver? = context?.contentResolver
// Find out what the settings say about which providers are enabled
val mode = Settings.Secure.getInt(contentResolver, Settings.Secure.LOCATION_MODE,
Settings.Secure.LOCATION_MODE_OFF)
if (mode == Settings.Secure.LOCATION_MODE_OFF) {
Timber.d("onReceive(), LOCATION is OFF")
var count = 2
val mediaPlayer: MediaPlayer = MediaPlayer.create(context, R.raw.male_enable_location)
mediaPlayer.setOnCompletionListener { mediaPlayer ->
if (count > 0) {
count--
mediaPlayer.start()
Toast.makeText(context, "Please enable location", Toast.LENGTH_LONG).show()
} else {
mediaPlayer.reset()
mediaPlayer.release()
}
}
mediaPlayer.start()
Toast.makeText(context, "Please enable location", Toast.LENGTH_LONG).show()
} else {
Timber.d("onReceive(), LOCATION is ON")
}
} else {
Timber.d("onReceive(), unknown intent.action = ${intent?.action}")
}
} catch (ex: Exception) {
Timber.e(ex, "Exception in GpsSettingChangedBroadcastReceiver()::onReceive() ")
}
}`
When GPS is turned off, i'm playing audio message to user to turn on the GPS. Here i'm using audio alert because user might not be looking at the app continuously as long as the foreground service is running.
In your foreground service, register and unregister the above broadcast receiver at appropriate place
// registering GpsSettingChangedBroadcastReceiver gpsSettingChangedBroadcastReceiver = GpsSettingChangedBroadcastReceiver() val intentFilter = IntentFilter(LocationManager.PROVIDERS_CHANGED_ACTION) registerReceiver(gpsSettingChangedBroadcastReceiver, intentFilter)
Other approach, second approach is periodically check if GPS is turned off in your foreground service using repeating timer which runs every 5 minutes or so, according to your requirement.
timer = Timer() // Set the schedule function timer.scheduleAtFixedRate(object : TimerTask() { override fun run() { // code to check if internet and location enabled CheckInternetAndLocationTask().execute() } }, 0, internetAndLocationCheckInterval)
`class CheckInternetAndLocationTask : AsyncTask<Void, Void, Void>() {
private var isInternetEnabled = true
private var isLocationEnabled = true
override fun doInBackground(vararg p0: Void?): Void? {
try {
val sock = Socket()
val sockAddress: SocketAddress = InetSocketAddress("8.8.8.8", 53)
sock.connect(sockAddress, 3000)
sock.close()
} catch (ex: Exception) {
Timber.e(ex, "Exception in internet enabled check")
isInternetEnabled = false;
}
isLocationEnabled = try {
val locationManager = getSystemService(Context.LOCATION_SERVICE) as LocationManager
locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)
} catch (ex: Exception) {
Timber.e(ex, "Exception in location enabled check")
false
}
return null
}
override fun onPostExecute(result: Void?) {
var count = 2
if (!isInternetEnabled && !isLocationEnabled) {
val mediaPlayer: MediaPlayer = MediaPlayer.create(applicationContext,
R.raw.male_enable_internet_location)
mediaPlayer.setOnCompletionListener { mediaPlayer ->
if (count > 0) {
count--
mediaPlayer.start()
Toast.makeText(applicationContext, "Please enable internet and location",
Toast.LENGTH_LONG).show()
} else {
mediaPlayer.reset()
mediaPlayer.release()
}
}
mediaPlayer.start()
Toast.makeText(applicationContext, "Please enable internet and location", Toast.LENGTH_LONG).show()
return
}
if (!isInternetEnabled) {
val mediaPlayer: MediaPlayer = MediaPlayer.create(applicationContext, R.raw.male_enable_internet)
mediaPlayer.setOnCompletionListener { mediaPlayer ->
if (count > 0) {
count--
mediaPlayer.start()
Toast.makeText(applicationContext, "Please enable internet", Toast.LENGTH_LONG).show()
} else {
mediaPlayer.reset()
mediaPlayer.release()
}
}
mediaPlayer.start()
Toast.makeText(applicationContext, "Please enable internet", Toast.LENGTH_LONG).show()
return
}
if (!isLocationEnabled) {
val mediaPlayer: MediaPlayer = MediaPlayer.create(applicationContext, R.raw.male_enable_location)
mediaPlayer.setOnCompletionListener { mediaPlayer ->
if (count > 0) {
count--
mediaPlayer.start()
Toast.makeText(applicationContext, "Please enable location", Toast.LENGTH_LONG).show()
} else {
mediaPlayer.reset()
mediaPlayer.release()
}
}
mediaPlayer.start()
Toast.makeText(applicationContext, "Please enable location", Toast.LENGTH_LONG).show()
return
}
}
}`
Hope this helps. This is the best solution i could figure out. If you can find any other solution better than this. Please share it to me as well.
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub, or unsubscribe.
|
When a user turns off the GPS, no location updates are received. Even if app is in background or killed state.
The text was updated successfully, but these errors were encountered: