在helper类中没有调用OnIabPurchaseFinishedListener

时间:2017-08-24 12:40:01

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

此刻尝试在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是为失败的购买(已经是自己的项目)而不是成功购买而调用的。

这里的任何帮助都会很棒,我尽量保持代码尽可能轻松以帮助阅读。如果我遗漏了什么,请告诉我。

1 个答案:

答案 0 :(得分:0)

我已经解决了这个问题,暂时抛弃我的助手类,并将结算任务转移到主要活动中。