以下代码是我的inapp结算应用程序,但我有一个问题,如何保存用户购买的项目,以便每次检查时都不需要连接到Google Play?
以下代码是我的inapp结算应用程序,但我有一个问题,如何保存用户购买的项目,以便每次检查时都不需要连接到Google Play?
package com.example.inapptest;
import com.example.inapptest.R;
import com.example.android.trivialdrivesample.util.IabHelper;
import com.example.android.trivialdrivesample.util.IabResult;
import com.example.android.trivialdrivesample.util.Inventory;
import com.example.android.trivialdrivesample.util.Purchase;
import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.widget.ImageView;
public class MainActivity extends Activity {
boolean mIsPremium = false;
static final String SKU_PREMIUM = "premium";
static final int RC_REQUEST = 10001;
IabHelper mHelper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String base64EncodedPublicKey = "MIHNMA0GCSqGSIb3DQEBAQUAA4G7ADCBtwKBrwDEYf1TFjO6qpCR5t/7w+P0luTLEKH6aSUYyXt95mHdJOf7gLoLMYJLlfHp/nVyDLLDIoG5a2RaFE5cwZxc0CCVXouuhkO/B/ckFb03FZcg8T0oOSLIyNiPrrrtlln7DyXhvv60KFk8qozuX46MiR+SZfNdg5AJrV9KDHZ9aIHyHztQkpajZ9WJd7F4theRGom3Gydp0LRTumHcmgweOj0ZF6CzzSEQkgOp7S1Guo8CAwEAAQ==";
mHelper = new IabHelper(this, base64EncodedPublicKey);
mHelper.startSetup(new IabHelper.OnIabSetupFinishedListener() {
public void onIabSetupFinished(IabResult result) {
mHelper.queryInventoryAsync(mGotInventoryListener);
}
});
}
IabHelper.QueryInventoryFinishedListener mGotInventoryListener = new IabHelper.QueryInventoryFinishedListener() {
public void onQueryInventoryFinished(IabResult result, Inventory inventory) {
// Do we have the premium upgrade?
Purchase premiumPurchase = inventory.getPurchase(SKU_PREMIUM);
mIsPremium = (premiumPurchase != null && verifyDeveloperPayload(premiumPurchase));
updateUi();
}
};
public void onUpgradeAppButtonClicked(View arg0) {
String payload = "";
mHelper.launchPurchaseFlow(this, SKU_PREMIUM, RC_REQUEST,
mPurchaseFinishedListener, payload);
}
// Callback for when a purchase is finished
IabHelper.OnIabPurchaseFinishedListener mPurchaseFinishedListener = new IabHelper.OnIabPurchaseFinishedListener() {
public void onIabPurchaseFinished(IabResult result, Purchase purchase) {
if (purchase.getSku().equals(SKU_PREMIUM)) {
// bought the premium upgrade!
mIsPremium = true;
updateUi();
}
}
};
boolean verifyDeveloperPayload(Purchase p) {
String payload = p.getDeveloperPayload();
return false;
}
@Override
public void onDestroy() {
super.onDestroy();
if (mHelper != null) mHelper.dispose();
mHelper = null;
}
public void updateUi() {
// TODO Auto-generated method stub
((ImageView)findViewById(R.id.free_or_premium)).setImageResource(R.drawable.premium);
findViewById(R.id.upgrade_button).setVisibility(View.INVISIBLE);
}
}
答案 0 :(得分:2)
如果您使用IAB库的最新版本3,则无需保存购买。您可以在每次应用程序启动时从服务中查询它们,因为Google Play服务会为您在设备上缓存购买。请参阅此remark on Local Caching。