2013-10-22 50 views
0

好的,在這裏我的GUI是suppossed更新時,購買完成。我正在使用android.test.purchase和GUI不更新,我應該擔心或不?購買inApp賬單後GUI沒有更新更新android

變量

   IabHelper mHelper; 


static final String TAG = "com.back.to.school.zone.readingLevelPicker"; 

// SKUs for our products: the premium upgrade (non-consumable) 
static final String SKU_PREMIUM = "android.test.refunded"; 

// Does the user have the premium upgrade? 
boolean mIsPremium = false; 

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

onCreate方法

     String base64EncodedPublicKey = "<my key is in here>"; 
    mHelper = new IabHelper(this, base64EncodedPublicKey); 

    //It is recommended to add more security than just pasting it in your source code; 

    Log.d(TAG, "Starting setup."); 
    mHelper.startSetup(new IabHelper.OnIabSetupFinishedListener() { 
    public void onIabSetupFinished(IabResult result) { 
    Log.d(TAG, "Setup finished."); 

    if (!result.isSuccess()) { 
    // Oh noes, there was a problem. 
    Log.d(TAG, "Problem setting up In-app Billing: " + result); 
    } 
    // Hooray, IAB is fully set up! 
    mHelper.queryInventoryAsync(mGotInventoryListener); 
    } 
    }); 

一個按鈕來購買需要的物品

   buyButton = (Button) findViewById(R.id.buyButtonS); 
    buyButton.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      // TODO Auto-generated method stub 
      mHelper.launchPurchaseFlow(readingLevelPicker.this, SKU_PREMIUM, 10001, 
         mPurchaseFinishedListener, ""); 
     } 

一個需要的按鈕啓用和的結束mLevel10

mLevel10.setEnabled(false); 
    mLevel10.setOnClickListener(new OnClickListener() { 
     public void onClick(View v) { 
      Intent myIntent = new Intent(v.getContext(), 
        readingMode10.class); // Coming soon 
      startActivityForResult(myIntent, 0); 

     } 
    }); 


} //end of oncreate method 



IabHelper.QueryInventoryFinishedListener mGotInventoryListener = new IabHelper.QueryInventoryFinishedListener() { 
    public void onQueryInventoryFinished(IabResult result, Inventory inventory) { 
    Log.d(TAG, "Query inventory finished."); 
    if (result.isFailure()) { 
    Log.d(TAG, "Failed to query inventory: " + result); 
    return; 
    } 
    else { 
    Log.d(TAG, "Query inventory was successful."); 
    // does the user have the premium upgrade? 
    mIsPremium = inventory.hasPurchase(SKU_PREMIUM); 

    // update UI accordingly 

    mLevel6 = (Button) findViewById(R.id.level6); 
    mLevel7 = (Button) findViewById(R.id.level7); 
    mLevel8 = (Button) findViewById(R.id.level8); 
    mLevel9 = (Button) findViewById(R.id.level9); 
    mLevel10 = (Button) findViewById(R.id.level10); 

    mLevel6.setEnabled(true); 
    mLevel7.setEnabled(true); 
    mLevel8.setEnabled(true); 
    mLevel9.setEnabled(true); 
    mLevel10.setEnabled(true); 
    Log.d(TAG, "User is " + (mIsPremium ? "PREMIUM" : "NOT PREMIUM")); 
    } 

    Log.d(TAG, "Initial inventory query finished; enabling main UI."); 
    } 
    }; 

    IabHelper.OnIabPurchaseFinishedListener mPurchaseFinishedListener = new IabHelper.OnIabPurchaseFinishedListener() { 
    public void onIabPurchaseFinished(IabResult result, Purchase purchase) { 
    if (result.isFailure()) { 
    Log.d(TAG, "Error purchasing: " + result); 
    return; 
    } 
    else if (purchase.getSku().equals(SKU_PREMIUM)) { 
    // give user access to premium content and update the UI 

     mLevel6 = (Button) findViewById(R.id.level6); 
     mLevel7 = (Button) findViewById(R.id.level7); 
     mLevel8 = (Button) findViewById(R.id.level8); 
     mLevel9 = (Button) findViewById(R.id.level9); 
     mLevel10 = (Button) findViewById(R.id.level10); 

     mLevel6.setEnabled(true); 
     mLevel7.setEnabled(true); 
     mLevel8.setEnabled(true); 
     mLevel9.setEnabled(true); 
     mLevel10.setEnabled(true); 
    } 
    } 
    }; 

    @Override 
    public void onActivityResult(int requestCode, int resultCode, Intent data) { 
    super.onActivityResult(requestCode, resultCode, data); 

    Log.d(TAG, "onActivityResult(" + requestCode + "," + resultCode + "," 
    + data); 

    // Pass on the activity result to the helper for handling 
    if (!mHelper.handleActivityResult(requestCode, resultCode, data)) { 
    super.onActivityResult(requestCode, resultCode, data); 

    } else { 
    Log.d(TAG, "onActivityResult handled by IABUtil."); 
    } 
    } 

