【如何在App Starts - Play Billing 1.0中查询用户购买】得意犹堪夸世俗,诏黄新湿字如鸦。这篇文章主要讲述如何在App Starts - Play Billing 1.0中查询用户购买相关的知识,希望能为你提供帮助。
这是我的账单管理员
public class BillingManager implements PurchasesUpdatedListener {
private BillingClient mBillingClient;
private Activity mActivity;
public BillingManager(Activity activity) {
mActivity = activity;
mBillingClient = BillingClient.newBuilder(mActivity).setListener(this).build();
startServiceConnectionIfNeeded(null);
}private void startServiceConnectionIfNeeded(final Runnable executeOnSuccess) {
if (mBillingClient.isReady()) {
if (executeOnSuccess != null) {
executeOnSuccess.run();
}
} else {
mBillingClient.startConnection(new BillingClientStateListener() {
@Override
public void onBillingSetupFinished(@BillingClient.BillingResponse int billingResponse) {
if (billingResponse == BillingClient.BillingResponse.OK) {
Log.i(TAG, "onBillingSetupFinished() response: " + billingResponse);
if (executeOnSuccess != null) {
executeOnSuccess.run();
}
} else {
Log.w(TAG, "onBillingSetupFinished() error code: " + billingResponse);
}
}@Override
public void onBillingServiceDisconnected() {
Log.w(TAG, "onBillingServiceDisconnected()");
}
});
}
}
以下监听器仅在完成Google Play库时执行
@Override
public void onPurchasesUpdated(int responseCode, @Nullable List<
Purchase>
purchases) {
Log.d(TAG, "onPurchasesUpdated: ResponseCode = "+ responseCode);
Log.d(TAG, "onPurchasesUpdated: Purchase = "+purchases);
if (responseCode == BillingClient.BillingResponse.OK &
&
purchases != null) {
//if user purchased something
} else if (responseCode == BillingClient.BillingResponse.USER_CANCELED) {
// Handle an error caused by a user cancelling the purchase flow.
} else {
// Handle any other error codes.
}
}
答案除了成功的onBillingSetupFinished回调之外,在你的活动的onCreate内部甚至更好的onResume(这将防止在应用之间切换时丢失购买的潜在问题)。
对于所有类似的问题,请查看最新的TrivialDrive_v2。
例如。 queryPurchase在那里被触发here和here。
推荐阅读
- 仅针对新用户Android Play商店发布更新
- Android安装失败 - 相同的包但签名不同
- 支持Android Wear的APK必须至少具有至少23的SDK版本
- Android IAB(“刷新库存时出错(查询商品价格)”开发人员错误)
- 15个最好的开源JavaScript游戏引擎推荐
- 5个Android评分对话框小部件推荐
- 最佳树形视图(jQuery和Javascript插件)
- 最佳开源JavaScript Time Ago库
- Web应用程序的最佳jQuery调度程序和事件日历