为什么 Android 内购初始化后无法正常工作?

huangapple go评论89阅读模式
英文:

Why Android IAP initializing but not working?

问题

我正在使用这个库来在我的应用中实现应用内购买。

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;

我在 onCreate 方法中的 setContentView 之后使用了(用于测试)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");

代码没有执行 onProductPurchased 方法。只有 onBillingInitialized 方法被执行。我查看了详细的 Logcat 日志。当我看到 "initialized" 行时,并没有关于 purchased 的任何行。

因此,我该如何解决我的问题?为什么它不起作用?

X 和 Y 的值是正确的,我在 Google Play 控制台中进行了检查。

英文:

I am using this library to implement in app purchase in my app.

https://github.com/anjlab/android-inapp-billing-v3

I added this code to my gradle.

repositories {
  mavenCentral()
}
dependencies {
  implementation &#39;com.anjlab.android.iab.v3:library:1.0.44&#39;
}

I added the permission.

 &lt;uses-permission android:name=&quot;com.android.vending.BILLING&quot; /&gt;

I declared variable

 BillingProcessor bp;

I used(to test it) bp in onCreate method after setContentView

   setContentView(R.layout.activity_main);



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


        bp.purchase(this, &quot;y&quot;);

The code does not run onProductPurchased method. It only runs onBillingInitialized method. I looked verbose logcat. There is no line about purchased when I can see initialized line.

As a result, how can I solve my problem? Why does not it work?

X and Y values are correct, I checked them in Google Play Console.

答案1

得分: 4

我认为你应该在 onCreate 中这样调用 bp.initialize()

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");

你还应该重写 ActivityonActivityResult 方法:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  if (!bp.handleActivityResult(requestCode, resultCode, data)) {
    super.onActivityResult(requestCode, resultCode, data);
  }  
}
祝你好运
英文:

I think you should call

bp.initialize();

in oncreate like this

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

bp.initialize();

and call

bp.purchase(this, &quot;y&quot;);

in button click event.

you should also override Activity's onActivityResult method:

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

good luck!

huangapple
  • 本文由 发表于 2020年8月22日 18:34:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/63535161.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定