为什么android iap初始化但不工作?

mpbci0fu  于 2021-07-06  发布在  Java
关注(0)|答案(1)|浏览(413)

我正在使用此库在我的应用程序中实现应用内购买。
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" />

我声明了变量

BillingProcessor bp;

我在setcontentview之后的oncreate方法中使用了(测试它)bp

setContentView(R.layout.activity_main);

   bp = new BillingProcessor(this, "x", null,new BillingProcessor.IBillingHandler() {
            @Override
            public void onProductPurchased(@NonNull String productId, @Nullable TransactionDetails details) {
                Log.d("purchase2","purchased");
            }
            @Override
            public void onBillingError(int errorCode, @Nullable Throwable error) {
                Log.d("purchase2","error");
            }
            @Override
            public void onBillingInitialized() {
                Log.d("purchase2","initialized");
            }
            @Override
            public void onPurchaseHistoryRestored() {
                Log.d("purchase2","historyrestored");
            }
        });

        bp.purchase(this, "y");

代码不在ProductPurchased方法上运行。它只在BillingInitialized方法上运行。我看起来像个冗长的罗格猫。当我看到初始化的行时,没有关于购买的行。
因此,我该如何解决我的问题?为什么不起作用?
x和y值是正确的,我在googleplay控制台中检查了它们。

lzfw57am

lzfw57am1#

我想你应该打电话

bp.initialize();

像这样的创造

setContentView(R.layout.activity_main);

       bp = new BillingProcessor(this, "x", null,new BillingProcessor.IBillingHandler() {
                @Override
                public void onProductPurchased(@NonNull String productId, @Nullable TransactionDetails details) {
                    Log.d("purchase2","purchased");
                }
                @Override
                public void onBillingError(int errorCode, @Nullable Throwable error) {
                    Log.d("purchase2","error");
                }
                @Override
                public void onBillingInitialized() {
                    Log.d("purchase2","initialized");
                }
                @Override
                public void onPurchaseHistoryRestored() {
                    Log.d("purchase2","historyrestored");
                }
            });

bp.initialize();

打电话给我 bp.purchase(this, "y"); 在按钮单击事件中。
还应重写活动的onactivityresult方法:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  if (!bp.handleActivityResult(requestCode, resultCode, data)) {
    super.onActivityResult(requestCode, resultCode, data);
  }  
}

祝你好运!

相关问题