2016-08-25 34 views
0

我遇到了我的應用程序應用內結算問題。我認爲上週運行良好,但我得到了意想不到的結果。Android應用內結算獲取購買問題

我有大約10個項目出售。如果購買/查詢庫存(如果購買),則每個料品都會將共享前期價值設爲true。其中一個項目是「全部購買」按鈕,購買時假設將所有其他值設置爲true。這運作良好,當我添加新的項目購買時出現問題。 「全部購買」應該也可以訪問那些,但它似乎不是。

我會盡量讓我的代碼儘可能簡單,同時還顯示所需的信息: BaseActivity.java(其中所有的應用內購買設置):

//SKU FOR our products 
static final String SKU_W31 = "workout_31"; 
static final String SKU_W32 = "workout_32"; 
static final String SKU_W37 = "workout_37"; 
static final String SKU_ALL = "all_workouts"; 

//is paid? 
public boolean m31paid = false; 
public boolean m32paid = false; 
public boolean m37paid = false; 
public boolean mAllPaid = false; 

IabHelper mHelper; 
IabBroadcastReceiver mBroadcastReceiver; 

    @Override 
protected void onCreate(@Nullable Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    SharedPreferences pref = getApplicationContext().getSharedPreferences("MyPref", 0); 
mHelper = new IabHelper(this, base64EncodedPublicKey); 
    //SET FALSE FOR LIVE APP 
    mHelper.enableDebugLogging(false); 

    mHelper.startSetup(new IabHelper.OnIabSetupFinishedListener() { 
     public void onIabSetupFinished(IabResult result) { 
      if (!result.isSuccess()) { 
       Log.d(LOG, "Problem setting up in app billing: " + result); 
      } else Log.d(LOG, "set up correctly!"); 
      if (mHelper == null) return; 

      mBroadcastReceiver = new IabBroadcastReceiver(BaseActivity.this); 
      IntentFilter broadcastFilter = new IntentFilter(IabBroadcastReceiver.ACTION); 
      registerReceiver(mBroadcastReceiver, broadcastFilter); 

      // IAB is fully set up. Now, let's get an inventory of stuff we own. 
      Log.d(LOG, "Setup successful. Querying inventory."); 

      try { 
       mHelper.queryInventoryAsync(mGotInventoryListener); 
      } catch (IabHelper.IabAsyncInProgressException e) { 
       complain("Error querying inventory. Another async operation in progress."); 
      } 
     } 
    }); 
} 

IabHelper.QueryInventoryFinishedListener mGotInventoryListener = new IabHelper.QueryInventoryFinishedListener() { 
    public void onQueryInventoryFinished(IabResult result, Inventory inventory) { 
     Log.d(LOG, "Query inventory finished."); 
     SharedPreferences.Editor editor = getSharedPreferences("my_pref", MODE_PRIVATE).edit(); 

     // Have we been disposed of in the meantime? If so, quit. 
     if (mHelper == null) return; 


Purchase w37Purchase = inventory.getPurchase(SKU_W37); 
     m37paid = (w37Purchase != null && verifyDeveloperPayload(w37Purchase)); 
     Log.d(LOG, "User has workout 37" + (m37paid ? "BOUGHT" : "NOT BOUGHT")); 
     if (w37Purchase != null) { 
      editor.putBoolean("workout37", true); 
      editor.apply(); 
     } 

Purchase w32Purchase = inventory.getPurchase(SKU_W32); 
     m32paid = (w32Purchase != null && verifyDeveloperPayload(w32Purchase)); 
     Log.d(LOG, "User has workout 32" + (m32paid ? "BOUGHT" : "NOT BOUGHT")); 
     if (w32Purchase != null) { 
      editor.putBoolean("workout32", true); 
      editor.apply(); 
     } 

Purchase w31Purchase = inventory.getPurchase(SKU_W31); 
     m31paid = (w31Purchase != null && verifyDeveloperPayload(w31Purchase)); 
     Log.d(LOG, "User has workout 31" + (m31paid ? "BOUGHT" : "NOT BOUGHT")); 
     if (w31Purchase != null) { 
      editor.putBoolean("workout31", true); 
      editor.apply(); 
     } 

Purchase wAllPurchase = inventory.getPurchase(SKU_ALL); 
     mAllPaid = (wAllPurchase != null && verifyDeveloperPayload(wAllPurchase)); 
     Log.d(LOG, "User has " + (mAllPaid ? "BOUGHT" : "NOT BOUGHT")); 
     if (wAllPurchase != null) { 
      editor.putBoolean("workout31", true); 
      editor.putBoolean("workout32", true); 
      editor.putBoolean("workout37", true); 
      editor.apply(); 
     } 

    }}; 

然後我有方法購買,我把在onclick對應的按鈕:

