GooglePlayServicesUtil बनाम GoogleApiAvucation


102

मैं अपने Android ऐप में Google Play सेवा का उपयोग करने का प्रयास कर रहा हूं। जैसा कि Google दस्तावेज़ कहता है, हमें यह जांचने की आवश्यकता है कि क्या Google API का उपयोग करने से पहले उपलब्ध है। मैंने इसे जांचने का कोई तरीका खोजा है। यह है जो मुझे मिला:

private boolean checkPlayServices() {
int resultCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
if (resultCode != ConnectionResult.SUCCESS) {
    if (GooglePlayServicesUtil.isUserRecoverableError(resultCode)) {
        GooglePlayServicesUtil.getErrorDialog(resultCode, this,
                PLAY_SERVICES_RESOLUTION_REQUEST).show();
    } else {
        Log.i(TAG, "This device is not supported.");
        finish();
    }
    return false;
}
return true;
}

लेकिन जब मैं Google Api GooglePlayServicesUtil पृष्ठ पर जाता हूं, तो https://developers.google.com/android/reference/com/google/android/gms/common/GooglePlayServicesUtil

मुझे लगता है कि सभी कार्य पदावनत हैं । उदाहरण के लिए, विधि

GooglePlayServicesUtil.isGooglePlayServicesAvailable (पदावनत)

और Google उपयोग करने की अनुशंसा करता है:

GoogleApiAvucation.isGooglePlayServicesAvailable

हालाँकि, जब मैं GoogleApiAvucation.isGooglePlayServicesAvailable का उपयोग करने का प्रयास करता हूं, तो मुझे त्रुटि संदेश मिलता है:

यहां छवि विवरण दर्ज करें


मुझे GoogleAvAvucation कहां मिलेगा? मैं इसे नहीं ढूँढ सकता।
mcmillab

@ मममिल्लब +1। मैंने 8.1.0 से 8.4.0 तक अपग्रेड किया है और GooglePlayServicesUtilचला गया है (जो कि "मामूली" अपडेट के लिए एक बुरे अभ्यास की तरह लगता है) लेकिन मैं GoogleApiAvailabilityप्रतिस्थापन के रूप में उपयोग करना नहीं देख रहा हूं ।
Spaaarky21

जवाबों:


203

मैंने इसका हल ढूंढ लिया है। सभी में GoogleApiAvailability, सभी विधियाँ सार्वजनिक विधि हैं, जबकि GooglePlayServicesUtilसभी पद्धतियाँ स्थिर सार्वजनिक कार्य हैं।

तो GoogleAvAvAvavience का उपयोग करने के लिए, सही तरीका है:

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;
}

9
क्या है: PLAY_SERVICES_RESOLUTION_REQUEST
समन सत्तारी

12
private final static int PLAY_SERVICES_RESOLUTION_REQUEST = 9000;
फेर्रामोलिना

7
यह एक मनमाना पूर्णांक है। आप मूल्य बना सकते हैं।
टिम्मम्म

4
कुछ 9000 से अधिक के मूल्य को सेट करना पसंद करते हैं।
matthias_b_nz

12
यह संपूर्ण Google Play Services लाइब्रेरी डिज़ाइन एक संपूर्ण गड़बड़ है। यह सभी त्रुटिपूर्ण है इसके अलावा प्रलेखन की कमी के साथ इसका पालन करना कठिन है।
mr5

64

वर्ग GooglePlayServicesUtil उपयोग अब और नहीं किया जाना चाहिए!

यहां बताया गया है कि क्लास GoogleApiAvucation का उपयोग कैसे किया जा सकता है - जब उदाहरण के लिए GCM (या किसी अन्य Google सेवा) की आवश्यकता होती है:

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);
    }
}

अपडेट करें:

REQUEST_GOOGLE_PLAY_SERVICESमनमाने ढंग से नाम और मूल्य के साथ एक पूर्णांक स्थिर है, जिसे onActivityResult()विधि में संदर्भित किया जा सकता है ।

इसके अलावा, this.onActivityResult()उपरोक्त कोड में कॉल करना ठीक है (आप super.onActivityResult()दूसरी जगह भी कॉल करें )।


2
क्या आप उस स्रोत को इंगित कर सकते हैं जो दावा करता है कि "क्लास GooglePlayServicesUtil का उपयोग अब और नहीं किया जाना चाहिए!"। Google Play Services API इतना भ्रामक है।
लाचेजार

8
GoogleApiAvucation वर्ग से निरंतर लेने के लिए javadoc के शीर्ष पर पदावनत प्लस अनुशंसा के रूप में चिह्नित GooglePlayServicesUtil की सभी विधियाँ आपको बताने के लिए Google का तरीका है: अब कक्षा का उपयोग न करें । GOOGLE_PLAY_SERVICES_PACKAGEGooglePlayServicesUtil
अलेक्जेंडर फरबर

3
क्या होगा यदि डिवाइस में Google Play सेवाओं का एक पुराना संस्करण है जिसमें GoogleApiAvailabilityकक्षा मौजूद नहीं है? अगर हम एक सशर्त अभिव्यक्ति के अंदर भी कक्षा को वैधानिक रूप से संदर्भित करते हैं, तो क्या यह ऐप को क्रैश नहीं करेगा?
केविन क्रुमविडे

6
@ केविन क्रुमविडे, GoogleApiAvailabilityक्लाइंट लाइब्रेरी का हिस्सा है। इसलिए यह कोड ऐप में संकलित है => इस बारे में चिंता न करें।
विंडराइड

9
आपको onActivityResult () पर कॉल नहीं करना चाहिए। इसका मतलब बाहर से बुलाया जाना है, जब कोई अन्य गतिविधि परिणाम देती है।
यार

10

इसके स्थान पर आपको GoogleAAAAvavience का उपयोग करना होगा :

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

thisका प्रतिनिधित्व करता है context


9

यह सुनिश्चित करने के लिए डिवाइस की जांच करें कि उसमें Google Play Services एपीके है। यदि ऐसा नहीं होता है, तो एक संवाद प्रदर्शित करें जो उपयोगकर्ताओं को Google Play Store से एपीके डाउनलोड करने या डिवाइस की सिस्टम सेटिंग्स में सक्षम करने की अनुमति देता है।

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

मैंने इसे सभी स्थानों में उपयोग किए जाने वाले बेसएक्टिविटी क्लास में मज़े के रूप में जोड़ा है

    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
}

इसे इस तरह से उपयोग करें

    (activity as? BaseActivity)?.checkGooglePlayServices({
        // ok so start map
        initializeMap()
    },
        { msg, isResolved ->
            if (!isResolved)
                context?.show(msg)

        }
    )

या आप इसे अपनी इच्छानुसार कस्टमाइज़ कर सकते हैं।

हमारी साइट का प्रयोग करके, आप स्वीकार करते हैं कि आपने हमारी Cookie Policy और निजता नीति को पढ़ और समझा लिया है।
Licensed under cc by-sa 3.0 with attribution required.