Prepare your application.

Source: Internet
Author: User
Tags key string android sdk manager

Get ready for your in-app purchase

Before you start using the In-app billing service, you need to add a library containing In-app billing Version 3 APIs to your Android project. You'll also need to set permissions for your app and Google Play communications. In addition, you need to establish a stable connection between your app and Google Play. Finally, confirm that Google Play supports the In-app billing API version that your app uses

Download the sample program

In this training session, you will use a TrivialDrive reference implementation called the example program for In-app billing Version 3. In this example, there are some handy classes that can quickly set up In-app billing services, encode and decode data types, and process In-app billing requests in your program's main thread. Note 1

To download the example program:

    1. Open Android SDK Manager.
    2. In the SDK Manager, expand the Extras section.
    3. Choose Google Play billing Library.
    4. Click Install Packages to complete the download.

This example file will be installed <sdk>/extras/google/play_billing/ in the. (Translator Note: It is on your computer under the SDK directory)

Adding apps to the console

On the Google Play Developer console, you can post your app with In-app Billing and manage a variety of digital goods that can be sold from your app. When you create a new app in the developer console, the console automatically generates a public key (license key) for your app. You will need to use this public key to establish a trustworthy connection between your app and Google Play server. This public key is generated when the app is created and does not have to be generated again at a later time when you update your program's APK file.

Add your app to the developer console:

    1. Open the Google Play Developer console and login, if you have not previously registered, you need to register a new developer account first. You also need a Google Wallet business account in order to sell your in-store purchases.
    2. If you haven't logged in to the latest version, click Try the new design to get to the developer Console preview.
    3. Add a new app at the all applications tab.
      1. Click Add New Application.
      2. Enter a name for your new In-app billing app.
      3. Click Prepare Store Listing.
    4. On the Services & APIs tab, find the public key generated by Google Play for your app and note that the BASE64 string will be used in your program for a while.

Your app should now appear in the app list in the developer console.

Add In-app Billing Library file

In order to use the In-app billing Version 3 feature, you must IInAppBillingService.aidl add the file to your project. This Android Interface definition Language (aidl) file defines the interfaces available for the Google Play service.

你可以在提供的例子程序里找到这个IInAppBillingService.aidl文件。根据你是创建一个新的应用还是修改现有的应用,跟随下面的指导把In-app Billing库添加到你的工程中。

New Project

Add In-app Billing Version 3 Library to your new In-app billing project:

    1. TrivialDriveCopy the example program files to your project.
    2. Modify the copied package name to the package name you used for your project. In Eclipse, you can use a shortcut: Right-click the package name and select Refactor > Rename.
    3. Open the AndroidManifest.xml file and change the package Name property value to the package name of your project.
    4. See the situation to fix the import statement so that your project can be compiled successfully. In Eclipse, you can use this shortcut: Press Ctrl+shift+o in the wrong file . (Translator Note: The Guide Package shortcut key, the earth people all know ~ ~)
    5. Modify the example code to create your own app. Remember to make a copy of the Base64 public key for your program generated by the developer console to your MainActivity.java .
Existing projects

Add In-app Billing Version 3 Library to your existing In-app billing project:

    1. 把 IInAppBillingService.aidl文件复制到你的Android Project.
      • If you are using eclipse: Bring the IInAppBillingService.aidl file into the project /src directory.
      • If you are developing a non-eclipse environment: Create the following directory /src/com/android/vending/billing and IInAppBillingService.aidl copy the files to this directory.
    2. Build your program. In your project./gen目录下应该可以看到一个生成的文件IInAppBillingService.java
    3. TrivialDriveAdd the auxiliary class from the/util directory in the example to your project. Remember to change the name of the package declared in these files so that your project can be compiled successfully.

Now your project should already contain the In-app billing Version 3 library.

Set up a billing license

Your program should have a license to send a request to Google Play's purchase service and receive a response. To add the necessary licenses to your app, add the following line of permissions to your manifest fileAndroidManifest.xml: 

<android:name= "Com.android.vending.BILLING"/>

Initializing a Google Play connection

In order to send a request to In-app billing and receive a response, you must bind your activity to Google Play's In-app billing service. In the example, you have provided a convenient class to handle the binding service, so you do not have to manage the network connection directly.

To set up synchronous communication with Google Play, create an instance in the OnCreate method of your program activity IabHelper . The Iabhelper constructor method Context also has a public key string that was previously generated in the developer console.

Security Advice: It is strongly recommended that you do not write the public key into the code as is. Alternatively, before passing the public key to the constructor, you can dynamically construct the full public key with a substring, or you can get it from an encrypted store. Note 3 This can be effective in preventing some intentioned third parties from modifying the public key in your apk file.

iabhelper mhelper; @Override  Public void onCreate (Bundle savedinstancestate) {   //  ...    String base64encodedpublickey;       // compute your Public key and store it in Base64encodedpublickey   New Iabhelper (this, Base64encodedpublickey);}

Next, call the Startsetup method in the   Iabhelper instance you created to perform the service binding. In the method, pass a   Oniabsetupfinishedlistener , this listener will be in   iabhelper Called once when the asynchronous setting is complete. &NBSP in the setup process; Iabhelper   also detects if Google Play supports In-app version 3 APIs, if this version of the API is not supported, or if there is an error in establishing a service binding, This listener will be notified and passed to a Iabresult object with an error message.

Mhelper.startsetup (new  Iabhelper.oniabsetupfinishedlistener () {   publicvoid  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!      }});

If this setting is completed successfully, you can now use this to mHelper communicate with the Google Play service. When your app starts, it's a good idea to check Google Play to see what you have in store. The details are in the later Query purchased Items section.

Important: Remember to unbind and In-app the billing service after you have exhausted your activity. If you do not unbind, this open service connection may affect the performance of your device. To unbind and free up your system resources, you can call IabHelper的 it when your Activity is destroyed.dispose 方法。

@Override  Public void OnDestroy () {   super. OnDestroy ();    if NULL ) Mhelper.dispose ();    NULL ;}
 
Note 1: Original in this training class, you'll use a Reference implementation for the In-app billing Version 3 API called the  trivialdrive  sample appli cation. The sample includes convenience classes to quickly set up the In-app billing service, marshal and unmarshal data types, an D Handle In-app billing requests from the main thread of your application.

Note 2: Original The convenience classes provided in the sample handles the binding to the In-app billing service, so you don ' t has to manage the network connection directly.

Note 3: Original Instead, you can construct the whole public License key string at runtime from substrings, or retrieve it fr Om an encrypted store, before passing it to the constructor.

Prepare your application

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.