检查用户是否订阅的正确方法?
Right way to check whether a user subscribed?
我正在使用以下代码查询订阅状态。有了这个我就可以得到关于这个订阅的布尔状态。此结果是否会受到网络状态或包 uninstall/reinstall 或任何其他标准的影响。是否有正确的查找订阅状态的方法?
mHelper = new IabHelper(this, PUBLIC_KEY);
mHelper.startSetup( new IabHelper.OnIabSetupFinishedListener() {
public void onIabSetupFinished(IabResult result) {
if ( ! result.isSuccess()) {
return;
}
if (QueryInventoryListner.mHelper == null){
return;
}
mHelper.queryInventoryAsync(mGotInventoryListener);
}
});
&查询库存完成监听器
mGotInventoryListener = new IabHelper.QueryInventoryFinishedListener() {
@Override
public void onQueryInventoryFinished(final IabResult result, final Inventory inventory) {
Purchase subscriptionForFullVersion = inventory.getPurchase(SKU_SUBSCRIPTION);
boolean isSubscribe = subscriptionForFullVersion != null ;
if( isSubscribe ) {
//User is subscribed to SKU_SUBSCRIPTION
}
}
此代码示例即使在重新安装应用程序后仍然有效。
您忘记添加 try\catch
块:
public void onIabSetupFinished(IabResult result) {
if (!result.isSuccess()) {
return;
}
if (QueryInventoryListner.mHelper == null){
return;
}
try {
mHelper.queryInventoryAsync(mGotInventoryListener);
} catch (IabAsyncInProgressException e) {
complain(context.getResources().getString(R.string.subscription_error_subscription_error_to_query_inventory_another_async));
}
}
我的 mGotInventoryListener 是这样的:
// Listener that's called when we finish querying the items and subscriptions we own
private QueryInventoryFinishedListener mGotInventoryListener = new QueryInventoryFinishedListener() {
public void onQueryInventoryFinished(IabResult result, Inventory inventory) {
Logging.d(TAG, "Query inventory finished.");
// Have we been disposed of in the meantime? If so, quit.
if (mHelper == null) return;
// Is it a failure?
if (result.isFailure()) {
complain(context.getResources().getString(R.string.subscription_error_subscription_error_to_query_inventory) + " " + result);
return;
}
Logging.d(TAG, "Query inventory was successful.");
// First find out which subscription is auto renewing
Purchase subscriptionMonthly = inventory.getPurchase(SKU_SUBSRIPTION_MONTHLY);
// The user is subscribed if either subscription exists, even if neither is auto
// renewing
mSubscribedToFreeAds = (subscriptionMonthly != null);
Logging.d(TAG, "User " + (mSubscribedToFreeAds ? "HAS" : "DOES NOT HAVE")
+ " monthly subscription.");
if (mSubscribedToFreeAds) {
putPurchase(subscriptionMonthly);//save purchase
isSubscribed = true;
} else {
clearPurchase();
isSubscribed = false;
}
updateUi();
setWaitScreen(false);
Logging.d(TAG, "Initial inventory query finished; enabling main UI.");
}
};
您还可以在发布前测试您的订阅:
Setting Up for Test Purchases
我正在使用以下代码查询订阅状态。有了这个我就可以得到关于这个订阅的布尔状态。此结果是否会受到网络状态或包 uninstall/reinstall 或任何其他标准的影响。是否有正确的查找订阅状态的方法?
mHelper = new IabHelper(this, PUBLIC_KEY);
mHelper.startSetup( new IabHelper.OnIabSetupFinishedListener() {
public void onIabSetupFinished(IabResult result) {
if ( ! result.isSuccess()) {
return;
}
if (QueryInventoryListner.mHelper == null){
return;
}
mHelper.queryInventoryAsync(mGotInventoryListener);
}
});
&查询库存完成监听器
mGotInventoryListener = new IabHelper.QueryInventoryFinishedListener() {
@Override
public void onQueryInventoryFinished(final IabResult result, final Inventory inventory) {
Purchase subscriptionForFullVersion = inventory.getPurchase(SKU_SUBSCRIPTION);
boolean isSubscribe = subscriptionForFullVersion != null ;
if( isSubscribe ) {
//User is subscribed to SKU_SUBSCRIPTION
}
}
此代码示例即使在重新安装应用程序后仍然有效。
您忘记添加 try\catch
块:
public void onIabSetupFinished(IabResult result) {
if (!result.isSuccess()) {
return;
}
if (QueryInventoryListner.mHelper == null){
return;
}
try {
mHelper.queryInventoryAsync(mGotInventoryListener);
} catch (IabAsyncInProgressException e) {
complain(context.getResources().getString(R.string.subscription_error_subscription_error_to_query_inventory_another_async));
}
}
我的 mGotInventoryListener 是这样的:
// Listener that's called when we finish querying the items and subscriptions we own
private QueryInventoryFinishedListener mGotInventoryListener = new QueryInventoryFinishedListener() {
public void onQueryInventoryFinished(IabResult result, Inventory inventory) {
Logging.d(TAG, "Query inventory finished.");
// Have we been disposed of in the meantime? If so, quit.
if (mHelper == null) return;
// Is it a failure?
if (result.isFailure()) {
complain(context.getResources().getString(R.string.subscription_error_subscription_error_to_query_inventory) + " " + result);
return;
}
Logging.d(TAG, "Query inventory was successful.");
// First find out which subscription is auto renewing
Purchase subscriptionMonthly = inventory.getPurchase(SKU_SUBSRIPTION_MONTHLY);
// The user is subscribed if either subscription exists, even if neither is auto
// renewing
mSubscribedToFreeAds = (subscriptionMonthly != null);
Logging.d(TAG, "User " + (mSubscribedToFreeAds ? "HAS" : "DOES NOT HAVE")
+ " monthly subscription.");
if (mSubscribedToFreeAds) {
putPurchase(subscriptionMonthly);//save purchase
isSubscribed = true;
} else {
clearPurchase();
isSubscribed = false;
}
updateUi();
setWaitScreen(false);
Logging.d(TAG, "Initial inventory query finished; enabling main UI.");
}
};
您还可以在发布前测试您的订阅: Setting Up for Test Purchases