2015年11月11日 星期三

Check if Google Play Service is available

Reference:

http://stackoverflow.com/questions/18737632/how-to-check-google-play-services-version
http://stackoverflow.com/questions/28479849/when-does-isgoogleplayservicesavailable-return-service-version-update-required
http://stackoverflow.com/questions/25989727/isgoogleplayservicesavailable-always-returns-2
http://www.4byte.cn/question/135275/googlemap-v2-crash-if-first-use-android.html

Code snippet:

public static void checkGooglePlayServiceAvailability(Activity activity){
        Log.d(TAG,"sdk gms versionCode: " + GooglePlayServicesUtil.GOOGLE_PLAY_SERVICES_VERSION_CODE);
        try{
            int deviceGmsVersion = activity.getPackageManager().getPackageInfo("com.google.android.gms",0).versionCode;
            Log.d(TAG,"" + "device gms versionCode: " + deviceGmsVersion);
        } catch(Exception e){
            e.printStackTrace();
        }

        int statusCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(activity);
        Log.e(TAG,"statuscode: " + statusCode);
        Log.e(TAG,"error string: " + GooglePlayServicesUtil.getErrorString(statusCode));

        if(statusCode != ConnectionResult.SUCCESS){
            if(GooglePlayServicesUtil.isUserRecoverableError(statusCode)){
                Dialog errorDialog = GooglePlayServicesUtil.getErrorDialog(statusCode, activity, 0);
                // If Google Play services can provide an error dialog
                if (errorDialog != null) {
                    errorDialog.show();
                }
            }
        }
    }

沒有留言: