Skip to content

Instantly share code, notes, and snippets.

@Budincsevity
Created July 16, 2015 22:12
Show Gist options
  • Save Budincsevity/0f62182f1e66e611129f to your computer and use it in GitHub Desktop.
Save Budincsevity/0f62182f1e66e611129f to your computer and use it in GitHub Desktop.
InApp Billing on Android
final String id = "android.test.purchased";
mConnection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder binder) {
service = IInAppBillingService.Stub.asInterface(binder);
String purchaseToken = "inapp:" + getPackageName() + ":android.test.purchased";
try {
int response = service.consumePurchase(3, getPackageName(), purchaseToken);
} catch (RemoteException e) {
e.printStackTrace();
}
Bundle buyIntentBundle = null;
try {
buyIntentBundle = service.getBuyIntent(3, getPackageName(),
id, "inapp", randomUUID().toString());
} catch (RemoteException e) {
e.printStackTrace();
}
if (buyIntentBundle != null) {
PendingIntent pendingIntent = buyIntentBundle.getParcelable("BUY_INTENT");
try {
startIntentSenderForResult(pendingIntent.getIntentSender(),
1001, new Intent(), Integer.valueOf(0), Integer.valueOf(0),
Integer.valueOf(0));
} catch (IntentSender.SendIntentException e) {
e.printStackTrace();
}
}
}
@Override
public void onServiceDisconnected(ComponentName name) {
service = null;
}
};
Intent serviceIntent = new Intent("com.android.vending.billing.InAppBillingService.BIND");
serviceIntent.setPackage("com.android.vending");
bindService(serviceIntent,
mConnection, Context.BIND_AUTO_CREATE);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment