我也在做同样的调查,但在我的测试中,我发现你不需要存储它,因为谷歌做了你需要的所有缓存,我怀疑(虽然我没有调查过)他们这样做是安全的尽可能(认为这也符合他们的利益!)
所以这就是我所做的
// Done in onCreate
mHelper = new IabHelper(this, getPublicKey());
mHelper.startSetup(new IabHelper.OnIabSetupFinishedListener() {
public void onIabSetupFinished(IabResult result) {
if (!result.isSuccess()) {
// Oh noes, there was a problem.
Log("Problem setting up In-app Billing: " + result);
} else {
Log("onIabSetupFinished " + result.getResponse());
mHelper.queryInventoryAsync(mGotInventoryListener);
}
}
});
// Called by button press
private void buyProUpgrade() {
mHelper.launchPurchaseFlow(this, "android.test.purchased", 10001,
mPurchaseFinishedListener, ((TelephonyManager)this.getSystemService(Context.TELEPHONY_SERVICE)).getDeviceId());
}
// Get purchase response
private IabHelper.OnIabPurchaseFinishedListener mPurchaseFinishedListener = new IabHelper.OnIabPurchaseFinishedListener() {
public void onIabPurchaseFinished(IabResult result, Purchase purchase)
{
if (result.isFailure()) {
Log("Error purchasing: " + result);
return;
}
else if (purchase.getSku().equals("android.test.purchased")) {
Log("onIabPurchaseFinished GOT A RESPONSE.");
mHelper.queryInventoryAsync(mGotInventoryListener);
}
}
};
// Get already purchased response
private IabHelper.QueryInventoryFinishedListener mGotInventoryListener = new IabHelper.QueryInventoryFinishedListener() {
public void onQueryInventoryFinished(IabResult result,
Inventory inventory) {
if (result.isFailure()) {
// handle error here
Log("Error checking inventory: " + result);
}
else {
// does the user have the premium upgrade?
mIsPremium = inventory.hasPurchase("android.test.purchased");
setTheme();
Log("onQueryInventoryFinished GOT A RESPONSE (" + mIsPremium + ").");
}
}
};
那么这里会发生什么?
IAB 已设置并调用startSetup
,在成功完成后(只要它已通过 Internet 连接运行一次并且设置正确,它将始终成功)我们调用queryInventoryAsync
以找出已经购买的内容(再次,如果这已经在线时调用它总是在离线时工作)。
因此,如果购买成功完成(只能在在线时完成),我们会致电queryInventoryAsync
以确保它已在在线时被调用。
现在无需存储与购买有关的任何内容,并使您的应用程序更不易被破解。
我已经测试了很多方法,飞行模式,再次关闭设备,唯一搞砸的就是清除手机上某些谷歌应用程序中的数据(不太可能发生!)。
如果您有不同的经验,请为此做出贡献,我的应用程序仍处于早期测试阶段。