完成后没有打电话。

我一直在尝试为我的第一个应用程序建立应用程序内计费,并且一直在使用 android.test.buy 的 sku。购买完成后,我设法将 SKU 加入到我的库存中,但是,正如标题所说,onIabPurchaseFinish 从未被调用。

我认为这可能与这个日志有关: “无法保存具有焦点的视图,因为聚焦视图 com.android.interal.policy. impl。PhoneWindow $DecorView@406743d0没有 id”。就在去 Google Play 之前弹出来的。我不太确定那是什么意思。

开始购买:

mHelper.launchPurchaseFlow(this, sku, 10001, mPurchaseFinishedListener, "");

听众:

IabHelper.OnIabPurchaseFinishedListener mPurchaseFinishedListener = new IabHelper.OnIabPurchaseFinishedListener() {


@Override
public void onIabPurchaseFinished(IabResult result, Purchase info) {
System.out.println("Purchase Finish heard something");


if (result.isFailure()) {
Log.d(TAG, "Error purchasing: " + result);
return;
} else{
Log.d(TAG,"Success!");
}




}
};
13387 次浏览

Try adding this to the Activity that calls mHelper.launchPurchaseFlow(..):

    @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Log.d(TAG, "onActivityResult(" + requestCode + "," + resultCode + "," + data);


// Pass on the activity result to the helper for handling
if (!mHelper.handleActivityResult(requestCode, resultCode, data)) {
// not handled, so handle it ourselves (here's where you'd
// perform any handling of activity results not related to in-app
// billing...
super.onActivityResult(requestCode, resultCode, data);
}
else {
Log.d(TAG, "onActivityResult handled by IABUtil.");
}
}

i just found out another important thing: the requestCode that is used to launch the purchase flow has to be >= 0!

i used "new Random().nextInt()" to generate a random requestCode, and sometimes it worked, sometimes it didn't. now i found out in the following documentation, that the requestCode should not be a negative number:

http://developer.android.com/reference/android/app/Activity.html#startActivityForResult%28android.content.Intent,%20int%29

I was facing the same issue and the accepted solution was already implemented but couldn't tell what was causing this. Moving to the new Google Play Billing Library 1.0 (released on 19 September 2017) fixed the issue for me!

I hope following links will help someone who would like to implement the new library:

Google's blog post about the release

Google's youtube video

Play Billing Library Training Class

Google's Trivial Drive v2 Sample

Play Billing Library codelab published during Google I/O 2017

Play Billing Library Docs

Official reference for classes and methods

Releases notes

You need to call protected void onActivityResult(); In your parent Activity instead of MainActivity(Trivial Drive) where from you are calling your MainActivity that is Trivial Drive Activity.

you will receive resultcode values -1 if purchase successful otherwise 0.

I had the same issue and the onActivityResult was not called either.
Inspired from @Ghulam's answer I realized that the activity onActivityResult doesn't call the fragment's onActivityResult automatically so I had to do it manually.

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