Pular para o conteúdo

Mais visitados

Visitados recentemente

navigation

Establishing In-app Billing Products for Sale

Before publishing your In-app Billing app, you must define the product list of digital goods available for purchase in the Google Play Console.

Specify in-app products in Google Play

From the Play Console, you can define product information for in-app products and associate a product list with your app.

To add new in-app products to your product list:

  1. Build a signed APK file for your In-app Billing app. To learn how to build and sign your APK, see Prepare for Release.
  2. In the Play Console, open the app entry that you created earlier.
  3. Click on the App releases in the left-hand menu and create a release to upload your APK. You can use any channel you want, and you can start with Beta or Alpha if you don't want to publish the app now. Check our support page for details on how to Prepare & rollout releases.
  4. Navigate to the uploaded app listing, and click on In-app Products.
  5. Click on the option to add a new product, then complete the form to specify the product information such as the item's unique product ID, also called its SKU, as well as a description, price, and country availability. Note down the product ID as you may need this information to query purchase details in your app later. To see a list of the products used by the TrivialDrive_v2 sample, review the UiDelegatesFactory class.

Note: The In-app Billing service only supports managed in-app products, so make sure that you specify that the purchase type is 'Managed' when you add new items to your product list in the Play Console.

  1. Once you have completed the form, activate the product so that your app can purchase it.

Query items available for purchase

You can query Google Play to programmatically retrieve details of the in-app products that are associated with your app, such as a product's price, title, description, and type. This query 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 a query, you must specify the product IDs for the products explicitly. You can manually find the product IDs from the Play Console by opening the In-app Products menu for your app. The product IDs are listed under the column labeled Name/ID.

To retrieve the product details, call the asynchronous querySkuDetailsAsync() method and pass it a reference to a listener that implements the SkuDetailsResponseListener interface and a reference to a SkuDetailsParams object with a SKU list and item type. The following code shows how you can integrate this method into the BillingManager class:

public void querySkuDetailsAsync(SkuDetailParams params, final SkuDetailsResponseListener listener) {
    // Create a runnable from the request to use inside the connection retry policy.
    Runnable queryRequest = new Runnable() {
        @Override
        public void run() {
            // Create the SkuDetailParams object
            SkuDetailsParams.Builder params = SkuDetailsParams.newBuilder();
            params.setSkusList(skuList).setType(itemType);
            // Run the query asynchronously.
            mBillingClient.querySkuDetailsAsync(params.build(), new SkuDetailsResponseListener() {
                        @Override
                        public void onSkuDetailsResponse(SkuDetailsResult result) {
                            listener.onSkuDetailsResponse(result);
                        }
                    });
        }
    };

    executeServiceRequest(queryRequest);
}

You must implement a retry policy to handle possible interruptions to the connection between the Play Billing Library client and the In-app Billing service. You can add a generic retry mechanism to the BillingManager class that accepts the Runnable object from the previous code sample as follows:

private void executeServiceRequest(Runnable runnable) {
    if (mIsServiceConnected) {
        runnable.run();
    } else {
        // If the billing service disconnects, try to reconnect once.
        startServiceConnection(runnable);
    }
}

If the asynchronous query is successful, the query results are stored in a List of SkuDetails objects that is passed back to the listener. The following sample code shows how you can call the querySkuDetailsAsync() method in the BillingManager class from your UI and pass it a reference to an object that implements the SkuDetailsResponseListener interface:

private void addSkuRows(final List<SkuRowData> inList, List<String> skusList,
        final @SkuType String billingType, final Runnable executeWhenFinished) {

    mBillingProvider.getBillingManager().querySkuDetailsAsync(billingType, skusList,
            new SkuDetailsResponseListener() {
                @Override
                public void onSkuDetailsResponse(@BillingResponse int responseCode,
                                                                         List<SkuDetails> skuDetailsList) {

                    if (result.getResponseCode() != BillingResponse.OK) {
                        // Handle any error responses.
                    } else if (skuDetailsList != null
                            && skuDetailsList.size() > 0) {
                            // Traverse through the list of SKUs inside SkuDetailsList.
                            ...
                }
            });
}
Este site usa cookies para armazenar suas preferências quanto às opções de idioma e exibição específicas ao site.

Receba as dicas e notícias mais recentes para desenvolvedores Android que ajudarão você a ter sucesso no Google Play.

* Campos obrigatórios

Uhu!

Siga o Google Developers no WeChat

Navegar neste site em ?

Você solicitou uma página em , mas sua preferência de idioma para este site está definida como .

Quer alterar sua preferência de idioma e navegar neste site em ? Para alterar a preferência de idioma depois, use o menu de idiomas na parte inferior de cada página.

É preciso ter o nível de API ou superior para esta aula

Este documento está oculto porque o nível de API selecionado para a documentação é o . Para alterar o nível, clique no seletor acima da barra de navegação esquerda.

Para mais informações sobre como especificar o nível de API que o seu app precisa, leia o artigo Compatibilidade com diferentes versões de plataforma.

Take a short survey?
Help us improve the Android developer experience. (April 2018 — Developer Survey)