0

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

启动活动.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();
}
}

购买活动.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;
}
}
4

1 回答 1

1

如果您使用的是 API 版本 3,您可以onCreate().

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

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

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

基本上你会收到一个ownedSkus(拥有的产品ID)列表,你所要做的就是在这个列表上循环,让你的按钮不可见/可见。

让我知道这是否是您要找的:)

于 2013-06-20T23:53:24.573 回答