在我的应用用户可以购买广告删除,我保留此项目(不消费)。所以我在我的主要活动中有片段,检查用户是否购买了物品。
public class BillingInventoryFragment extends Fragment {
// Helper billing object
private IabHelper mHelper;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRetainInstance(true);
initialiseBilling();
}
private void initialiseBilling() {
if (mHelper != null) {
return;
}
// Create the helper, passing it our context and the public key to verify signatures with
mHelper = new IabHelper(getActivity(), BillingUtils.getApplicationKey());
// Enable debug logging (for a production application, you should set this to false).
mHelper.enableDebugLogging(true);
// Start setup. This is asynchronous and the specified listener will be called once setup completes.
mHelper.startSetup(new IabHelper.OnIabSetupFinishedListener() {
@Override
public void onIabSetupFinished(IabResult result) {
// Have we been disposed of in the meantime? If so, quit.
if (mHelper == null) {
return;
}
// Something went wrong
if (!result.isSuccess()) {
Log.e(getActivity().getApplicationInfo().name, "Problem setting up in-app billing: " + result.getMessage());
return;
}
// IAB is fully set up. Now, let's get an inventory of stuff we own.
mHelper.queryInventoryAsync(iabInventoryListener());
}
});
}
/**
* Listener that's called when we finish querying the items and subscriptions we own
*/
private IabHelper.QueryInventoryFinishedListener iabInventoryListener() {
return new IabHelper.QueryInventoryFinishedListener() {
@Override
public void onQueryInventoryFinished(IabResult result, Inventory inventory) {
// Have we been disposed of in the meantime? If so, quit.
if (mHelper == null) {
return;
}
// Something went wrong
if (!result.isSuccess()) {
Log.d(TAG, String.format("result not success, result = %s", result) );
return;
}
// Do your checks here...
// Do we have the premium upgrade?
Purchase purchasePro = inventory.getPurchase(BillingUtils.SKU_PRO); // Where BillingUtils.SKU_PRO is your product ID (eg. permanent.ad_removal)
Log.d(TAG, String.format("Purchase pro = %s", purchasePro));
BillingUtils.isPro = (purchasePro != null && BillingUtils.verifyDeveloperPayload(purchasePro));
// After checking inventory, re-jig stuff which the user can access now
// that we've determined what they've purchased
BillingUtils.initialiseStuff();
}
};
}
/**
* Very important!
*/
@Override
public void onDestroy() {
super.onDestroy();
if (mHelper != null) {
mHelper.dispose();
mHelper = null;
}
}
}
一切都可以在一台设备上运行,但是当我在第二台设备上测试时:
inventory.getPurchase(BillingUtils.SKU_PRO);
返回null。
当我尝试在第二台设备上再次购买物品时,我不能因为我拥有它。
答案 0 :(得分:-2)
如果您确定已成功购买此商品,则可能是与使用Gmail
相关的问题
您应该在两个设备中使用相同的Gmail
,因为Google
使用设备上注册的Gmail
来识别其用户。