GooglePlayServicesUtil กับ GoogleApiAvailability


102

ฉันพยายามใช้บริการ Google Play ในแอป Android ของฉัน ตามที่ 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 แนะนำให้ใช้:

GoogleApiAvailability.isGooglePlayServicesAvailable

อย่างไรก็ตามเมื่อฉันพยายามใช้ GoogleApiAvailability.isGooglePlayServicesAvailable ฉันได้รับข้อความแสดงข้อผิดพลาด:

ใส่คำอธิบายภาพที่นี่


ฉันจะหา GoogleApiAvailability ได้ที่ไหน ฉันหาไม่เจอ
mcmillab

@mcmillab +1. ฉันอัปเกรดจาก 8.1.0 เป็น 8.4.0 และGooglePlayServicesUtilหายไปแล้ว (ซึ่งดูเหมือนเป็นการปฏิบัติที่ไม่ดีสำหรับการอัปเดต "เล็กน้อย") แต่ฉันไม่เห็นว่าGoogleApiAvailabilityจะใช้ทดแทนได้
spaaarky21

เมื่ออัปเดตเป็น Firebase ให้ชำระเงิน: etivy.com/…
Dawid Drozd

คำตอบ:


203

ฉันได้พบทางออกแล้ว ในGoogleApiAvailabilityวิธีการทั้งหมดเป็นวิธีสาธารณะในขณะที่GooglePlayServicesUtilทุกวิธีเป็นฟังก์ชันสาธารณะแบบคงที่

ดังนั้นในการใช้ GoogleApiAvailability วิธีที่ถูกต้องคือ:

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

7
มันเป็นจำนวนเต็มโดยพลการ คุณสามารถสร้างมูลค่าได้
Timmmm

4
บางคนชอบตั้งค่าให้มากกว่า 9000
matthias_b_nz

12
การออกแบบไลบรารีบริการ Google Play ทั้งหมดนี้เป็นระเบียบ ล้วนมีข้อบกพร่องและยากที่จะปฏิบัติตามนอกจากนี้ยังขาดเอกสารประกอบ
mr5

64

ไม่ควรใช้คลาสGooglePlayServicesUtilอีกต่อไป!

ต่อไปนี้เป็นวิธีใช้คลาสGoogleApiAvailabilityแทน - เมื่อจำเป็นต้องใช้เช่น 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 สับสนมาก
Lachezar

8
วิธีการทั้งหมดในGooglePlayServicesUtilทำเครื่องหมายว่าเลิกใช้แล้วพร้อมคำแนะนำที่ด้านบนของ javadoc เพื่อรับGOOGLE_PLAY_SERVICES_PACKAGEค่าคงที่จากคลาสGoogleApiAvailabilityเป็นวิธีที่ Google จะบอกคุณว่าอย่าใช้GooglePlayServicesUtilคลาสอีกต่อไป
Alexander Farber

3
จะเกิดอะไรขึ้นหากอุปกรณ์มีบริการ Google Play เวอร์ชันเก่าซึ่งGoogleApiAvailabilityไม่มีคลาสนี้อยู่ หากเราอ้างอิงคลาสแบบคงที่แม้จะอยู่ในนิพจน์เงื่อนไขแอปจะไม่ทำให้แอปเสียหายหรือไม่?
Kevin Krumwiede

6
@Kevin Krumwiede GoogleApiAvailabilityเป็นส่วนหนึ่งของไลบรารีไคลเอนต์ ดังนั้นโค้ดจึงถูกรวบรวมไว้ในแอพ => ไม่ต้องกังวลเรื่องนั้น
WindRider

9
คุณไม่ควรเรียก onActivityResult () หมายถึงการเรียกจากภายนอกเมื่อกิจกรรมอื่นส่งคืนผลลัพธ์
Yar

10

คุณจะต้องใช้GoogleApiAvailabilityแทน:

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

thisแสดงถึงcontext.


9

ตรวจสอบอุปกรณ์ว่ามี Google Play Services APK หากไม่เป็นเช่นนั้นให้แสดงกล่องโต้ตอบที่อนุญาตให้ผู้ใช้ดาวน์โหลด APK จาก 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

ฉันได้เพิ่มสิ่งนี้เป็นความสนุกในคลาส BaseActivity เพื่อใช้ในทุกสถานที่

    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)

        }
    )

หรือคุณสามารถปรับแต่งได้ตามที่คุณต้องการ

โดยการใช้ไซต์ของเรา หมายความว่าคุณได้อ่านและทำความเข้าใจนโยบายคุกกี้และนโยบายความเป็นส่วนตัวของเราแล้ว
Licensed under cc by-sa 3.0 with attribution required.