【问题标题】:How to use getPurchases() - Android如何使用 getPurchases() - Android
【发布时间】:2019-02-08 18:03:45
【问题描述】:
我已成功将 In-App-Billing 实施到我的应用中。 IAP 运行成功,我已经对其进行了测试以确保其正常运行。
当用户单击按钮时,他们必须进行 IAP 才能继续。但是,每次用户单击该按钮时,它都会启动 IAP,即使他们已经创建了 IAP。我希望我的 IAP 显然是非消耗品。目前,我将 IAP 存储在 SharedPreferences 中,但如果用户重新安装该应用程序,他们将失去其 IAP。
那么如何在我的onCreate 或onClick 方法上使用getPurchases() 或restoreTransactions() 来检查用户是否购买了特定商品?我已经在互联网上搜索并阅读了这么多示例,但它似乎不起作用,也许我是误会了。
如果您需要我发布任何代码,请询问,我会更新我的帖子。
【问题讨论】:
标签:
java
android
in-app-purchase
sharedpreferences
in-app-billing
【解决方案1】:
使用这个库:
https://github.com/anjlab/android-inapp-billing-v3
如何使用?
在你的 gradle 中使用它:
repositories {
mavenCentral()
}
dependencies {
implementation 'com.anjlab.android.iab.v3:library:1.0.44'
}
应用内结算的清单权限:
<uses-permission android:name="com.android.vending.BILLING" />
如何使用库方法:
public class SomeActivity extends Activity implements BillingProcessor.IBillingHandler {
BillingProcessor bp;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bp = new BillingProcessor(this, "YOUR LICENSE KEY FROM GOOGLE PLAY CONSOLE HERE", this);
bp.initialize();
// or bp = BillingProcessor.newBillingProcessor(this, "YOUR LICENSE KEY FROM GOOGLE PLAY CONSOLE HERE", this);
// See below on why this is a useful alternative
}
// IBillingHandler implementation
@Override
public void onBillingInitialized() {
/*
* Called when BillingProcessor was initialized and it's ready to purchase
*/
}
@Override
public void onProductPurchased(String productId, TransactionDetails details) {
/*
* Called when requested PRODUCT ID was successfully purchased
*/
}
@Override
public void onBillingError(int errorCode, Throwable error) {
/*
* Called when some error occurred. See Constants class for more details
*
* Note - this includes handling the case where the user canceled the buy dialog:
* errorCode = Constants.BILLING_RESPONSE_RESULT_USER_CANCELED
*/
}
@Override
public void onPurchaseHistoryRestored() {
/*
* Called when purchase history was restored and the list of all owned PRODUCT ID's
* was loaded from Google Play
*/
}
}
注意:onPurchaseHistoryRestored 仅在您初始化 BillingProcessor 时第一次调用
【解决方案2】:
他们有很多方法可以检查 onPurchaseHistoryRestored() 但这是我的意见。我已经通过使用解决了它..
您可以使用此代码检查横断面详细信息。
@Override
public void onPurchaseHistoryRestored() {
/*
* Called when purchase history was restored and the list of all owned PRODUCT ID's
* was loaded from Google Play
*/
// Check whether 'premium_id' has previously been purchased:
TransactionDetails premiumTransactionDetails = bp.getPurchaseTransactionDetails("premium_id");
if (premiumTransactionDetails == null) {
Log.i(TAG, "onPurchaseHistoryRestored(): Havn't bought premium yet.");
purchasePremiumButton.setEnabled(true);
}
else {
Log.i(TAG, "onPurchaseHistoryRestored(): Already purchases premium.");
purchasePremiumButton.setText(getString(R.string.you_have_premium));
purchasePremiumButton.setEnabled(false);
statusTextView.setVisibility(View.INVISIBLE);
}
}
第二个是检查“产品ID”。检查下面的代码。
if(bp.isPurchased(REMOVE_ID_SKU)){
if (bp.loadOwnedPurchasesFromGoogle()) {
Toast.makeText(this, R.string.subs_updated, Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(this, R.string.no_purchases, Toast.LENGTH_SHORT).show();
}
}