英文:
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 'com.anjlab.android.iab.v3:library:1.0.44'
}
I added the permission.
<uses-permission android:name="com.android.vending.BILLING" />
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, "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");
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");
你还应该重写 Activity
的 onActivityResult
方法:
@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, "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();
and call
bp.purchase(this, "y");
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!
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论