GooglePlayServicesUtil vs GoogleApiAvailability

后端 未结 6 569
慢半拍i
慢半拍i 2020-11-28 20:25

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

6条回答
  •  谎友^
    谎友^ (楼主)
    2020-11-28 21:00

    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.

提交回复
热议问题