Skip to content

Instantly share code, notes, and snippets.

@hmsk
Created January 22, 2013 11:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hmsk/4593945 to your computer and use it in GitHub Desktop.
Save hmsk/4593945 to your computer and use it in GitHub Desktop.
Google Play In-app BillingをAPI v3のサンプルを使って楽して導入する ref: http://qiita.com/items/39dff3374e2292a1daed
public abstract class BaseActivity extends SherlockFragmentActivity {
private static final String SKU_PREMIUM = "Developer Consoleで設定したproduct id";
private static final int REQUEST_CODE_PURCHASE_PREMIUM = よしなにリクエストコードを;
private static final String BILLING_PUBLIC_KEY = "Developer Consoleで得られるBase64encodedな公開鍵(そのままコードに持たせず別の形からデコードなりして持ってくるのが推奨されている";
private IabHelper mBillingHelper;
private boolean mIsPremium = false;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
~~
setupBilling();
}
@Override
protected void onDestroy() {
super.onDestroy();
tearDownBilling();
}
private void setupBilling() {
mBillingHelper = new IabHelper(this, BILLING_PUBLIC_KEY);
mBillingHelper.enableDebugLogging(true); // Remove before release
mBillingHelper.startSetup(new IabHelper.OnIabSetupFinishedListener() {
public void onIabSetupFinished(IabResult result) {
Log.d("billing", "Setup finished.");
if (result.isFailure()) return;
Log.d("billing", "Setup successful. Querying inventory.");
mBillingHelper.queryInventoryAsync(mGotInventoryListener);
}
});
}
private void tearDownBilling() {
if (mBillingHelper != null) mBillingHelper.dispose();
mBillingHelper = null;
}
private IabHelper.QueryInventoryFinishedListener mGotInventoryListener = new IabHelper.QueryInventoryFinishedListener() {
public void onQueryInventoryFinished(IabResult result, Inventory inventory) {
Log.d("billing", "Query inventory finished.");
if (result.isFailure()) return;
Log.d("billing", "Query inventory was successful.");
mIsPremium = inventory.hasPurchase(SKU_PREMIUM);
Log.d("billing", "User is " + (mIsPremium ? "PREMIUM" : "NOT PREMIUM"));
}
};
private IabHelper.OnIabPurchaseFinishedListener mPurchaseFinishedListener = new IabHelper.OnIabPurchaseFinishedListener() {
public void onIabPurchaseFinished(IabResult result, Purchase purchase) {
Log.d("billing", "Purchase finished: " + result + ", purchase: " + purchase);
if (result.isFailure()) return;
Log.d("billing", "Purchase successful.");
if (purchase.getSku().equals(SKU_PREMIUM)) {
Log.d("billing", "Purchase is premium upgrade. Congratulating user.");
mIsPremium = true;
}
}
};
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (!mBillingHelper.handleActivityResult(requestCode, resultCode, data)) {
super.onActivityResult(requestCode, resultCode, data);
}
}
protected void requestBilling() {
mBillingHelper.launchPurchaseFlow(this, SKU_PREMIUM, REQUEST_CODE_PURCHASE_PREMIUM, mPurchaseFinishedListener);
}
protected boolean isPremiumUser() {
return this.mIsPremium;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment