6

From the google i/o 2015 I have learned that there is a new dialog in google play service where user doesnt need to exit the current app to turn on location. here is the image below that shows how it looks like:

enter image description here

now my question is, how can I implement this in my project? I have searched but didnt find any valid answer, please help!

Reyjohn
  • 2,654
  • 9
  • 37
  • 63
  • 2
    Use `SettingsApi` on in the Play Services SDK: https://developer.android.com/reference/com/google/android/gms/location/SettingsApi.html – CommonsWare Jun 03 '15 at 11:46
  • http://stackoverflow.com/questions/28759454/enabling-location-with-mode-high-accuracy-or-battery-saving-without-user-needing – Pararth Jun 03 '15 at 11:49

2 Answers2

6

Take a look at google service documetation for api's and you will find everything well documented. For your request i would suggest to use the LocationSettingsRequest.Builder to reach your goal.

I have found an example by Kai in stackoverflow: Link

Community
  • 1
  • 1
eddy white
  • 301
  • 2
  • 24
  • someone specifically looking for SettingsApi page, please refer [link](https://developers.google.com/android/reference/com/google/android/gms/location/SettingsApi) – PunitD Sep 11 '15 at 06:57
0

Using Kotlin

This solution is applicable for both Activity and Fragment by doing one following change:

For Activity resolvableApiException.startResolutionForResult(this@MainActivity, REQUEST_CHECK_SETTING)

For Fragment startIntentSenderForResult(resolvableApiException.resolution.intentSender, REQUEST_CHECK_SETTING, null, 0, 0,0,null)

By using LocationSettingsResponse this task can be achieved.

inside MainActivity.kt


    private fun checkLocationSetting()
        {
            locationRequest = LocationRequest.create()
            locationRequest.apply {
                priority=LocationRequest.PRIORITY_HIGH_ACCURACY
                interval = 5000
                fastestInterval = 2000
            }
    
            val builder = LocationSettingsRequest.Builder()
                .addLocationRequest(locationRequest)
            builder.setAlwaysShow(true)
    
            val result: Task<LocationSettingsResponse> = LocationServices.getSettingsClient(applicationContext)
                .checkLocationSettings(builder.build())
    
            result.addOnCompleteListener {
                try{
                    val response: LocationSettingsResponse = it.getResult(ApiException::class.java)
                    Toast.makeText(this@MainActivity, "GPS is On", Toast.LENGTH_SHORT).show()
                    Log.d(TAG, "checkSetting: GPS On")
                }catch(e:ApiException){
    
                    when(e.statusCode){
                        LocationSettingsStatusCodes.RESOLUTION_REQUIRED ->{
                            val resolvableApiException = e as ResolvableApiException
// for fragment change below line to: startIntentSenderForResult(resolvableApiException.resolution.intentSender, REQUEST_CHECK_SETTING, null, 0, 0,0,null)
                            resolvableApiException.startResolutionForResult(this@MainActivity, REQUEST_CHECK_SETTING)
                            Log.d(TAG, "checkSetting: RESOLUTION_REQUIRED")
                        }
    
                        LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE -> {
                            // USER DEVICE DOES NOT HAVE LOCATION OPTION
                        }
                    }
                }
            }
        }

onActivityResult

 override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        when(requestCode)
        {
            REQUEST_CHECK_SETTING ->{
                when(resultCode){
                    Activity.RESULT_OK->{
                        Toast.makeText(this@MainActivity, "GPS is Turned on", Toast.LENGTH_SHORT).show()
                    }
                    Activity.RESULT_CANCELED ->{
                        Toast.makeText(this@MainActivity, "GPS is Required to use this app", Toast.LENGTH_SHORT).show()
                    }
                }
            }
        }
    }

Link to complete code MainActivity.kt

Output:

Link to complete code MainActivity.kt

Shoaib Kakal
  • 1,090
  • 2
  • 16
  • 32