【问题标题】:OnIabPurchaseFinishedListener not called in helper class辅助类中未调用 OnIabPurchaseFinishedListener
【发布时间】:2017-08-24 12:40:01
【问题描述】:

目前正在尝试在 Android 中用于应用计费。

一切正常,我可以购买商品并查询现有商品,但在成功购买期间未调用我的 OnIabPurchaseFinishedListener 侦听器。它在出现问题时调用,但不是在完成购买时调用。

我有我的主要活动,包括片段和导航抽屉。 (这里不涉及片段。我有一个助手类,所有的计费都发生在其中。

public class MainActivity extends AppCompatActivity
        implements NavigationView.OnNavigationItemSelectedListener, MyBilling.Update  {\

      MyBilling bill;
      OnCreate(){

        bill = new MyBilling(this, this);
        bill.onCreate();
      }


      private void RemoveAdsClick()
      {
          bill.purchaseRemoveAds();

      }

      public void NewPurchaseUpdate(){

        tinydb.putBoolean("Premium", true);
        nav_Menu.findItem(R.id.remove_ad_button).setVisible(false);
        displaySelectedScreen(R.id.distance_check); // Reload screen

      }


}

public class MyBilling extends Activity  {

  ////Interface
  public interface Update {
      void NewPurchaseUpdate();
  }

    // Debug tag, for logging
    static final String TAG = "XXXXXX";

    static final String SKU_REMOVE_ADS = "remove_ads";

    // (arbitrary) request code for the purchase flow
    static final int RC_REQUEST = 10111;

    // Activity
    Activity activity;

    // The helper object
    IabHelper mHelper;

    String base64EncodedPublicKey = "xxxxxxxx";
    String payload = "xxxxx";
    public boolean isAdsDisabled = false;
    public boolean AdCheck = false;

    ////Instance of interface
    Update myActivity;


    public MyBilling(Activity launcher,Update activity) {
        this.activity = launcher;
        myActivity = activity;

    }

    // User clicked the "Remove Ads" button.
public void purchaseRemoveAds() {

    activity.runOnUiThread(new Runnable() {
        @Override
        public void run() {
            mHelper.launchPurchaseFlow(activity, SKU_REMOVE_ADS,
                    RC_REQUEST, mPurchaseFinishedListener, payload);

        }
    });
}

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

    // 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.");
    }
}

IabHelper.OnIabPurchaseFinishedListener mPurchaseFinishedListener = new IabHelper.OnIabPurchaseFinishedListener()
    {
        public void onIabPurchaseFinished(IabResult result, Purchase purchase) {
            Log.d(TAG, "Purchase finished: " + result + ", purchase: "
                    + purchase);

            // if we were disposed of in the meantime, quit.
            if (mHelper == null)
                return;


            if (result.isFailure()) {
                complain("Error purchasing: " + result);
                return;
            }
            if (!verifyDeveloperPayload(purchase)) {
                complain("Error purchasing. Authenticity verification failed.");
                return;
            }

            Log.d(TAG, "Purchase successful.");


            if (purchase.getSku().equals(SKU_REMOVE_ADS)) {
                // bought the premium upgrade!
                myActivity.NewPurchaseUpdate();
                Log.d(TAG, "New Purchase Update Method was called");


            }
        }
    };






  }

一些谷歌搜索表明它可能是 onActivityResult 不在帮助程序类中的问题。所以我做了计费类扩展活动,然后添加了 onActivityResult 和 @Override 但这也不是用任何断点调用的。

如上所述,OnIabPurchaseFinishedListener 会在购买失败(已经拥有物品)时调用,但不会在成功购买时调用。

这里的任何帮助都会很棒,我已尝试使代码尽可能简洁以帮助阅读。如果我遗漏了什么,请告诉我。

【问题讨论】:

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


    【解决方案1】:

    我已经通过暂时放弃我的助手类并将计费任务移至主要活动来解决此问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-06-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多