Initiate a Connection with Google Play
Creating a ServiceConnection
Your application must have a ServiceConnection
to facilitate messaging between your application and Google Play. At a minimum, your application must do the following:
- Bind to
IInAppBillingService
. - Send billing requests (as IPC method calls) to the Google Play application.
- Handle the synchronous response messages that are returned with each billing request.
Binding to IInAppBillingService
To establish a connection with the In-app Billing service on Google Play, implement a ServiceConnection
to bind your activity to IInAppBillingService
. Override the onServiceDisconnected
and onServiceConnected
methods to get a reference to the IInAppBillingService
instance after a connection has been established.
IInAppBillingService mService; ServiceConnection mServiceConn = new ServiceConnection() { @Override public void onServiceDisconnected(ComponentName name) { mService = null; } @Override public void onServiceConnected(ComponentName name, IBinder service) { mService = IInAppBillingService.Stub.asInterface(service); } };
In your activity’s onCreate
method, perform the binding by calling the bindService
method. Pass the method anIntent
that references the In-app Billing service and an instance of the ServiceConnection
that you created.
@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); bindService(new Intent("com.android.vending.billing.InAppBillingService.BIND"), mServiceConn, Context.BIND_AUTO_CREATE);
IabHelper
instance in your activity's
onCreate
method. In the constructor, pass in the
Context
for the activity, along with a string containing the public license key that was generated earlier by the Google Play Developer Console.
IabHelper mHelper; @Override public void onCreate(Bundle savedInstanceState) { // ... String base64EncodedPublicKey; // compute your public key and store it in base64EncodedPublicKey mHelper = new IabHelper(this, base64EncodedPublicKey); }
2、Next, perform the service binding by calling the
startSetup
method on theIabHelper
instance that you created. Pass the method anOnIabSetupFinishedListener
instance, which is called once theIabHelper
completes the asynchronous setup operation. As part of the setup process, theIabHelper
also checks if the In-app Billing Version 3 API is supported by Google Play. If the API version is not supported, or if an error occured while establishing the service binding, the listener is notified and passed anIabResult
object with the error message.mHelper.startSetup(new IabHelper.OnIabSetupFinishedListener() { public void onIabSetupFinished(IabResult result) { if (!result.isSuccess()) { // Oh noes, there was a problem. Log.d(TAG, "Problem setting up In-app Billing: " + result); } // Hooray, IAB is fully set up! } });
Query Items Available for Purchase
1、You can query Google Play to programmatically retrieve details of the in-app products that are associated with your application (such as the product’s price, title, description, and type). This is useful, for example, when you want to display a listing of unowned items that are still available for purchase to users.
Note: When making the query, you will need to specify the product IDs for the products explicitly. You can manually find the product IDs from the Developer Console by opening the In-app Products tab for your application. The product IDs are listed under the column labeled Name/ID.
To retrieve the product details, call
queryInventoryAsync(boolean, List, QueryInventoryFinishedListener)
on your IabHelper instance.
- The first input argument indicates whether product details should be retrieved (should be set to
true
). - The
List
argument consists of one or more product IDs (also called SKUs) for the products that you want to query. - Finally, the
QueryInventoryFinishedListener
argument specifies a listener is notified when the query operation has completed and handles the query response.
The following code shows how you can retrieve the details for two products with IDs SKU_APPLE
and SKU_BANANA
that you previously defined in the Developer Console.
List additionalSkuList = new List(); additionalSkuList.add(SKU_APPLE); additionalSkuList.add(SKU_BANANA); mHelper.queryInventoryAsync(true, additionalSkuList, mQueryFinishedListener);