2
我最近在Google Play上发布了我的应用。该应用程序包含一个高级版本,所以如果购买,高级用户可以获得额外的功能。应用帐单从缓存中读取
要检查用户是否优质,我已经实施了一项测试。我从Android样本中跟踪了TrivialDrive中的示例。问题是我注意到有时需要一段时间才能检测到用户是高级用户。在我的朋友电话中,我曾两次看到,直到您尝试在一段时间后再次打开高级功能时,它才会检测到付费状态。
我开始怀疑它与需要时间的服务器连接有关。我认为这是自动缓存,但现在我不确定。
这里是我的代码:
@Override
protected void onResume(){
super.onResume();
MainActivity.showToast("onResume");
StringBuilder sb = new StringBuilder();
sb.append(BASE64_PUBLIC_KEY1);
sb.append(BASE64_PUBLIC_KEY2);
sb.append(BASE64_PUBLIC_KEY3);
sb.append(BASE64_PUBLIC_KEY4);
sb.append(BASE64_PUBLIC_KEY5);
sb.append(BASE64_PUBLIC_KEY6);
final String BASE64_PUBLIC_KEY = sb.toString();
// Create the helper, passing it our context and the public key to verify signatures with
// Log.d(TAG, "Creating IAB helper.");
mHelper = new IabHelper(this, BASE64_PUBLIC_KEY);
// enable debug logging (for a production application, you should set this to false).
mHelper.enableDebugLogging(false);
// Start setup. This is asynchronous and the specified listener
// will be called once setup completes.
// Log.d(TAG, "Starting setup.");
mHelper.startSetup(new IabHelper.OnIabSetupFinishedListener() {
public void onIabSetupFinished(IabResult result) {
// Log.d(TAG, "Setup finished.");
if (!result.isSuccess()) {
// Oh noes, there was a problem.
MainActivity.showToast("Problem setting up in-app billing: " + result);
return;
}
// Have we been disposed of in the meantime? If so, quit.
if (mHelper == null) return;
// Important: Dynamically register for broadcast messages about updated purchases.
// We register the receiver here instead of as a <receiver> in the Manifest
// because we always call getPurchases() at startup, so therefore we can ignore
// any broadcasts sent while the app isn't running.
// Note: registering this listener in an Activity is a bad idea, but is done here
// because this is a SAMPLE. Regardless, the receiver must be registered after
// IabHelper is setup, but before first call to getPurchases().
mBroadcastReceiver = new IabBroadcastReceiver(SkeetStatistic.this);
IntentFilter broadcastFilter = new IntentFilter(IabBroadcastReceiver.ACTION);
registerReceiver(mBroadcastReceiver, broadcastFilter);
// IAB is fully set up. Now, let's get an inventory of stuff we own.
ArrayList<String> list_items = new ArrayList<String>();
list_items.add(SKU_PREMIUM);
// Log.d(TAG, "Setup successful. Querying inventory.");
mHelper.queryInventoryAsync(true, list_items, mGotInventoryListener);
}
});
}
mGotInventoryListener:
// Listener that's called when we finish querying the items and subscriptions we own
IabHelper.QueryInventoryFinishedListener mGotInventoryListener = new IabHelper.QueryInventoryFinishedListener() {
public void onQueryInventoryFinished(IabResult result, Inventory inventory) {
// Log.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()) {
// Log.d(TAG,"Failed to query inventory: " + result);
return;
}
// Log.d(TAG, "Query inventory was successful.");
/*
* Check for items we own. Notice that for each purchase, we check
* the developer payload to see if it's correct! See
* verifyDeveloperPayload().
*/
// Do we have the premium upgrade?
Purchase premiumPurchase = inventory.getPurchase(SKU_PREMIUM);
mIsPremium = (premiumPurchase != null && verifyDeveloperPayload(premiumPurchase));
if (mIsPremium){
// System.out.println("Premium");
MainActivity.showToast("Premium");
}
if (!mIsPremium){
MainActivity.showToast("Not Premium");
}
// Log.d(TAG, "User is " + (mIsPremium ? "PREMIUM" : "NOT PREMIUM"));
setWaitScreen(false);
// Log.d(TAG, "Initial inventory query finished; enabling main UI.");
}
};
当我看在IabHelper类,似乎他们称之为
mService.getPurchase().
正如我的理解是从文档中,应该从缓存中读取它,但我可能是错的。
任何想法如何使其更强大的赞赏。我真的不喜欢有些高级用户可以选择升级。
问候 埃里克