我最近在Google Play上发布了我的应用。该应用程序包含高级版本,因此如果购买,Premium用户将获得额外的功能。
为了检查用户是否是高级用户,我已经实施了测试。我在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().
正如我从文档中理解的那样,应该从缓存中读取它,但我可能错了。
如何使其更加健壮的任何想法都值得赞赏。我真的不喜欢一些高级用户在他们已经拥有的时候可以选择升级。
此致 埃里克