2017-08-24 24 views
0

試圖在Android中的應用帳單中使用。OnIabPurchaseFinishedListener目前未在助手類中調用

一切工作正常,我可以購買項目和查詢現有的,但我的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被調用失敗的購買(已經擁有的項目),但不是成功的。

任何這裏的幫助會很好,我儘量保持代碼儘可能輕o幫助閱讀。如果我錯過任何東西,請告訴我。

回答

0

我已經解決了這個問題,現在先刪除我的助手類,然後將結算任務移到主要活動中。