一些意見後創建方法 - 從我瞭解一切工作儘可能的計費方式使用機器人時,因爲去。測試。購買它說支付完成。但用戶界面不啓用文本框,因此使文本框被禁用,我嘗試在位置mLevel7.setEnabled(「true」)處拋出一個斷點,但它不顯示任何不尋常的東西,沒有任何失敗提供了UI沒有更新?爲什麼?

+0

請在onCreate()方法mLevel6,7,8,9,10中定義您的按鈕變量。只有setEnable()方法的代碼應該位於onInventoryFinish或onIabPurchaseFinished Listener中。並且在onCreate()中將所有按鈕設置爲禁用。 – Maulik

回答

2

確保你不是測試的queryInvetory的方法,因爲它會工作,只有當你試圖購買真正的產品,而不是虛擬產品(android.test.purchased),因爲谷歌不會保留紀錄爲虛擬產品據我所知。

onCreate()方法

定義所有變量在OnCreate方法和只是改變文本框 在queryInventory和IabPurchaseFinishListener使能禁用。

mLevel6 = (Button) findViewById(R.id.level6); 
mLevel7 = (Button) findViewById(R.id.level7); 
mLevel8 = (Button) findViewById(R.id.level8); 
mLevel9 = (Button) findViewById(R.id.level9); 
mLevel10 = (Button) findViewById(R.id.level10); 

QueryInventoryFinishedListener

IabHelper.QueryInventoryFinishedListener mGotInventoryListener = new IabHelper.QueryInventoryFinishedListener() { 
public void onQueryInventoryFinished(IabResult result, Inventory inventory) { 
Log.d(TAG, "Query inventory finished."); 
if (result.isFailure()) { 
Log.d(TAG, "Failed to query inventory: " + result); 
return; 
} 
//else {    // you don't need to check for the failure then go if and other wise else. 
Log.d(TAG, "Query inventory was successful."); 
// does the user have the premium upgrade? 
mIsPremium = inventory.hasPurchase(SKU_PREMIUM); 

// update UI accordingly 

if(mIsPremium){ 

    mLevel6.setEnabled(true); 
    mLevel7.setEnabled(true); 
    mLevel8.setEnabled(true); 
    mLevel9.setEnabled(true); 
    mLevel10.setEnabled(true); 
} 

Log.d(TAG, "User is " + (mIsPremium ? "PREMIUM" : "NOT PREMIUM")); 
// } 

Log.d(TAG, "Initial inventory query finished; enabling main UI."); 
} 
}; 

OnIabPurchaseFinishedListener

IabHelper.OnIabPurchaseFinishedListener mPurchaseFinishedListener = new IabHelper.OnIabPurchaseFinishedListener() { 
    public void onIabPurchaseFinished(IabResult result, Purchase purchase) { 
    if (result.isFailure()) { 
    Log.d(TAG, "Error purchasing: " + result); 
    return; 
    } 
    if (purchase.getSku().equals(SKU_PREMIUM)) { // same as above you don't need to check else if condition again 
    // give user access to premium content and update the UI 

     mLevel6.setEnabled(true); 
     mLevel7.setEnabled(true); 
     mLevel8.setEnabled(true); 
     mLevel9.setEnabled(true); 
     mLevel10.setEnabled(true); 
    } 
    } 
    }; 

讓我知道它是爲你工作或沒有。

+0

我已經把它放在OnIabPurchaseFinishedListener之前,甚至當這個過程沒有通過它解鎖所有的級別?這是因爲這是一個測試嗎? –

+0

實際上ijust再 - 看你彪什麼,因爲我覺得你的代碼做了一個錯字,在格蘭查詢監聽器,你要我讓所有的文本框,從啓用到的setEnabled(假),而不是真正的喜歡你有以上?對? –

+0

將此添加到正確的位置對我而言非常感謝! –