查看Google In App购买清单

时间:2018-07-11 07:29:10

标签: android

我是编码的新手,我主要是从Google复制并粘贴到应用帐单中 谁能指导我代码中的错误。推荐这个 Android check if in app purchase was bought before

这是我的购买活动

public class PurchaseActivity extends CustomMenu {
tk.myessentialoils.ideasapp.util.IabHelper mHelper;
Button buyButton;
String product1SKU;
Boolean VIP=false;
//String product1SKU = "vip_member";//getResources().getString(R.string.product1SKU);

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_purchase);

    buyButton = findViewById(R.id.buyButton);
    product1SKU = getString(R.string.product1SKU);
    mHelper = new tk.myessentialoils.ideasapp.util.IabHelper(this, getString(R.string.billing64basecode));

    mHelper.startSetup(new tk.myessentialoils.ideasapp.util.IabHelper.OnIabSetupFinishedListener() {
        public void onIabSetupFinished(tk.myessentialoils.ideasapp.util.IabResult result) {
            if (!result.isSuccess()) {
                // Oh no, there was a problem.
                Log.d("TAG", "Problem setting up In-app Billing: " + result);
            }
            // Hooray, IAB is fully set up!
        }
    });
/*
        try {
            mHelper.queryInventoryAsync(mGotInventoryListener);
        } catch (IabHelper.IabAsyncInProgressException e) {
            Log.d("TAG","Error querying inventory. Another async operation in progress.");
        }

        SharedPreferences.Editor editor = this.getSharedPreferences("Name", MODE_PRIVATE).edit();
        Log.d("TAG", "checkVIPStatus: " +VIP);
        editor.putBoolean("VIP",VIP);
        editor.apply();
*/
}

@Override
public void onDestroy() {
    super.onDestroy();
    if (mHelper != null) try {
        mHelper.dispose();
    } catch (tk.myessentialoils.ideasapp.util.IabHelper.IabAsyncInProgressException e) {
        e.printStackTrace();
    }
    mHelper = null;
}

public void buyClick(View view) {
    try {
        mHelper.launchPurchaseFlow(this, product1SKU, 10001,
                mPurchaseFinishedListener, "mypurchasetoken");
    } catch (tk.myessentialoils.ideasapp.util.IabHelper.IabAsyncInProgressException e) {
        e.printStackTrace();
        Toast.makeText(this, "Please set up your google account", Toast.LENGTH_SHORT).show();
    }
}

@Override
protected void onActivityResult(int requestCode, int resultCode,
                                Intent data)
{
    if (!mHelper.handleActivityResult(requestCode,
            resultCode, data)) {
        super.onActivityResult(requestCode, resultCode, data);
    }
}

tk.myessentialoils.ideasapp.util.IabHelper.OnIabPurchaseFinishedListener mPurchaseFinishedListener
        = new tk.myessentialoils.ideasapp.util.IabHelper.OnIabPurchaseFinishedListener() {
    public void onIabPurchaseFinished(tk.myessentialoils.ideasapp.util.IabResult result,
                                      tk.myessentialoils.ideasapp.util.Purchase purchase)
    {
        if (mHelper == null) return;
        if (result.isFailure()) {
            // Handle error
            Log.d("TAG", "onQueryInventoryFinished: Failed");
        }
        else if (purchase.getSku().equals(product1SKU)) {
            buyButton.setEnabled(false);
            VIP=true;
            storeVIP();
        }

    }
};

tk.myessentialoils.ideasapp.util.IabHelper.QueryInventoryFinishedListener mGotInventoryListener
        = new tk.myessentialoils.ideasapp.util.IabHelper.QueryInventoryFinishedListener() {
    public void onQueryInventoryFinished(tk.myessentialoils.ideasapp.util.IabResult result,
                                         tk.myessentialoils.ideasapp.util.Inventory inventory) {

        if (result.isFailure()) {
            // handle error here
            Log.d("TAG", "onQueryInventoryFinished: Failed");
        }
        else {
            // does the user have the premium upgrade?
            boolean mIsPremium = inventory.hasPurchase(product1SKU);
            buyButton.setEnabled(false);// update UI accordingly
            VIP=true;
            storeVIP();

        }

    }
};

private void storeVIP(){
    SharedPreferences.Editor editor = getSharedPreferences("Name", MODE_PRIVATE).edit();
    Log.d("TAG", "checkVIPStatus: " +VIP);
    editor.putBoolean("VIP",VIP);
    editor.apply();
}

}

问题1
启用注释的代码后,我的购买活动崩溃了。
mHelper.queryInventoryAsync(mGotInventoryListener);
出现错误,未设置IAB。
谁能告诉我这是怎么了?

问题2
我应该使用queryInventoryAsync还是使用此处所述的方法?
Android: in-app: check if an item has been purchased
哪种方法更简单或更有效?

1 个答案:

答案 0 :(得分:0)

问题1回答
放置在IAB中

   <!-- <filter>

      <filter-name>RestAuthenticationFiler</filter-name>

       <filter-class>io.antmedia.serverapp.pscp.filter.RestAuthenticationFilter</filter-class>

   </filter>

   <filter-mapping>

      <filter-name>RestAuthenticationFiler</filter-name>

        <url-pattern>/rest/*</url-pattern>

    </filter-mapping>-->

问题2回答
这取决于我们出售的商品类型。
对于一次交易,无法再购买该物品,我使用了queryInventoryAsync。