I am trying to use Google Play Service in my Android app. As Google document says, we need to check if the Google API is available before using it. I have searched some way
I have added this as fun in BaseActivity class to be used in all places
fun checkGooglePlayServices(okAction : ()-> Unit , errorAction: (msg:String, isResolved:Boolean)-> Unit){
val apiAvailability = GoogleApiAvailability.getInstance()
val resultCode = apiAvailability.isGooglePlayServicesAvailable(this)
if (resultCode != ConnectionResult.SUCCESS) {
if (apiAvailability.isUserResolvableError(resultCode)) {
apiAvailability.getErrorDialog(
this,
resultCode,
PLAY_SERVICES_RESOLUTION_REQUEST
).show()
// dialoe when click on ok should let user go to install/update play serices
errorAction("dialog is shown" , true)
} else {
"checkGooglePlayServices This device is not supported.".log(mTag)
errorAction("This device is not supported",false)
}
}else{
okAction()
}
}
companion object {
const val PLAY_SERVICES_RESOLUTION_REQUEST = 1425
}
use it like this
(activity as? BaseActivity)?.checkGooglePlayServices({
// ok so start map
initializeMap()
},
{ msg, isResolved ->
if (!isResolved)
context?.show(msg)
}
)
Or you can customize it as you want.