在InApp android中恢复功能

时间:2013-06-20 22:03:13

标签: android in-app-purchase in-app-billing restore

我已在我的应用中实施了Inapp结算。为此,我使用了按钮。当我点击按钮它进入购买活动,它购买产品。一旦购买产品,该按钮将不可见。并且将显示另一个按钮,允许用户打开产品。这一切都发生得很完美。但是当我再次重新打开应用程序时,可以看到购买按钮。我不希望按钮在购买后可见。一旦用于成功购买,如何使按钮不可见。

StartUpActivity.java

public class StartUpActivity extends PurchaseActivity implements   OnIabSetupFinishedListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
   Log.d("App started");
}
@Override
public void onIabSetupFinished(IabResult result) {
    if (result.isSuccess()) {
        Log.d("In-app Billing set up" + result);
        dealWithIabSetupSuccess();
    } else {
        Log.d("Problem setting up In-app Billing: " + result);
        dealWithIabSetupFailure();
    }
}
@Override
protected void dealWithIabSetupSuccess() {
    navigate().toMainActivity();
    finish();
}
@Override
protected void dealWithIabSetupFailure() {
    popBurntToast("Sorry In App Billing isn't available on your device");
}
}

MainActivity.java

public class MainActivity extends BlundellActivity implements MainMenu {
public Button topicsbutton;
public Button mediabutton;
public Button purchasetopicsbutton;
public Button purchasemediabutton;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    topicsbutton = (Button) findViewById(R.id.button1a);
    mediabutton = (Button) findViewById(R.id.button2a);
    purchasetopicsbutton = (Button) findViewById(R.id.button1);
    purchasemediabutton = (Button) findViewById(R.id.button2);
}
@Override
public void onTopicsPurchaseItemClick(View v) {
    navigate().toPurchaseTopicsActivityForResult();
}
@Override
public void onMediaPurchaseItemClick(View v) {
    navigate().toPurchaseMediaActivityForResult();
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (Navigator.REQUEST_TOPICS_PURCHASE == requestCode) {
        if (RESULT_OK == resultCode) {
            dealWithSuccessfulTopicsPurchase();
        } else {
            dealWithFailedTopicsPurchase();
        }
    }        else if (Navigator.REQUEST_MEDIA_PURCHASE == requestCode) {
        if (RESULT_OK == resultCode) {
            dealWithSuccessfulMediaPurchase();
        } else {
            dealWithFailedMediaPurchase();
        }
    }
}
public void dealWithSuccessfulTopicsPurchase() {
    Log.d("Topics purchased");
    popToast("Topics purchased");
    topicsbutton.setVisibility(View.VISIBLE);
    purchasetopicsbutton.setVisibility(View.INVISIBLE);

}
private void dealWithSuccessfulMediaPurchase() {
    Log.d("Media purchased");
    popToast("Media purchased");
    mediabutton.setVisibility(View.VISIBLE);
    purchasemediabutton.setVisibility(View.INVISIBLE);

}
private void dealWithFailedTopicsPurchase() {
    Log.d("Topics purchase failed");
    popToast("Failed to purchase Topics");
}
private void dealWithFailedMediaPurchase() {
    Log.d("Media purchase failed");
    popToast("Failed to purchase Media");
}
public void TopicsOpen(View v) {
    navigate().toTopicsopen();
}
public void MediaOpen(View v) {
    navigate().toMediaopen();
}
}

PurchaseActivity.java

 public abstract class PurchaseActivity extends BlundellActivity implements OnIabSetupFinishedListener, OnIabPurchaseFinishedListener {
private IabHelper billingHelper;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_purchase);
    setResult(RESULT_CANCELED);
    billingHelper = new IabHelper(this, AppProperties.BASE_64_KEY);
    billingHelper.startSetup(this);
}
@Override
public void onIabSetupFinished(IabResult result) {
    if (result.isSuccess()) {
        Log.d("In-app Billing set up" + result);
        dealWithIabSetupSuccess();
    } else {
        Log.d("Problem setting up In-app Billing: " + result);
        dealWithIabSetupFailure();
    }
}
protected abstract void dealWithIabSetupSuccess();
protected abstract void dealWithIabSetupFailure();
protected void purchaseItem(String sku) {
    billingHelper.launchPurchaseFlow(this, sku, 123, this);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    billingHelper.handleActivityResult(requestCode, resultCode, data);
}
@Override
public void onIabPurchaseFinished(IabResult result, Purchase info) {
    if (result.isFailure()) {
        dealWithPurchaseFailed(result);
    } else if (Items.SKU1.equals(info.getSku())) {
        dealWithTopicsPurchaseSuccess(result, info);
    }
    else if (Items.SKU2.equals(info.getSku())) {
        dealWithMediaPurchaseSuccess(result, info);
    }
    finish();
}
protected void dealWithPurchaseFailed(IabResult result) {
    Log.d("Error purchasing: " + result);
}
protected void dealWithTopicsPurchaseSuccess(IabResult result, Purchase info) {
    Log.d("Item purchased: " + result);
}
protected void dealWithMediaPurchaseSuccess(IabResult result, Purchase info) {
    Log.d("Item purchased: " + result);
    billingHelper.consumeAsync(info, null);
}
@Override
protected void onDestroy() {
    disposeBillingHelper();
    super.onDestroy();
}
private void disposeBillingHelper() {
    if (billingHelper != null) {
        billingHelper.dispose();
    }
    billingHelper = null;
}
}

1 个答案:

答案 0 :(得分:1)

如果您使用的是API版本3,则可以onCreate()调用getPurchases()方法。

  

此方法返回用户拥有的当前未消费产品。

这意味着如果您的产品是不可消费的,即用户只会购买一次,那么您将获得必要的信息。

无法解释如何比官方文档更好地解决问题。请查看Querying for Purchased Items主题以获取代码示例。

基本上,您将收到ownedSkus(拥有的产品ID)列表,您只需在此列表上循环,并使您的按钮不可见/可见。

如果您正在寻找的话,请告诉我:))