-
-
Save ShoaibKakal/4d07b4001e34cb770d3ee7cae294f7b0 to your computer and use it in GitHub Desktop.
Turn on GPS with dialog box.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.app.Activity | |
import android.content.Intent | |
import android.os.Bundle | |
import android.util.Log | |
import android.widget.Button | |
import android.widget.Toast | |
import androidx.appcompat.app.AppCompatActivity | |
import com.google.android.gms.common.api.* | |
import com.google.android.gms.location.* | |
import com.google.android.gms.tasks.Task | |
class MainActivity : AppCompatActivity() { | |
val TAG = "MainTag" | |
private lateinit var locationRequest: LocationRequest | |
private val REQUEST_CHECK_SETTING = 1001 | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
setContentView(R.layout.activity_main) | |
findViewById<Button>(R.id.gps_btn).setOnClickListener { | |
checkLocationSetting() | |
} | |
} | |
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 | |
resolvableApiException.startResolutionForResult( | |
this@MainActivity, | |
REQUEST_CHECK_SETTING | |
) | |
Log.d(TAG, "checkSetting: RESOLUTION_REQUIRED") | |
} | |
LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE -> { | |
// USER DEVICE DOES NOT HAVE LOCATION OPTION | |
} | |
} | |
} | |
} | |
} | |
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() | |
} | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks for the solution but am getting this error
java.lang.ClassCastException: com.google.android.gms.common.api.ApiException cannot be cast to com.google.android.gms.common.api.ResolvableApiException
because ResolvableApiException is a subclass of APIException. How can I solve this error and achieve the intended result?