in-app-billing for google play (google应用内付费 v3)
来源:互联网 发布:java爬虫爬取视频 编辑:程序博客网 时间:2024/05/18 01:42
详细文档参考: https://developer.android.com/google/play/billing/billing_integrate.html#billing-add-aidl
Google的In-app Billing, V3版本的接口是google发布的最新充值接口,支持以服务的形式异步的与Google Play进行交互,更加安全可靠,减少交易中的不确定因素造成的损失。 但是暂时只支持 In-app Products,不支持订阅。基本步骤如下:
将 IInAppBillingService.aidl文件拷贝到src文件夹下,正常的话会在gen目录下生成对应的.java文件
在AndroidManifest.xml 文件中添加如下权限
<uses-permission android:name="com.android.vending.BILLING" />
创建一个 ServiceConnection 并将其与 IInAppBillingService绑定。这个是最核心的功能,用于与Google Play进行通信。详细代码参考IabHelper.java 核心代码如下:
IInAppBillingService mService;ServiceConnection mServiceConn = new ServiceConnection() { @Override public void onServiceDisconnected(ComponentName name) { mService = null; } @Override public void onServiceConnected(ComponentName name, IBinder service) { mService = IInAppBillingService.Stub.asInterface(service); }};@Overridepublic void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); bindService(new Intent("com.android.vending.billing.InAppBillingService.BIND"), mServiceConn, Context.BIND_AUTO_CREATE);@Overridepublic void onDestroy() { super.onDestroy(); if (mServiceConn != null) { unbindService(mServiceConn); }}
一旦成功与Google Play建立连接,就可以使用上面创建的service与其进行通信。比如获取商品信息,发布购买请求等等。获取商品信息的代码示例(注意,不要在主线程调用此代码,涉及网络请求可能会阻塞主线程,应该另开一个线程调用):
ArrayList skuList = new ArrayList();skuList.add("premiumUpgrade");skuList.add("gas");Bundle querySkus = new Bundle();querySkus.putStringArrayList(“ITEM_ID_LIST”, skuList);Bundle skuDetails = mService.getSkuDetails(3, getPackageName(), "inapp", querySkus);int response = skuDetails.getInt("RESPONSE_CODE");if (response == 0) { ArrayList responseList = skuDetails.getStringArrayList("DETAILS_LIST"); for (String thisResponse : responseList) { JSONObject object = new JSONObject(thisResponse); String sku = object.getString("productId"); String price = object.getString("price"); if (sku.equals("premiumUpgrade")) mPremiumUpgradePrice = price; else if (sku.equals("gas")) mGasPrice = price; }}
申请购买商品:
// 购买代码Bundle buyIntentBundle = mService.getBuyIntent(3, getPackageName(), sku, "inapp", "bGoa+V7g/yqDXvKRqq+JTFn4uQZbPiQJo4pf9RzJ");PendingIntent pendingIntent = buyIntentBundle.getParcelable("BUY_INTENT");startIntentSenderForResult(pendingIntent.getIntentSender(), 1001, new Intent(), Integer.valueOf(0), Integer.valueOf(0), Integer.valueOf(0));// 结果返回Overrideprotected void onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode == 1001) { int responseCode = data.getIntExtra("RESPONSE_CODE", 0); String purchaseData = data.getStringExtra("INAPP_PURCHASE_DATA"); String dataSignature = data.getStringExtra("INAPP_DATA_SIGNATURE"); if (resultCode == RESULT_OK) { try { JSONObject jo = new JSONObject(purchaseData); String sku = jo.getString("productId"); alert("You have bought the " + sku + ". Excellent choice, adventurer!"); } catch (JSONException e) { alert("Failed to parse purchase data."); e.printStackTrace(); } } }}// 返回结果示例'{ "orderId":"12999763169054705758.1371079406387615", "packageName":"com.example.app", "productId":"exampleSku", "purchaseTime":1345678900000, "purchaseState":0, "developerPayload":"bGoa+V7g/yqDXvKRqq+JTFn4uQZbPiQJo4pf9RzJ", "purchaseToken":"rojeslcdyyiapnqcynkjyyjh" }'
设置物品已消耗(默认情况下,物品一旦被购买就属于用户,不允许再次被购买,如果是消耗型商品,比如货币或者装备,就需要向Google Play发送信息通知该物品已被消耗。注意,这个步骤要在真正提供商品之前进行,之后正确的获取到返回值才提供商品给玩家。)
int response = mService.consumePurchase(3, getPackageName(), token);
Handle In-app Billing responses from Google Play.
调试不成功报错:Error checking for billing v3 support. (response: 3:Billing Unavailable)
参考:http://zengrong.net/post/1801.htm
- in-app-billing for google play (google应用内付费 v3)
- google in-app-billing 应用内付费的简介与实现(转)
- Google Play In-app Billing
- Google Play In-app Billing
- Google Play In-app Billing
- Google Play In-app Billing
- google play in app billing
- Google Play In-app Billing
- Google Play In-app Billing
- Google Play In-app Billing
- Google in app billing 应用内支付
- google in-app-billing 应用内付费的简介与实现
- google应用内支付 in-app billing(二)
- Google Play In-app Billing错误问题
- Google play billing(Google play 内支付)
- Google play billing(Google play 内支付)
- google内购In-App Billing
- google内购In-App Billing
- ubuntu下文件转码工具enca
- 数据库对象
- 第一个Ant脚本
- 使用FilenameFilter
- centos 安装 oracle 11g R2 开机启动配置
- in-app-billing for google play (google应用内付费 v3)
- Android Studio加载第三方jar包 + 中文乱码
- winform拖动窗体
- 查找附近点--Geohash方案讨论
- linux操作系统实验四--打造自己的Linux发行版(续)
- V$ACTIVE_SESSION_HISTORY视图的列说明
- 比较图像处理中图像的匹配,图像 配准, 图像融合的概念上的不同
- 非常不错的学习博客
- _blank失效问题解决