GooglePlayServicesUtil vs GoogleApiAvailability

后端 未结 6 537
慢半拍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.

    0 讨论(0)
  • 2020-11-28 21:00

    Just putting a .getInstance() in between the two methods would do the game.

    0 讨论(0)
  • 2020-11-28 21:06

    You will have to use GoogleApiAvailability instead:

    GoogleApiAvailability googleApiAvailability = GoogleApiAvailability.getInstance(); 
    int errorCode = googleApiAvailability.isGooglePlayServicesAvailable(this);
    

    this represents the context.

    0 讨论(0)
  • 2020-11-28 21:07

    The class GooglePlayServicesUtil shouldn't be used anymore!

    Here is how the class GoogleApiAvailability can be used instead - when for example GCM (or any other Google service) is needed:

    public static final int REQUEST_GOOGLE_PLAY_SERVICES = 1972;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (savedInstanceState == null) {
            startRegistrationService();
        }
    }
    
    private void startRegistrationService() {
        GoogleApiAvailability api = GoogleApiAvailability.getInstance();
        int code = api.isGooglePlayServicesAvailable(this);
        if (code == ConnectionResult.SUCCESS) {
            onActivityResult(REQUEST_GOOGLE_PLAY_SERVICES, Activity.RESULT_OK, null);
        } else if (api.isUserResolvableError(code) &&
            api.showErrorDialogFragment(this, code, REQUEST_GOOGLE_PLAY_SERVICES)) {
            // wait for onActivityResult call (see below)
        } else {
            Toast.makeText(this, api.getErrorString(code), Toast.LENGTH_LONG).show();
        }
    }
    
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch(requestCode) {
            case REQUEST_GOOGLE_PLAY_SERVICES:
                if (resultCode == Activity.RESULT_OK) {
                    Intent i = new Intent(this, RegistrationService.class); 
                    startService(i); // OK, init GCM
                }
                break;
    
            default:
                super.onActivityResult(requestCode, resultCode, data);
        }
    }
    

    UPDATE:

    REQUEST_GOOGLE_PLAY_SERVICES is an integer constant with arbitrary name and value, which can be referred to in the onActivityResult() method.

    Also, calling this.onActivityResult() in the above code is okay (you also call super.onActivityResult() in the other place).

    0 讨论(0)
  • 2020-11-28 21:15

    I have found the solution. In the GoogleApiAvailability, all methods are public method, while in GooglePlayServicesUtil all methods are static public function.

    So to use GoogleApiAvailability, the right way is:

    private boolean checkPlayServices() {
        GoogleApiAvailability googleAPI = GoogleApiAvailability.getInstance();
        int result = googleAPI.isGooglePlayServicesAvailable(this);
        if(result != ConnectionResult.SUCCESS) {
            if(googleAPI.isUserResolvableError(result)) {
                googleAPI.getErrorDialog(this, result,
                        PLAY_SERVICES_RESOLUTION_REQUEST).show();
            }
    
            return false;
        }
    
        return true;
    }
    
    0 讨论(0)
  • 2020-11-28 21:25

    Check the device to make sure it has the Google Play Services APK. If it doesn't, display a dialog that allows users to download the APK from the Google Play Store or enable it in the device's system settings.

    public static boolean checkPlayServices(Activity activity) {
        final int PLAY_SERVICES_RESOLUTION_REQUEST = 9000;
        GoogleApiAvailability apiAvailability = GoogleApiAvailability.getInstance();
        int resultCode = apiAvailability.isGooglePlayServicesAvailable(activity);
        if (resultCode != ConnectionResult.SUCCESS) {
            if (apiAvailability.isUserResolvableError(resultCode)) {
                apiAvailability.getErrorDialog(activity, resultCode, PLAY_SERVICES_RESOLUTION_REQUEST)
                        .show();
            } else {
                Logger.logE(TAG, "This device is not supported.");
            }
            return false;
        }
        return true;
    }
    
    0 讨论(0)
提交回复
热议问题