我已经成功实施应用程式账单到我的应用程序。该IAP成功地工作,我测试,以确保其工作。
当用户点击一个按钮,他们必须做出的IAP进行。然而,每次用户点击它启动IAP按钮,即使他们已经取得了IAP。我希望我的IAP是非消耗性明显。目前,我存储在IAP SharedPreferences
,但如果用户重新安装应用程序,他们失去了IAP。
那么,如何可以使用getPurchases()
或restoreTransactions()
我onCreate
或onClick
方法来检查用户是否有购买的具体项目?我已经找遍了互联网,通过这么多的样本看,它似乎并没有工作,也许我误解虽然。
如果你需要我张贴任何代码,请你和我会更新我的职务。
使用这个库:
https://github.com/anjlab/android-inapp-billing-v3
如何使用?
在您的gradle这个使用这个:
repositories {
mavenCentral()
}
dependencies {
implementation 'com.anjlab.android.iab.v3:library:1.0.44'
}
应用内结算清单的权限:
<uses-permission android:name="com.android.vending.BILLING" />
如何利用图书馆的方法:
public class SomeActivity extends Activity implements BillingProcessor.IBillingHandler {
BillingProcessor bp;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bp = new BillingProcessor(this, "YOUR LICENSE KEY FROM GOOGLE PLAY CONSOLE HERE", this);
bp.initialize();
// or bp = BillingProcessor.newBillingProcessor(this, "YOUR LICENSE KEY FROM GOOGLE PLAY CONSOLE HERE", this);
// See below on why this is a useful alternative
}
// IBillingHandler implementation
@Override
public void onBillingInitialized() {
/*
* Called when BillingProcessor was initialized and it's ready to purchase
*/
}
@Override
public void onProductPurchased(String productId, TransactionDetails details) {
/*
* Called when requested PRODUCT ID was successfully purchased
*/
}
@Override
public void onBillingError(int errorCode, Throwable error) {
/*
* Called when some error occurred. See Constants class for more details
*
* Note - this includes handling the case where the user canceled the buy dialog:
* errorCode = Constants.BILLING_RESPONSE_RESULT_USER_CANCELED
*/
}
@Override
public void onPurchaseHistoryRestored() {
/*
* Called when purchase history was restored and the list of all owned PRODUCT ID's
* was loaded from Google Play
*/
}
}
注:onPurchaseHistoryRestored调用时初始化BillingProcessor只有第一次