public void onBuy31ButtonClicked (View arg0) { 
    Log.d(LOG, "Buy 31 button clicked."); 
    if (m31paid) { 
     Toast.makeText(getApplicationContext(), R.string.already_bought, Toast.LENGTH_LONG).show(); 
     return; 
    } 
    Log.d(LOG, "launching purchase for 31"); 
    String payload = ""; 
    try { 
     mHelper.launchPurchaseFlow(this, SKU_W31, RC_REQUEST, mPurchaseFinishedListener, payload); 
    } catch (IabHelper.IabAsyncInProgressException e) { 
     Toast.makeText(getApplicationContext(), R.string.purchase_error, Toast.LENGTH_LONG).show(); 
    } 
} 
public void onBuy32ButtonClicked (View arg0) { 
    Log.d(LOG, "Buy 32 button clicked."); 
    if (m32paid) { 
     Toast.makeText(getApplicationContext(), R.string.already_bought, Toast.LENGTH_LONG).show(); 
     return; 
    } 
    Log.d(LOG, "launching purchase for 32"); 
    String payload = ""; 
    try { 
     mHelper.launchPurchaseFlow(this, SKU_W32, RC_REQUEST, mPurchaseFinishedListener, payload); 
    } catch (IabHelper.IabAsyncInProgressException e) { 
     Toast.makeText(getApplicationContext(), R.string.purchase_error, Toast.LENGTH_LONG).show(); 
    } 
} 
public void onBuy37ButtonClicked (View arg0) { 
    Log.d(LOG, "Buy 37 button clicked."); 
    if (m37paid) { 
     Toast.makeText(getApplicationContext(), R.string.already_bought, Toast.LENGTH_LONG).show(); 
     return; 
    } 
    Log.d(LOG, "launching purchase for 37"); 
    String payload = ""; 
    try { 
     mHelper.launchPurchaseFlow(this, SKU_W37, RC_REQUEST, mPurchaseFinishedListener, payload); 
    } catch (IabHelper.IabAsyncInProgressException e) { 
     Toast.makeText(getApplicationContext(), R.string.purchase_error, Toast.LENGTH_LONG).show(); 
    } 
} 
public void onBuyAllButtonClicked (View arg0) { 
    Log.d(LOG, "Buy all button clicked."); 
    if (m32paid) { 
     Toast.makeText(getApplicationContext(), R.string.already_bought, Toast.LENGTH_LONG).show(); 
     return; 
    } 
    Log.d(LOG, "launching purchase for all"); 
    String payload = ""; 
    try { 
     mHelper.launchPurchaseFlow(this, SKU_ALL, RC_REQUEST, mPurchaseFinishedListener, payload); 
    } catch (IabHelper.IabAsyncInProgressException e) { 
     Toast.makeText(getApplicationContext(), R.string.purchase_error, Toast.LENGTH_LONG).show(); 
    } 
} 

我mPurchaseFinishedListener:

IabHelper.OnIabPurchaseFinishedListener mPurchaseFinishedListener = new IabHelper.OnIabPurchaseFinishedListener() { 
    public void onIabPurchaseFinished(IabResult result, Purchase purchase) { 
     Log.d(LOG, "Purchase finished: " + result + ", purchase: " + purchase); 
     SharedPreferences.Editor editor = getSharedPreferences("my_pref", MODE_PRIVATE).edit(); 

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

     if (result.isFailure()) { 
      complain("Error purchasing: " + result); 

      return; 
     } 
     if (purchase.getSku().equals(SKU_W30)) { 
      editor.putBoolean("workout30", true); 
      editor.apply(); 
      return; 
     } 
     if (purchase.getSku().equals(SKU_W31)) { 
      editor.putBoolean("workout31", true); 
      editor.apply(); 
      return; 
     } 
     if (purchase.getSku().equals(SKU_W32)) { 
      editor.putBoolean("workout32", true); 
      editor.apply(); 
      return; 
     } 
     if (purchase.getSku().equals(SKU_W37)) { 
      editor.putBoolean("workout37", true); 
      editor.apply(); 
      return; 
     if(purchase.getSku().equals(SKU_ALL)) { 
      editor.putBoolean("workout31", true); 
      editor.putBoolean("workout32", true); 
      editor.putBoolean("workout37", true); 
      editor.apply(); 
      return; 
     } 

然後在那裏的數據是,我只是有一個if語句來檢查布爾值,例如:

xpandableListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() { 
     SharedPreferences pref = getApplicationContext().getSharedPreferences("my_pref", MODE_PRIVATE); 
     boolean w31 = pref.getBoolean("workout31", false); 
     boolean w32 = pref.getBoolean("workout32", false); 
     boolean w37 = pref.getBoolean("workout37", false); 

if (groupPosition == 2) { 
       if(w31 == true) { 
        if (childPosition == 0) { 
         Intent intent = new Intent(getApplicationContext(), WorkoutDaysActivity.class); 
         intent.putExtra("workout", "w31w1"); 
         startActivity(intent); 
        } 
        if (childPosition == 1) { 
         Intent intent = new Intent(getApplicationContext(), WorkoutDaysActivity.class); 
         intent.putExtra("workout", "w31w2"); 
         startActivity(intent); 
        } 
       }else Toast.makeText(getApplicationContext(), "Sorry, but you need to purchase these workouts from the menu.", Toast.LENGTH_LONG).show(); 
     } 

所有子項具有相同的代碼上面,切換出W31 W32與W37和。

我已經拿出了大部分購買,試圖削減代碼,並仍然得到了重點,但基本上發生了什麼是31和32前加,然後我用購買所有按鈕,他們的工作。但是我在稍後的更新中添加了37個,我的理論是當它查詢所有鍛鍊並且看到它被購買時,布爾值會改變。但實際上,當我在可擴展列表視圖中單擊37時,我會得到敬酒說需要購買的東西,當我去購買頁面並點擊「全部購買」時,我會得到祝酒說它已經被購買。

有人看到我的代碼有什麼問題嗎?非常感謝,這造成了巨大的問題!

回答

0

異步操作的原因您可能有競爭條件。您的可擴展列表視圖的活動應註冊到您的SharedPreferences中的所有更改。