Tag Archives: Android

Android In App Purchase Tutorial

Android In App Purchase

Android In App Purchase

To implement in app billing in android this example is quiet simple and easy to handle at first .. the things you have to do are:

  1. download the sample project code from the above link (Having Description of code and download link at bottom)
  2. in your android project where you want to implement in app billing, create package and place IMarketBillingService.aidl (you can find this and all files mention below in the project you downloaded in step 1)
  3. place following utility files in any package and correct import statements accordingly.
  4. place the public key (you can find it in developer console in the bottom section of edit profile) in the in line saying
    String base64EncodedPublicKey = "your public key here"
  5. Declare the following permission (outside the application tag), service and receiver (Inside the application tag) in your manifest like shown below(can also see manifest which is along the code for reference)
     //outside the application tag 
     <uses-permission android:name="" />
     // Inside the application tag
     <service android:name=".BillingService" />
    <receiver android:name=".BillingReceiver">
            <action android:name="" />
            <action android:name="" />
            <action android:name="" />            
  6. place the following code as mentioned with there places in your activity where purchase is being held.
    //at the starting of your onCreate()
    startService(new Intent(mContext, BillingService.class));
    //outside onCreate() Within class
     public Handler mTransactionHandler = new Handler(){
        public void handleMessage(android.os.Message msg) {
            Log.i(TAG, "Transaction complete");
            Log.i(TAG, "Transaction status: "+BillingHelper.latestPurchase.purchaseState);
            Log.i(TAG, "Item purchased is: "+BillingHelper.latestPurchase.productId);
                //code here which is to be performed after successful purchase
     //code to initiate a purchase... can be placed in onClickListener etc
            BillingHelper.requestPurchase(mContext, "android.test.purchased"); 
            // where android.test.purchased is test id for fake purchase, when you create
    // products through developer console you can set a code to pass the id(which is given on
    // developer console while creating a product) of the item which is selected for purchase
    // to initiate purchase of that item.
        } else {
            Log.i(TAG,"Can't purchase on this device");
             // Do Anything Heer to show user that purchase not possible on this device

Note: To do a test purchase you need to put the public key in as mentioned above secondly you need to upload the apk to the developer console(you can leave it uupublished and unactive) and thirdly you need a real android device(emulator wouldn’t work) having updated play store app.

Note: the account needed for in app purchase and described in all above discussion is not just simple publisher account its publisher account embedded with google merchant wallet account. The details can be found in link below.


Posted by on September 27, 2012 in Android Devalopement


Tags: , ,