In app purchase Android
In-app purchase Android Example:-
Sometimes your application requires buying subscription to use the application or your application is seeking the virtual goods inside the application. To achieve this functionality we have in-app purchases.The example will show how to use google in-app purchases to your android project.
Before making In-app purchase the application you need to check that you
have installed google billing services in your device or not.
To check perform below :-
1) Open android studio.
2) Select tools —> android —> SDK
manager.
3) Click on Launch stand alone SDK.
4) Click on extra and select google
Billing services.
5) Install the package if you don’t have it.
For making in-app purchase application
you need IInAppBillingServie.aidl which is automatically downloaded in the computer
when you download billing services.
IInAppBillingService.aidl is an Android
Interface Definition Language (AIDL) file that defines the interface to the
In-app Billing Version 3 service.
To add the AIDL
to your project:
To create an AIDL directory in the studio
:- Right-click on the app then select
app—>New—>Folder—>AIDL Folder.
Copy the IInAppBillingService.aidl file to your
project.
<sdk>/extras/google/play_billing/,
copy the IInAppBillingService.aidl
file, and paste it into the com.android.vending.billing
package in your project.
Add permission to your manifest:-
<uses-permission android:name="com.android.vending.BILLING" />
Add Util package to your Project:-
For that open
<sdk>/extras/google/play_billing/sample/TrivialDrive/src/com/example/android/trivialdrivesample/util.
Copy that package to your application.
Step 1) Create xml file.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:showIn="@layout/activity_main"
android:gravity="center"
android:background="@color/colorPrimary">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_testing"/>
<Button
android:id="@+id/purchase"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Buy Item!" />
</LinearLayout>
Step 2) Create a main.java file.
package inapp.example.test;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
import com.android.vending.billing.IInAppBillingService;
import inapp.example.test.util.IabHelper;
import inapp.example.test.util.IabResult;
import inapp.example.test.util.Inventory;
import inapp.example.test.util.Purchase;
public class MainActivity extends AppCompatActivity {
//Enter your product Id
static final String SKU_PREMIUM = "product_p1";
//Enter your base 64 here
String base64EncodedPublicKey = "";
IabHelper mHelper;
IabHelper.QueryInventoryFinishedListener onQueryInventoryFinishedListener = new IabHelper.QueryInventoryFinishedListener() {
@Override
public void onQueryInventoryFinished(IabResult result, Inventory inv) {
if (result.isFailure())
return;
Purchase purchase = inv.getPurchase(SKU_PREMIUM);
if (purchase != null)
Toast.makeText(MainActivity.this, "Purchase successful", Toast.LENGTH_SHORT).show();
}
};
IabHelper.OnIabPurchaseFinishedListener mPurchaseFinishedListener = new IabHelper.OnIabPurchaseFinishedListener() {
@Override
public void onIabPurchaseFinished(IabResult result, Purchase info)
{
if (mHelper == null)
return;
else {
if (result.isSuccess())
Toast.makeText(MainActivity.this, "Sucessfully Purchase", Toast.LENGTH_SHORT).show();
}
}
};
private IInAppBillingService mService;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mHelper = new IabHelper(this, base64EncodedPublicKey);
mHelper.startSetup(new IabHelper.OnIabSetupFinishedListener()
{
@Override
public void onIabSetupFinished(IabResult result)
{
if (!result.isSuccess()) {
Toast.makeText(MainActivity.this, "Setup Unsuccessful", Toast.LENGTH_SHORT).show();
} else {
if (mHelper == null)
return;
else {
mHelper.queryInventoryAsync(onQueryInventoryFinishedListener);
}
}
}
});
TextView textView = (TextView) findViewById(R.id.purchase);
textView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
onPurchaseClick();
}
});
}
@Override
protected void onDestroy() {
super.onDestroy();
mHelper.dispose();
}
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
}
public void onPurchaseClick() {
mHelper.launchPurchaseFlow(this, SKU_PREMIUM, 101,
mPurchaseFinishedListener, "Purchase");
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (!mHelper.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);
} else {
Log.d("MYTAG", "onActivityResult handled by IABUtil.");
}
}
}
Output:-
Add permission to your manifest:-
<uses-permission android:name="com.android.vending.BILLING" />
Add Util package to your Project:-
For that open
<sdk>/extras/google/play_billing/sample/TrivialDrive/src/com/example/android/trivialdrivesample/util.
Copy that package to your application.
Step 1) Create xml file.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:showIn="@layout/activity_main"
android:gravity="center"
android:background="@color/colorPrimary">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_testing"/>
<Button
android:id="@+id/purchase"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Buy Item!" />
</LinearLayout>
Step 2) Create a main.java file.
package inapp.example.test;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
import com.android.vending.billing.IInAppBillingService;
import inapp.example.test.util.IabHelper;
import inapp.example.test.util.IabResult;
import inapp.example.test.util.Inventory;
import inapp.example.test.util.Purchase;
public class MainActivity extends AppCompatActivity {
//Enter your product Id
static final String SKU_PREMIUM = "product_p1";
//Enter your base 64 here
String base64EncodedPublicKey = "";
IabHelper mHelper;
IabHelper.QueryInventoryFinishedListener onQueryInventoryFinishedListener = new IabHelper.QueryInventoryFinishedListener() {
@Override
public void onQueryInventoryFinished(IabResult result, Inventory inv) {
if (result.isFailure())
return;
Purchase purchase = inv.getPurchase(SKU_PREMIUM);
if (purchase != null)
Toast.makeText(MainActivity.this, "Purchase successful", Toast.LENGTH_SHORT).show();
}
};
IabHelper.OnIabPurchaseFinishedListener mPurchaseFinishedListener = new IabHelper.OnIabPurchaseFinishedListener() {
@Override
public void onIabPurchaseFinished(IabResult result, Purchase info)
{
if (mHelper == null)
return;
else {
if (result.isSuccess())
Toast.makeText(MainActivity.this, "Sucessfully Purchase", Toast.LENGTH_SHORT).show();
}
}
};
private IInAppBillingService mService;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mHelper = new IabHelper(this, base64EncodedPublicKey);
mHelper.startSetup(new IabHelper.OnIabSetupFinishedListener()
{
@Override
public void onIabSetupFinished(IabResult result)
{
if (!result.isSuccess()) {
Toast.makeText(MainActivity.this, "Setup Unsuccessful", Toast.LENGTH_SHORT).show();
} else {
if (mHelper == null)
return;
else {
mHelper.queryInventoryAsync(onQueryInventoryFinishedListener);
}
}
}
});
TextView textView = (TextView) findViewById(R.id.purchase);
textView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
onPurchaseClick();
}
});
}
@Override
protected void onDestroy() {
super.onDestroy();
mHelper.dispose();
}
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
}
public void onPurchaseClick() {
mHelper.launchPurchaseFlow(this, SKU_PREMIUM, 101,
mPurchaseFinishedListener, "Purchase");
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (!mHelper.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);
} else {
Log.d("MYTAG", "onActivityResult handled by IABUtil.");
}
}
}
Output:-
Comments
Post a Comment