Skip to content
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

Open
rajbecks23 opened this issue Apr 17, 2019 · 8 comments
Open

GPS Turned off, we do not receive location updates #2

rajbecks23 opened this issue Apr 17, 2019 · 8 comments

Comments

@rajbecks23
Copy link

When a user turns off the GPS, no location updates are received. Even if app is in background or killed state.

@BlackBlind567
Copy link
Owner

may be you will mistake somewhere because In this code using FUSED LOCATION API .
You can read here about fused location api

@kpradeepkumarreddy
Copy link

When a user turns off the GPS, no location updates are received. Even if app is in background or killed state.

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 ??
Can we periodically check if location is off and notify user from a foreground service ??

@kpradeepkumarreddy
Copy link

may be you will mistake somewhere because In this code using FUSED LOCATION API .
You can read here about fused location api

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 ??

@sattarsh
Copy link

hi
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 ??
pls help !

@kpradeepkumarreddy
Copy link

hi
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 ??
pls help !

Yes we can, i already implemented it and it is working

@sattarsh
Copy link

Very good
Can you upload something that I can use?
on github or telegram share this code ... very important .
my email : sattarshekoohi@yahoo.com

@kpradeepkumarreddy
Copy link

kpradeepkumarreddy commented Oct 24, 2020

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
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.

@sattarsh
Copy link

sattarsh commented Oct 24, 2020 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants