前言

  Google Play应用商店在国外Android市场中地位基本与AppStore在IOS中的地位一致,为此考虑国外的应用时,Android首要考虑的是接入GooglePlay的排行榜等支持。

同样的由于Google未进入大陆市场,在大陆还是需要VPN才可以访问这些服务。

登录

  官方文档: https://developers.google.com/games/services/android/quickstart

  1、设置 AndroidManifest.xml中的标签项。

   <meta-data android:name="com.google.android.gms.games.APP_ID"android:value="@string/app_id" /><meta-data android:name="com.google.android.gms.version"android:value="@integer/google_play_services_version"/>

  2、在res\values\string.xml中添加应用的app_id。

<?xml version="1.0" encoding="utf-8"?>
<resources><string name="app_name">XXX_NAME</string><string name="app_id">10XXXXXXXXXX</string>
</resources>

  app_id 需要在google play 开发控制台添加对应的应用获取,网址:https://play.google.com/apps/publish

在游戏服务中添加新游戏,点进游戏项即可在游戏名下看到app_id,如下图:

              

  3、登录并设置监听回调。直接new 新的监听添加到 mGoogleApiClient 上。

     mGoogleApiClient = new GoogleApiClient.Builder(activity).addConnectionCallbacks(new GoogleApiClient.ConnectionCallbacks() {@Overridepublic void onConnected(Bundle bundle) {OnConnected();}@Overridepublic void onConnectionSuspended(int i) {Log.i(TAG, "onConnectionSuspended");}}).addOnConnectionFailedListener(new GoogleApiClient.OnConnectionFailedListener() { @Overridepublic void onConnectionFailed(com.google.android.gms.common.ConnectionResult connectionResult) {Log.i(TAG, "onConnectionFailed  getErrorCode:" + connectionResult.getErrorCode());if (connectionResult.hasResolution()) {try {// !!!connectionResult.startResolutionForResult(_gameActivity, RC_SIGN_IN);} catch (SendIntentException e) {mGoogleApiClient.connect();}}}}).addApi(Plus.API).addScope(Plus.SCOPE_PLUS_LOGIN).addApi(Games.API).addScope(Games.SCOPE_GAMES).build(); 

  回调设置原因如所述: http://stackoverflow.com/questions/22935861/android-implementing-google-plus-login-error-on-mconnectionresult-hasresolution  

大致是,登录时会先判断应用是否已有帐号登录,有的话回调登录成功onConnected,否则会转到 onConnectionFailed,此时会返回一个connectionResult可以用来新建一个登录窗口。

  

  4、直接登录测试。

    // 登录public static void login(){Log.i(TAG, "login");if (mGoogleApiClient.isConnected()){OnConnected();}else{mGoogleApiClient.connect();}}

测试登录注意事项:

  1、将测试帐号在https://play.google.com/apps/publish 加入到应用的测试帐号里。

  2、如果手机Google Play已经登录帐号没退出,且该帐号不是测试帐号,会出现登录界面闪下消失。

为了做帐号区分需要获取帐号信息,需要添加AndroidManifest.xml标签项。

<!-- To access accounts configured on device -->
<uses-permission android:name="android.permission.GET_ACCOUNTS" />

然后用 如下代码获取名字:

 mAccountName = Plus.AccountApi.getAccountName(mGoogleApiClient);

排行榜

  官网地址: https://developers.google.com/games/services/android/leaderboards

  1、首先接入上述的登录。

  2、在Google Play应用开发后台中的应用添加新的排行榜,得到排行榜ID LEADERBOARD_ID 如下图:

                  

    3、显示排行榜,代码如下:

    private static final String LEADERBOARD_ID = "XXXXXXXXXX";private static final int REQUEST_LEADERBOARD =100;// 显示排行榜public static void showLeaderboards(){Log.i(TAG, "showLeaderboards");if (mGoogleApiClient.isConnected()){_gameActivity.startActivityForResult(Games.Leaderboards.getLeaderboardIntent(mGoogleApiClient,LEADERBOARD_ID),REQUEST_LEADERBOARD);}else{mGoogleApiClient.connect();}}

   4、提交分数

    // 排行榜 提交public static void commit(long score){if (!mGoogleApiClient.isConnected()){ mGoogleApiClient.connect();return;}Log.i(TAG, "commit " + score); Games.Leaderboards.submitScore(mGoogleApiClient,LEADERBOARD_ID,score);}

    5、登录后获取原来的排行榜数据

    public static void OnConnected(){mAccountName = Plus.AccountApi.getAccountName(mGoogleApiClient);Log.i(TAG, "onConnected "+ mAccountName);Games.Leaderboards.loadCurrentPlayerLeaderboardScore(mGoogleApiClient,LEADERBOARD_ID,LeaderboardVariant.TIME_SPAN_ALL_TIME, LeaderboardVariant.COLLECTION_PUBLIC).setResultCallback(new ResultCallback<LoadPlayerScoreResult>() {@Overridepublic void onResult(LoadPlayerScoreResult arg0) {LeaderboardScore c = arg0.getScore();if(c!=null){Log.i(TAG, "onResult " +  c.getDisplayScore() + " rawScore:" + c.getRawScore());mAccountScore =  c.getRawScore();}loginCallback();}});}

支付

  官网地址: http://developer.android.com/training/in-app-billing/preparing-iab-app.html

  1、从SDK Manager中下载安装对应Google 服务,如下:

      

      

  2、从对应的sdk路径下 ~sdk\extras\google\play_billing  拷贝  IInAppBillingService.aidl  到 游戏目录下 ~proj.android\src\com\android\vending\billing

  3、将sample\TrivialDrive\src\com\example\android\trivialdrivesample\util 整个目录拷贝到游戏目录下并修改目录下文件的响应引用库路径。

  4、在AndroidManifest.xml添加对应的权限,注意大小写。

<uses-permission android:name="com.android.vending.BILLING" />

  5、在Google Play 商店添加商品信息。

     a、生成一个添加好上述第4点的权限的带有签名的release包,可参考cocos2dx - android环境配置及编译。

     b、将生成的包上传到Google Play商店对应应用的Apk项。并将其关联到游戏服务中。(这里是为了后面测试支付等上传信息验证)

                

      c、在所以应用 ->应用内商品 -> 添加新商品,然后将添加的商品激活,此时同时得到一个商品Id(xxxxxxxxx)。如下图:

         

   6、几个重要函数,可参考sample\TrivialDrive\src\com\example\android\trivialdrivesample\MainActivity.java文件。

初始化设置Inpay_publickey可以在Google Play后台应用服务及API选项找到,调试log显示,添加消耗监听 OnConsumeFinishedListener,并进行 startSetup。只有在 startSetup成功回调后才可以进行下一步的购买。

    /// google pay// compute your public key and store it in base64EncodedPublicKeymHelper = new IabHelper(_gameActivity, Inpay_publickey); // enable debug logging (for a production application, you should set this to false).mHelper.enableDebugLogging(true);// Called when consumption is completemConsumeFinishedListener = new IabHelper.OnConsumeFinishedListener() {@Overridepublic void onConsumeFinished(Purchase purchase, IabResult result) {Log.d(TAG, "Consumption finished. Purchase: " + purchase + ", result: " + result);// if we were disposed of in the meantime, quit.if (mHelper == null) return;// We know this is the "gas" sku because it's the only one we consume,// so we don't check which sku was consumed. If you have more than one// sku, you probably should check...if (result.isSuccess()) {// successfully consumed, so we apply the effects of the item in our// game world's logic, which in our case means filling the gas tank a bitLog.d(TAG, "Consumption successful. Provisioning." + purchase);//    String[] parts = purchase.getSku().split("_");//    String part3 =parts[2];//    buyCallback(Integer.parseInt(part3));}else {Log.e(TAG,"Error while consuming: " + result);}}};mHelper.startSetup(new IabHelper.OnIabSetupFinishedListener() {public void onIabSetupFinished(IabResult result) {Log.i(TAG, "onIabSetupFinished " );if (!result.isSuccess()) {// Oh noes, there was a problem.Log.d(TAG, "Problem setting up In-app Billing: " + result);}}});

调用购买并设置回调,设置变量仅能同时存在一个购买界面否则会崩溃,因为是Manager类型一次消耗道具所以在购买成功后直接进行consumeAsync使用消耗品。

   //购买public static void buy(int idx){if(isInBuyed){return;}if (!mGoogleApiClient.isConnected()){ mGoogleApiClient.connect();return;}/* TODO: for security, generate your payload here for verification. See the comments on*        verifyDeveloperPayload() for more info. Since this is a SAMPLE, we just use*        an empty string, but on a production app you should carefully generate this. */String payload = "XXXXXXXXXXXII";//createDeveloperPayload();mHelper.launchPurchaseFlow(_gameActivity, SKU_GAS+idx, RC_REQUEST,new IabHelper.OnIabPurchaseFinishedListener() {@Overridepublic void onIabPurchaseFinished(IabResult result, Purchase purchase) {Log.d(TAG, "Purchase finished: " + result + ", purchase: " + purchase);isInBuyed =false;// if we were disposed of in the meantime, quit.if (mHelper == null) return;if (result.isFailure()) {Log.e(TAG,"Error purchasing: " + result);return;}
//                if (!verifyDeveloperPayload(purchase)) {
//                    Log.e(TAG,"Error purchasing. Authenticity verification failed.");
//                    return;
//                }Log.d(TAG, "Purchase successful." + purchase.getSku());mHelper.consumeAsync(purchase, mConsumeFinishedListener);}}, payload);isInBuyed = true;}

这里必须对购买失败进行处理,否则重新点击购买也会导致崩溃。如下:

     @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) {Log.d(TAG, "onActivityResult(" + requestCode + "," + resultCode + "," + data);if (JaveJniHelper.mHelper == null) return;// Pass on the activity result to the helper for handlingif (!JaveJniHelper.mHelper.handleActivityResult(requestCode, resultCode, data)) {// not handled, so handle it ourselves (here's where you'd// perform any handling of activity results not related to in-app// billing...super.onActivityResult(requestCode, resultCode, data);}}

至此,一个正常的购买流程已经可以正常完成了。

这里在提几个碰到问题及修复方案:

  1、点击购买出现 需要验证身份 您需要登录自己的google账户。

解决: 在Google Play商店中提交release签名一致,版本号一致的包,并进行alpha/beta发布。(发布后需要一段时间等Google Play后台显示的更新完成)

  2、提示   无法购买您要买的商品。

解决: 在beta测试中选择需要的测试方法并提交,选择后一定要点击右上角的 提交更新 按钮,稍等片刻刷新后确定可以看到已选中了。(封闭式测试帐号需要将测试测好加入测试列表)

GooglePlay - 排行榜及支付接入相关推荐

  1. android googleplay 支付接入

    今天跟大家一起看下Google的in-app Billing V3支付.   如果没有GooglePlay此处附上安装Google Play的一键安装器的链接(需要Root权限):http://www ...

  2. Android支付接入(7):Google In-app-Billing

    今天跟大家一起看下Google的in-app Billing V3支付. 如果没有GooglePlay此处附上安装Google Play的一键安装器的链接(需要Root权限):http://www.m ...

  3. Android支付接入(七):Google In-app-Billing

    转载 http://blog.csdn.net/michael_liu_89/article/details/12704461 今天跟大家一起看下Google的in-app Billing V3支付. ...

  4. Android支付接入:Google In-app-Billing

    http://blog.csdn.net/michael_liu_89/article/details/12704461 今天跟大家一起看下Google的in-app Billing V3支付.    ...

  5. coco2d-x游戏开发google play Google In-app-Billing 支付接入

    android google play接入一样的操作借用了网上别人博客的部分 改正的一些错误,主要介绍 cocos2d-x google play 接入不一样的部分 如果没有GooglePlay此处附 ...

  6. Android支付接入(七):Google In-app-Billing

     今天跟大家一起看下Google的in-app Billing V3支付.  如果没有GooglePlay此处附上安装Google Play的一键安装器的链接(需要Root权限):http://www ...

  7. Android支付接入(五):机锋网

    前边已经陆续跟大家走了一遍运营商和支付宝付费接入,今天跟大家一起看看机锋网的支付接入.事实上付费接入本身并没有太多须要注意的地方,做的多了以后你会发现套路都是大同小异的.而须要注意的地方在于怎么跟游戏 ...

  8. 支付接入开发的陷阱有多深?

    支付接入开发的陷阱有多深? 发表于2015-10-27 15:04| 3975次阅读| 来源CSDN| 10 条评论| 作者蒲婧 CTO俱乐部CTOCTO讲堂移动支付BeeCloud width=&q ...

  9. C#开发微信门户及应用(32)--微信支付接入和API封装使用

    C#开发微信门户及应用(32)--微信支付接入和API封装使用 在微信的应用上,微信支付是一个比较有用的部分,但也是比较复杂的技术要点,在微商大行其道的年代,自己的商店没有增加微信支付好像也说不过去, ...

最新文章

  1. linux oracle 关闭防火墙,Linux 下关闭防火墙设置
  2. 如何设计一门语言(八)——异步编程和CPS变换
  3. MySQL出现Waiting for table metadata lock的原因以及解决方法
  4. MySQL设置从库只读模式
  5. python分析工具有哪些_常用Python数据分析工具汇总
  6. php获取url返回的json,【求助】本地页面如何取某个URL返回的json
  7. FreeRTOS任务优先级
  8. javascript要点_JavaScript要点:为什么您应该知道引擎如何工作
  9. Python工作笔记002---PYTHON之DEF函数
  10. Python之旅Day6 模块应用
  11. subclipse同步冲突问题A conflict in the working copy obstructs the current operation
  12. 新浪微博html5模板,个人主题建站首选微博秀模板,仿新浪微博官网
  13. fontawesome-webfont.woff2 404
  14. 篇2:基于windows10专业版搭建ftp服务器
  15. Swift4.0 实现底部弹出框
  16. 简要描述CSS 中的定位机制。
  17. java必备基础知识点
  18. 【luogu P2071 座位安排】 题解
  19. 第一节:服务注册与服务发现
  20. 计算机网络基本概念汇总

热门文章

  1. 计算机网络知识汇总(十万字超详细)
  2. mysql 中字段存放表情符号,Incorrect string value: ‘\xF0\x9F\x8C\xBB‘ for column
  3. 华云数据董事长许广彬接受中央电视台采访:推动职业教育发展 打造信创人才核心竞争力
  4. Unity优化材质,清除空引用贴图
  5. 广告精准投放和大数据
  6. vue2实现海康威视根据海康插件进行监控实时预览和回放功能,全套代码,开箱即用。
  7. win10移动桌面图标字体发虚
  8. 《Adobe Fireworks CS5中文版经典教程》——第1课 了解工作区 1.1 熟悉Adobe Fireworks...
  9. office安装后无法打开 office 2016找不到VCRUNTIME 140.1
  10. 【挑战学习一百天冲刺实习面试】第二十一天:全面理解BIO、NIO、AIO