Android In-app purchases template.

Get it on Google Play

Settings

  • Copy AIDL file to your project and build project:

    aidl

  • Copy helper classes to you project:

    iab2

  • Add permission to AndroidManifest.xml :

    <uses-permission android:name="com.android.vending.BILLING" />
  • Add license key to strings.xml :

     <resources>
      
      .....
      
        <string name="base64_app_license_key">your key here</string>
     </resources>

    lic

  • Add subscription periods to Activity:

      // SKU for our subscription
      static final String SKU_SUB_MONTHLY = "monthly_subscribe_demo";
      static final String SKU_SUB_SIX_MONTHLY = "halfyearly_subscribe_demo";
      static final String SKU_SUB_YEARLY = "yearly_subscribe_demo";

    inapp

  • Copy helper methods to Activity:

      private void setupInAppBilling() {
         .......
         .......
      }
      private void destroyInAppBilling() {
        .......
        .......
      }
       private void showSubscriptionDialog(){
         .......
         .......
       }

    and call their in Activity methods:

       @Override
       protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            mSubsButton = (Button) findViewById(R.id.button_subscribe);
            mSubsButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    //for example calling chooser dialog here
                    showSubscriptionDialog();
                }
            });
            setupInAppBilling();
        }
       @Override
       protected void onDestroy() {
           super.onDestroy();
           destroyInAppBilling();
       }
       ```
  • Process the response code of purchase result in onActivityResult:

       @Override
       protected void onActivityResult(int requestCode, int resultCode, Intent data) {
           if (mIabHelper == null) return;
           // Pass on the activity result to the helper for handling
           if (!mIabHelper.handleActivityResult(requestCode, resultCode, data)) {
              // not handled, so handle it ourselves (here's where you'd
              // perform any handling of activity results not related to in-app
              // billing...
              super.onActivityResult(requestCode, resultCode, data);
           }
       }
      ```