简体   繁体   中英

How to correctly verify availability of google play services?

Google play services availability is verified in onResume :

override fun onResume() {
    super.onResume()
    verifyGooglePlayServicesAvailability()
}

The dialog from getErrorDialog is show if the check fails:

availability.getErrorDialog(this, status, RESOLVE_GOOGLE_PLAY_SERVICES_UNAVAILABILITY_REQUEST).show()

But for some reason the dialog is shown again one after the error is resolved. I could not find an example of how to implement the checks in the documentation. This is my approach:

private fun verifyGooglePlayServicesAvailability()
{
    val availability = GoogleApiAvailability.getInstance()
    val status = availability.isGooglePlayServicesAvailable(this)
    if (status != ConnectionResult.SUCCESS)
        availability.getErrorDialog(this, status, RESOLVE_GOOGLE_PLAY_SERVICES_UNAVAILABILITY_REQUEST).show()
}

This is because a Dialog takes foreground and puts the Activity in the background so your onResume is called multiple times. Consider moving it to your onCreate or if you need it in OnResume consider a flag to avoid checking twice if handled

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM