如何使用getPurchases()-Android

时间:2019-02-08 18:03:46

标签: java android in-app-purchase sharedpreferences in-app-billing

我已经在我的应用中成功实现了应用内结算。 IAP工作正常,我已经对其进行了测试以确保其正常工作。

用户单击按钮时,必须进行一次IAP才能继续。但是,即使用户已经进行了IAP,每次用户单击按钮时,它都会启动IAP。我希望我的IAP不可消耗。当前,我将IAP存储在SharedPreferences中,但是如果用户重新安装该应用程序,他们将丢失其IAP。

那么,如何在getPurchases()restoreTransactions()方法上使用onCreateonClick来检查用户是否购买了特定商品?我已经在Internet上进行了广泛的搜索,并阅读了许多示例,但它似乎没有用,但是也许我误会了。

如果您需要我发布任何代码,请询问,我将更新我的帖子。

2 个答案:

答案 0 :(得分:0)

使用此库:

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
    */
  }
}

注意:只有在您初始化BillingProcessor时第一次调用onPurchaseHistoryRestored

答案 1 :(得分:0)

他们有很多方法可以检查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();
}
}