In-App Billing After Purchase

有些话、适合烂在心里 提交于 2019-12-11 18:52:23

问题


I was wondering how I would be able to have my app remove the buttons for buying an item that a user has purchased in in-app billing. I could use sharedpreferences, but how would I go about doing that. This is the tutorial I used: http://www.anddev.org/advanced-tutorials-f21/simple-inapp-billing-payment-t52060.html.

Thanks

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 attempted purchase is: "+BillingHelper.latestPurchase.productId);



    };     
};

回答1:


If you followed the Dungeons example, you probably have implemented a ResponsHandler/PurchaseObserver ?

Somewhere in your code, you have registered a PurchaseObserver like this

ResponseHandler.register(purchaseObserver); 

In the purchaseObserver, you override the method called

public void onPurchaseStateChange(...)

By using shared preferences you can keep track of the state of your app in that method. It is important to handle cancellations/refunds. If not, you're giving your stuff away for free. The code might look something like this

SharedPreferences p = PreferenceManager.getDefaultSharedPreferences(context);
SharedPreferences.Editor e = p.edit();
if (purchaseState == Consts.PurchaseState.CANCELED 
         || purchaseState ==   Consts.PurchaseState.REFUNDED) {
      e.putBoolean("PURCHASED", false);
} else if (purchaseState == Consts.PurchaseState.PURCHASED) {
      e.putBoolean("PURCHASED", true);
}
e.commit();



回答2:


You can use SharedPreferences to persist the bought items. Then inside onCreate() of your InAppActivity, do this :

if(settings.getBoolean("isAwesomeItemBought") {
   buyButton.setVisibility(View.GONE);
   buyText.setVisibility(View.VISIBLE);
}


来源:https://stackoverflow.com/questions/8571246/in-app-billing-after-purchase

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!