成功购买后,In App Purchase不会更新UI

时间:2018-07-29 06:13:43

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

我所在的国家/地区刚刚加入了Google Play商家的批准。 今天,我在自己的游戏中实现了In App Billing,我想知道为什么UI没有更新:

  1. 我希望购买后开始游戏按钮为“ View.VISIBLE”
  2. 购买后我希望“购买游戏”按钮为“ View.GONE”

购买测试成功,但用户界面未按我预期的那样更新。 有人知道这个问题吗? 下面是我的完整代码。非常感谢。

public class mygamebilling extends AppCompatActivity {

private static final String TAG = "MyGameBilling";
static final String SKU_PREMIUM = "android.test.purchased";
static final int RC_REQUEST = 10001;
IabHelper mHelper;

private ImageButton startGame_Btn, upgradeGame_Btn;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mygamebilling_gui);

String base64EncodedPublicKey = "abc123456789";
mHelper = new IabHelper(this, base64EncodedPublicKey);

mHelper.startSetup(new IabHelper.OnIabSetupFinishedListener()
{
        public void onIabSetupFinished(IabResult result)
        {
            if (!result.isSuccess())
            {
                Log.d(TAG, "In-app Billing setup failed: " + result);
            } else {
                Log.d(TAG, "In-app Billing is set up OK");
                mHelper.enableDebugLogging(true, TAG);
                }
            }
    });
startGame_Btn = findViewById(R.id.startgame);
startGame_Btn.setVisibility(View.GONE);
upgradeGame_Btn = findViewById(R.id.buygame);
}

public void upgradeGame_Btn(View arg0)
{
    //Buy Full Version Dialog
        buyFullVersion_Dialog();
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
    if (!mHelper.handleActivityResult(requestCode, resultCode, data))
    {
        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)
    {
        if (result.isFailure())
        {
            return;
        }
        else if (purchase.getSku().equals(SKU_PREMIUM))
        {
            consumeItem();
            upgradeGame_Btn.setVisibility(View.GONE);
        }
    }
};

public void consumeItem()
{
     mHelper.queryInventoryAsync(mGotInventoryListener);
}

IabHelper.QueryInventoryFinishedListener mGotInventoryListener = new IabHelper.QueryInventoryFinishedListener()
{
    public void onQueryInventoryFinished(IabResult result, Inventory inventory)
    {
        if (result.isFailure())
        {
        }
        else
        {
            mHelper.consumeAsync(inventory.getPurchase(SKU_PREMIUM), mConsumeFinishedListener);
        }
    }
};

IabHelper.OnConsumeFinishedListener mConsumeFinishedListener = new IabHelper.OnConsumeFinishedListener()
{
    public void onConsumeFinished(Purchase purchase, IabResult result)
    {
        if (result.isSuccess()) 
        {
            startGame_Btn.setVisibility(View.VISIBLE);
        }
        else 
        {
            //Handle Error
        }
    }
};

@Override
public void onDestroy()
{
    super.onDestroy();
    if (mHelper != null) mHelper.dispose();
    mHelper = null;
}
}

0 个答案:

没有答案