Google Pay 概念
- 只能用来销售数字内容,不能销售实体商品
- 应用一旦被购买,无法进行退款服务
- Google Play不提供内容交付,开发者需要自行交付在应用内购买的数字内容
- 一个应用不能购买另一个应用发布的商品
开发备忘
混淆时添加
-keep class com.android.vending.billing.**
清单文件添加com.android.vending.BILLING 权限
配置应用的key,初始化Googlepay的时候使用,key的位置
代码编写
- 谷歌提供的sample中有一个aidl文件和util包下的工具类可以使用,功能已经封装好
- AIDL文件创建com.android.vending.billing的包将IInAppBillingService.aidl放进去(之后进行编译会在gen对应包名下面生成IInAppBillingService.java)
- com.android.vending.BILLING 权限添加
此后就可以访问google服务了
- 初始化尝试连接google服务,如果连接成功就进行消耗性商品的账单查询是否有没有消耗的商品(防止漏单)
mHelper = new IabHelper(this, GOOGLE_PAY_key);
// 打开log日志
mHelper.enableDebugLogging(AppConfigs.isDebug(this));
Mlog.e("google pay 开始初始化.", mContext);
// // ### Google play连接初始化开始 ### 异步回调
mHelper.startSetup(new OnIabSetupFinishedListener() {
@Override
public void onIabSetupFinished(IabResult result) {
Mlog.e("google pay 初始化完成.", mContext);
// 连接过程中出现问题
if (!result.isSuccess()) {
//
Mlog.e("谷歌服务未连接上Problem setting up in-app billing: "
+ result, mContext);
// add 当未连接上谷歌服务的时候就结束,不然在没有google服务的手机上ondestory()中断服务时会出非法状态异常
mHelper = null;
return;
}
// 异步过程,连接过程中就结束
if (mHelper == null)
return;
// ### Google play连接初始化结束 ###
// 查询消耗商品的拥有 状态
Mlog.e("谷歌服务连接成功!开始查询消耗商品的购买状态", mContext);
try {
mHelper.queryInventoryAsync(mGotInventoryListener);
} catch (IabAsyncInProgressException e) {
Mlog.e("查询用户物品清单失败. Another async operation in progress.",
mContext);
}
}
});
- 由于此连接耗费性能,所以需要在合适地方中断链接
@Override
protected void onDestroy