Tetap teratur dengan koleksi
Simpan dan kategorikan konten berdasarkan preferensi Anda.
Untuk mengakses
API yang didukung oleh layanan Google Play
di aplikasi Android Anda, Anda harus menggunakan objek klien API. Objek ini menangani
koneksi ke layanan Google Play, mengantrekan permintaan, dan mengeksekusinya secara
berurutan saat koneksi tersedia. Anda dapat membuat klien API baru sesuai kebutuhan,
karena klien ini tidak mahal untuk dibuat.
Untuk mengakses layanan yang tidak memerlukan otorisasi, buat instance objek klien layanan, dengan meneruskan objek Context atau Activity. Jika perlu, pengguna akan diminta untuk mengupgrade layanan Google Play sebelum
panggilan API dijalankan.
Cuplikan kode berikut menunjukkan cara mendapatkan lokasi terakhir perangkat yang diketahui menggunakan Fused Location Provider:
Kotlin
// Code required for requesting location permissions omitted for brevity.valclient=LocationServices.getFusedLocationProviderClient(this)// Get the last known location. In some rare situations, this can be null.client.lastLocation.addOnSuccessListener{location:Location? ->
location?.let{// Logic to handle location object.}}
Java
// Code required for requesting location permissions omitted for brevity.FusedLocationProviderClientclient=LocationServices.getFusedLocationProviderClient(this);// Get the last known location. In some rare situations, this can be null.client.getLastLocation().addOnSuccessListener(this,location->{if(location!=null){// Logic to handle location object.}});
Sebelum mengaktifkan fitur yang bergantung pada API layanan Google Play, periksa apakah API tersedia di perangkat dengan memanggil
checkApiAvailability().
Cuplikan kode berikut menunjukkan cara memeriksa apakah penyedia lokasi gabungan tersedia:
[[["Mudah dipahami","easyToUnderstand","thumb-up"],["Memecahkan masalah saya","solvedMyProblem","thumb-up"],["Lainnya","otherUp","thumb-up"]],[["Informasi yang saya butuhkan tidak ada","missingTheInformationINeed","thumb-down"],["Terlalu rumit/langkahnya terlalu banyak","tooComplicatedTooManySteps","thumb-down"],["Sudah usang","outOfDate","thumb-down"],["Masalah terjemahan","translationIssue","thumb-down"],["Masalah kode / contoh","samplesCodeIssue","thumb-down"],["Lainnya","otherDown","thumb-down"]],["Terakhir diperbarui pada 2025-09-10 UTC."],[[["\u003cp\u003eTo use Google Play services APIs, create API client objects which manage the connection and queue requests when offline.\u003c/p\u003e\n"],["\u003cp\u003eAccess services without authorization by obtaining the client object using the current \u003ccode\u003eContext\u003c/code\u003e or \u003ccode\u003eActivity\u003c/code\u003e.\u003c/p\u003e\n"],["\u003cp\u003eAccess services requiring authorization by signing in the user, requesting necessary permissions, and then getting the client object.\u003c/p\u003e\n"],["\u003cp\u003eCheck for API availability using \u003ccode\u003echeckApiAvailability()\u003c/code\u003e if you need to disable features when an API is unavailable, although API calls will fail gracefully if the API isn't present.\u003c/p\u003e\n"]]],["To use Google Play services APIs, create an API client object, which manages the connection and queues requests. For services not requiring authorization, obtain the client instance using `Context` or `Activity`; for example, the Fused Location Provider client. For services requiring authorization, sign the user in, request necessary permissions, and then get the client using `GoogleSignInAccount`. For example, the Fit API client to get the daily steps. Before making API calls check with `checkApiAvailability()`.\n"],null,[]]