0

Dear friend I used a lot of sample codes to turn on location (gps) but no one work properly. I want to turn on Location without close app or anything. I want to turn on that over app without switch to settings or another page.

mehdi
  • 31
  • 1
  • 6
  • https://developers.google.com/android/reference/com/google/android/gms/location/SettingsClient – CommonsWare May 18 '20 at 12:49
  • Here is what you want: https://stackoverflow.com/a/61868985/12478830 – MMG May 18 '20 at 12:52
  • Does this answer your question? [How to show enable location dialog like Google maps?](https://stackoverflow.com/questions/29801368/how-to-show-enable-location-dialog-like-google-maps) – Daan Seuntjens May 18 '20 at 12:54
  • Check this Answer:- https://stackoverflow.com/a/33254073/9523118 – Krishna Sony May 18 '20 at 12:56
  • I checked all of them, but my problem not solved – mehdi May 18 '20 at 13:00
  • What is the problem with my link? – MMG May 18 '20 at 13:01
  • when I enable loc my activity close and location dialog box show. I don't want to close activity. SO I want after enable location work with app without run that again by touch on icon I used [link](https://stackoverflow.com/questions/29801368/how-to-show-enable-location-dialog-like-google-maps) but not work properly – mehdi May 18 '20 at 13:06
  • You don't want to allow it manually. – MMG May 18 '20 at 13:09
  • I just want user confirm dialog box without close app – mehdi May 18 '20 at 13:10
  • When I call Enable Location function my app goes to background and location dialog box show. it means I just see home screen of my device with a dialog box in center of that – mehdi May 18 '20 at 13:13
  • In my code, app won't be closed – MMG May 18 '20 at 13:14
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/214108/discussion-between-mehdi-and-mmg). – mehdi May 18 '20 at 13:16
  • If your app is getting closed, then post the stack trace. – Srikar Reddy May 18 '20 at 13:18

1 Answers1

0
 you can use below function to enable GPS without open app. make sure before calling this 
 function below permissions granted.


<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> 


private fun settingsrequest() {

    var googleApiClient = GoogleApiClient.Builder(this)
        .addApi(LocationServices.API).build()
    googleApiClient.connect()

    val locationRequest = LocationRequest.create()
    locationRequest.priority = LocationRequest.PRIORITY_HIGH_ACCURACY
    locationRequest.interval = 30 * 1000
    locationRequest.fastestInterval = 5 * 1000
    val builder = LocationSettingsRequest.Builder()
        .addLocationRequest(locationRequest)
    builder.setAlwaysShow(true) //this is the key ingredient
    val result =
        LocationServices.SettingsApi.checkLocationSettings(googleApiClient, builder.build())
    result.setResultCallback { result ->
        val status = result.status
        val state = result.locationSettingsStates
        when (status.statusCode) {
            LocationSettingsStatusCodes.SUCCESS -> {
               --> here gps on successfully 
            }
            LocationSettingsStatusCodes.RESOLUTION_REQUIRED ->
                // Location settings are not satisfied. But could be fixed by showing 
                 // the user
                // a dialog.
                try {
                    // Show the dialog by calling startResolutionForResult(),
                    // and check the result in onActivityResult().
                    status.startResolutionForResult(this, GPSDIALOG)
                } catch (e: IntentSender.SendIntentException) {
                    // Ignore the error.
                }
            LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE -> {
            }
        }// All location settings are satisfied. The client can initialize location
        // requests here.
        // Location settings are not satisfied. However, we have no way to fix the
        // settings so we won't show the dialog.
    }
}
Mayur Dabhi
  • 3,607
  • 2
  • 14
  • 25