【发布时间】:2019-05-04 17:23:06
【问题描述】:
我目前正在使用this 库在我的应用程序中实现应用内计费。我选择使用这个库是因为它很简单。
我是这样实现的:
public class MainActivity extends AppCompatActivity {
private Button buyButton;
BillingProcessor bp;
TextView freeorfull;
String LICENSE_KEY = "MyKey";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
freeorfull = (TextView) findViewById(R.id.freeorfull);
buyButton = (Button) findViewById(R.id.buyButton);
buyButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (!BillingProcessor.isIabServiceAvailable(getApplicationContext())) {
Toast.makeText(getApplicationContext(), "Please upgrade Android Market/Play store to version >= 3.9.16",
Toast.LENGTH_LONG).show();
} else {
bp.purchase(MainActivity.this, "myProductKey");
}
}
});
buyit();
if (bp.isPurchased("mybuyfullversion")) {
freeorfull.setText("FULL oncreate");
}
}
public void buyit() {
bp = new BillingProcessor(this, LICENSE_KEY, new BillingProcessor.IBillingHandler() {
@Override
public void onProductPurchased(@NonNull String productId, @Nullable TransactionDetails details) {
freeorfull.setText("FULL in buyit");
Toast.makeText(getApplicationContext(), "onProductPurchased",
Toast.LENGTH_LONG).show();
}
@Override
public void onBillingError(int errorCode, @Nullable Throwable error) {
Toast.makeText(getApplicationContext(), "onBillingError",
Toast.LENGTH_LONG).show();
if (bp.isPurchased("mybuyfullversion")) {
freeorfull.setText("FULL oncreate");
}
}
@Override
public void onBillingInitialized() {
Toast.makeText(getApplicationContext(), "onBillingInitialized",
Toast.LENGTH_LONG).show();
}
@Override
public void onPurchaseHistoryRestored() {
Toast.makeText(getApplicationContext(), "onPurchaseHistoryRestored",
Toast.LENGTH_LONG).show();
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (!bp.handleActivityResult(requestCode, resultCode, data)) {
super.onActivityResult(requestCode, resultCode, data);
}
}
@Override
public void onDestroy() {
if (bp != null) {
bp.release();
}
super.onDestroy();
}
}
我尝试从 Google Play 管理中心测试退款用户,一段时间后购买被退款并且用户(我)收到退款,但应用程序仍然显示该项目已购买。
我正在寻找一种方法来检查用户是否已获得退款,然后将 UI/TextView 改回“免费版”。
我已经看到可以使用以下内容:
Purchase removeAdsPurchase = inventory.getPurchase(SKU_REMOVE_ADS);
if(removeAdsPurchase != null) {
int purchaseStateForRemoveAds = removeAdsPurchase.getPurchaseState();
if(purchaseStateForRemoveAds == 1) {
//Do cancelled purchase stuff here
}
else if(purchaseStateForRemoveAds == 2) {
//Do refunded purchase stuff here
}
}
但是这个库没有Purchaseclass,我不明白库存指的是什么。
据我了解,我应该让 PurchaseState 检查购买是否已退款。我应该如何进行?
【问题讨论】:
标签: android in-app-purchase android-library