android - 检查 Google 应用内购买库存
问题描述
我是编码新手,我主要是在应用计费中从谷歌复制和粘贴任何人都可以指导我解决代码中的问题。请参阅此 Android 检查是否之前购买了应用内购买
这是我的购买活动
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:应用内:检查商品是否已购买
哪种方法更容易或更有效?
解决方案
问题 1 答案
放置在 IAB 内
@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()) {
Log.d("TAG", "onIabSetupFinished: ");
// 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.");
}
}
else {
// Oh no, there was a problem.
Log.d("TAG", "Problem setting up In-app Billing: " + result);
}
}
});
}
问题 2 答案
取决于我们销售的商品类型。
对于一次性交易,并且无法进一步购买该商品,我使用了 queryInventoryAsync。
推荐阅读
- matlab - 转置 double 和 int64 值的表,无需 int64 转换
- json - 将特定实体发送到 FIWARE QuantumLeap
- javascript - discord.js v12 向第一个频道发送消息
- mysql - 当列和表被定义时,列是未知的?
- php - 如何打印 SimpleXMLElement 对象的特定数组元素
- oracle - 表中的分层首选项
- python - pymongo - “OperationFailure:管道阶段规范对象必须包含一个字段”
- python - C# 返回“C:\Windows\Sysnative”作为无效目录,但 python 没有
- typescript - 在 Typescript 中控制类型级别的“评估”
- lua - 使用 fiber.yield() 和 fiber.testcancel() 的 Tarantool 纤维行为