【发布时间】:2014-02-16 13:43:44
【问题描述】:
我正在我的应用中实施应用内结算服务。我在onCreate() 中设置了对IabHelper.queryInventoryAsync() 的调用以检索以前的购买。如果购买了商品,我将全局布尔变量设置为 true。同时,在onCreateOptionsMenu() 中,我检查该变量是否删除菜单项。我的问题是onCreateOptionsMenu(),正如预期的那样,有时在异步任务完成设置我的布尔变量之前被调用。我需要一个解决方案来告诉onCreateOptionsMenu() 等待该任务设置布尔值,以便它可以相应地创建菜单。详细代码如下:
@Override
protected void onCreate(Bundle savedInstanceState) {
//other stuff...
helper.queryInventoryAsync(new IabHelper.QueryInventoryFinishedListener() {
public void onQueryInventoryFinished(IabResult result,
Inventory inventory) {
if (result.isFailure()) {
// handle error here
return;
}
if (inventory.hasPurchase(REMOVE_ADS_SKU)) {
//item purchased. set control variable
isNoAds = true;
}
}
});
}
@Override
public boolean onCreateOptionsMenu(final Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
//if the control variable is true, remove the option to buy ad-free version
if (isNoAds) menu.removeItem(R.id.action_remove_ads);
return super.onCreateOptionsMenu(menu);
}
【问题讨论】:
标签: android in-app-purchase android-optionsmenu