คุณสามารถใช้ Install Referrer API ของ Google Play Store เพื่อดึงข้อมูลได้อย่างปลอดภัย
เนื้อหาที่แนะนำจาก Google Play ไลบรารีของไคลเอ็นต์ Play Setup Referrer API
เขียนในภาษาโปรแกรม Java และเป็น Wrapper สำหรับ Android
ไฟล์ Interface Definition Language (AIDL) ที่กำหนดอินเทอร์เฟซสำหรับ
ติดตั้งบริการผู้อ้างอิง คุณใช้ไคลเอ็นต์ Play Install Referrer API ได้
ไลบรารีเพื่อลดความซับซ้อนของขั้นตอนการพัฒนา
คู่มือนี้ครอบคลุมพื้นฐานการเรียกข้อมูลการอ้างอิงจาก Google
เล่นโดยใช้ไลบรารี Play install Referrer
ลบล้างonInstallReferrerSetupFinished()
เพื่อรับการแจ้งเตือนเมื่อการติดต่อกลับเสร็จสิ้น วิธีการนี้เรียกว่า
พร้อมด้วยโค้ดตอบกลับที่คุณต้องใช้ในการจัดการสถานะต่างๆ
OK
แสดงว่าเชื่อมต่อสำเร็จ กันและกัน
InstallReferrerResponse
ค่าคงที่มีไว้สำหรับข้อผิดพลาดประเภทต่างๆ
ลบล้างonInstallReferrerServiceDisconnected()
ในการจัดการการสูญเสียการเชื่อมต่อกับ Google Play สำหรับ
ตัวอย่างเช่น ไคลเอ็นต์ไลบรารีการอ้างอิงสำหรับการติดตั้ง Play อาจสูญเสียการเชื่อมต่อหาก
บริการ Play Store กำลังอัปเดตอยู่เบื้องหลัง ไคลเอ็นต์ไลบรารีจะต้องเรียก
เวลา
startConnection()
ในการรีสตาร์ทการเชื่อมต่อก่อนที่จะส่งคำขอเพิ่มเติม
โค้ดต่อไปนี้แสดงวิธีเริ่มต้นและทดสอบการเชื่อมต่อกับ
แอป Play Store
Kotlin
privatelateinitvarreferrerClient:InstallReferrerClientreferrerClient=InstallReferrerClient.newBuilder(this).build()referrerClient.startConnection(object:InstallReferrerStateListener{overridefunonInstallReferrerSetupFinished(responseCode:Int){when(responseCode){InstallReferrerResponse.OK->{// Connection established.}InstallReferrerResponse.FEATURE_NOT_SUPPORTED->{// API not available on the current Play Store app.}InstallReferrerResponse.SERVICE_UNAVAILABLE->{// Connection couldn't be established.}}}overridefunonInstallReferrerServiceDisconnected(){// Try to restart the connection on the next request to// Google Play by calling the startConnection() method.}})
Java
InstallReferrerClientreferrerClient;referrerClient=InstallReferrerClient.newBuilder(this).build();referrerClient.startConnection(newInstallReferrerStateListener(){@OverridepublicvoidonInstallReferrerSetupFinished(intresponseCode){switch(responseCode){caseInstallReferrerResponse.OK:// Connection established.break;caseInstallReferrerResponse.FEATURE_NOT_SUPPORTED:// API not available on the current Play Store app.break;caseInstallReferrerResponse.SERVICE_UNAVAILABLE:// Connection couldn't be established.break;}}@OverridepublicvoidonInstallReferrerServiceDisconnected(){// Try to restart the connection on the next request to// Google Play by calling the startConnection() method.}});
การรับ URL ที่มาของการติดตั้ง
หลังจากสร้างการเชื่อมต่อกับแอป Play Store แล้ว ให้ดูรายละเอียด
จาก URL ที่มาของการติดตั้งโดยทำตามขั้นตอนต่อไปนี้
[[["เข้าใจง่าย","easyToUnderstand","thumb-up"],["แก้ปัญหาของฉันได้","solvedMyProblem","thumb-up"],["อื่นๆ","otherUp","thumb-up"]],[["ไม่มีข้อมูลที่ฉันต้องการ","missingTheInformationINeed","thumb-down"],["ซับซ้อนเกินไป/มีหลายขั้นตอนมากเกินไป","tooComplicatedTooManySteps","thumb-down"],["ล้าสมัย","outOfDate","thumb-down"],["ปัญหาเกี่ยวกับการแปล","translationIssue","thumb-down"],["ตัวอย่าง/ปัญหาเกี่ยวกับโค้ด","samplesCodeIssue","thumb-down"],["อื่นๆ","otherDown","thumb-down"]],["อัปเดตล่าสุด 2025-07-27 UTC"],[],[],null,["# Play Install Referrer Library\n\nYou can use the Google Play Store's Install Referrer API to securely retrieve\nreferral content from Google Play. The Play Install Referrer API Client Library\nis written in the Java programming language and is a wrapper for the Android\nInterface Definition Language (AIDL) file that defines the interface to the\nInstall Referrer service. You can use the Play Install Referrer API Client\nLibrary to simplify your development process.\n\nThis guide covers the basics of retrieving referral information from Google\nPlay using the Play Install Referrer Library.\n| **Note:** If you are developing your app using a language other than the Kotlin programming language or the Java programming language, you can interact directly with the AIDL file by using the [Play Install Referrer\n| API](/google/play/installreferrer/igetinstallreferrerservice).\n\nUpdating your app's dependencies\n--------------------------------\n\nAdd the following line to the dependencies section of the `build.gradle` file\nfor your app: \n\n### Groovy\n\n```groovy\ndependencies {\n ...\n implementation \"com.android.installreferrer:installreferrer:2.2\"\n}\n```\n\n### Kotlin\n\n```kotlin\ndependencies {\n ...\n implementation(\"com.android.installreferrer:installreferrer:2.2\")\n}\n```\n\nConnecting to Google Play\n-------------------------\n\nBefore you can use the Play Install Referrer API Library, you must\nestablish a connection to the Play Store app using the following\nsteps:\n\n1. Call the [`newBuilder()`](/reference/com/android/installreferrer/api/InstallReferrerClient#newBuilder(android.content.Context)) method to create an instance of [`InstallReferrerClient`](/reference/com/android/installreferrer/api/InstallReferrerClient) class.\n2. Call the\n [`startConnection()`](/reference/com/android/installreferrer/api/InstallReferrerClient#startConnection(com.android.installreferrer.api.InstallReferrerStateListener))\n to establish a connection to Google Play.\n\n3. The [`startConnection()`](/reference/com/android/installreferrer/api/InstallReferrerClient#startConnection(com.android.installreferrer.api.InstallReferrerStateListener))\n method is asynchronous, so you must override\n [`InstallReferrerStateListener`](/reference/com/android/installreferrer/api/InstallReferrerStateListener)\n to receive a callback after\n [`startConnection()`](/reference/com/android/installreferrer/api/InstallReferrerClient#startConnection(com.android.installreferrer.api.InstallReferrerStateListener))\n completes.\n\n4. Override the [`onInstallReferrerSetupFinished()`](/reference/com/android/installreferrer/api/InstallReferrerStateListener#onInstallReferrerSetupFinished(int))\n method to be notified when the callback completes. This method is called\n with a response code that you must use to handle the different states.\n [`OK`](https://developer.android.com/reference/com/android/installreferrer/api/InstallReferrerClient.InstallReferrerResponse#OK)\n indicates that the connection was successful. Each of the other\n [`InstallReferrerResponse`](/reference/com/android/installreferrer/api/InstallReferrerClient.InstallReferrerResponse)\n constants are for different types of errors.\n\n5. Override the [`onInstallReferrerServiceDisconnected()`](/reference/com/android/installreferrer/api/InstallReferrerStateListener#oninstallreferrerservicedisconnected)\n method to handle lost connections to Google Play. For\n example, the Play Install Referrer Library client may lose the connection if the\n Play Store service is updating in the background. The library client must call\n the\n [`startConnection()`](/reference/com/android/installreferrer/api/InstallReferrerClient#startConnection(com.android.installreferrer.api.InstallReferrerStateListener))\n method to restart the connection before making further requests.\n\nThe following code demonstrates how to start and test a connection to the\nPlay Store app: \n\n### Kotlin\n\n```kotlin\nprivate lateinit var referrerClient: InstallReferrerClient\n\nreferrerClient = InstallReferrerClient.newBuilder(this).build()\nreferrerClient.startConnection(object : InstallReferrerStateListener {\n\n override fun onInstallReferrerSetupFinished(responseCode: Int) {\n when (responseCode) {\n InstallReferrerResponse.OK -\u003e {\n // Connection established.\n }\n InstallReferrerResponse.FEATURE_NOT_SUPPORTED -\u003e {\n // API not available on the current Play Store app.\n }\n InstallReferrerResponse.SERVICE_UNAVAILABLE -\u003e {\n // Connection couldn't be established.\n }\n }\n }\n\n override fun onInstallReferrerServiceDisconnected() {\n // Try to restart the connection on the next request to\n // Google Play by calling the startConnection() method.\n }\n})\n```\n\n### Java\n\n```java\nInstallReferrerClient referrerClient;\n\nreferrerClient = InstallReferrerClient.newBuilder(this).build();\nreferrerClient.startConnection(new InstallReferrerStateListener() {\n @Override\n public void onInstallReferrerSetupFinished(int responseCode) {\n switch (responseCode) {\n case InstallReferrerResponse.OK:\n // Connection established.\n break;\n case InstallReferrerResponse.FEATURE_NOT_SUPPORTED:\n // API not available on the current Play Store app.\n break;\n case InstallReferrerResponse.SERVICE_UNAVAILABLE:\n // Connection couldn't be established.\n break;\n }\n }\n\n @Override\n public void onInstallReferrerServiceDisconnected() {\n // Try to restart the connection on the next request to\n // Google Play by calling the startConnection() method.\n }\n});\n```\n\nGetting the install referrer\n----------------------------\n\nAfter you have established a connection to the Play Store app, get the details\nfrom the install referrer by completing the following steps:\n\n1. Use the synchronized\n [`getInstallReferrer()`](/reference/com/android/installreferrer/api/InstallReferrerClient#getInstallReferrer())\n method to return an instance of\n [`ReferrerDetails`](/reference/com/android/installreferrer/api/ReferrerDetails).\n\n2. Use the methods that the\n [`ReferrerDetails`](/reference/com/android/installreferrer/api/ReferrerDetails)\n class provides to get details about the install referrer.\n\nThe following code demonstrates how you can access the install referrer\ninformation: \n\n### Kotlin\n\n```kotlin\nval response: ReferrerDetails = referrerClient.installReferrer\nval referrerUrl: String = response.installReferrer\nval referrerClickTime: Long = response.referrerClickTimestampSeconds\nval appInstallTime: Long = response.installBeginTimestampSeconds\nval instantExperienceLaunched: Boolean = response.googlePlayInstantParam\n```\n\n### Java\n\n```java\nReferrerDetails response = referrerClient.getInstallReferrer();\nString referrerUrl = response.getInstallReferrer();\nlong referrerClickTime = response.getReferrerClickTimestampSeconds();\nlong appInstallTime = response.getInstallBeginTimestampSeconds();\nboolean instantExperienceLaunched = response.getGooglePlayInstantParam();\n```\n\n**Caution:** The install referrer information will be\navailable for 90 days and **won't change** unless the application is\nreinstalled. To avoid unnecessary API calls in your app, you should invoke the\nAPI **only once** during the first execution after install.\n\nClosing service connection\n--------------------------\n\nAfter getting referrer information, call the\n[`endConnection()`](/reference/com/android/installreferrer/api/InstallReferrerClient#endConnection())\nmethod on your\n[`InstallReferrerClient`](/reference/com/android/installreferrer/api/InstallReferrerClient)\ninstance to close the connection. Closing the connection will help you avoid\nleaks and performance problems.\n\nFor further information, refer to the [Play Install Referrer Library\nReference](/reference/com/android/installreferrer/packages)."]]