简介

游戏浮标是指游戏界面中以悬浮窗的方式展示华为游戏中心的入口,用户点击游戏浮标后可以使用用户在华为游戏中心的游戏增值功能,如浏览论坛、领取礼包、查看攻略、游戏录屏等。

您将建立什么

在本次codelab中,您将建立一个具备简单游戏交互功能的Android应用程序,您的应用程序将包含:

您将会学到什么

通过这个Codelab,您将学到:

硬件要求

软件要求

需要的知识点

集成HUAWEI HMS Core能力,需要完成以下准备工作:

  1. AppGallery Connect首页,点击"我的项目"->选择您创建的项目->"项目设置"->"API 管理",进入服务管理菜单。
  2. 点击"游戏服务"后面的开关,开启游戏服务。开关打开后,建议等15分钟以后再测试游戏服务相关功能。

添加当前应用的AppGallery Connect配置文件

  1. AppGallery Connect首页,点击"我的项目"->选择您项目下的应用->"项目设置"->"常规"->"应用"下的"agconnect-services.json"按钮,下载配置文件。
  2. 将"agconnect-services.json"文件拷贝到应用级根目录下。

配置HMS Core SDK的Maven仓地址

Android Studio的代码库配置在Gradle插件7.0以下版本、7.0版本和7.1及以上版本有所不同。请根据您当前的Gradle插件版本,选择对应的配置过程。

7.0以下版本

7.0版本

7.1及以上版本

7.0以下版本

7.0版本

7.1及以上版本

添加编译依赖

  1. 打开Android Studio应用级build.gradle文件。
  2. dependencies中添加如下编译依赖。其中:
    • hwid的{version}替换为最新帐号服务版本号,参见版本更新说明。
    • game的{version}替换为最新游戏服务版本号,参见版本更新说明
      dependencies { implementation 'com.huawei.hms:hwid:{version}' implementation 'com.huawei.hms:game:{version}' }
  3. 添加agcp插件配置。请根据实际情况选择:
    • 方式一:在文件头部声明的下一行添加如下配置。
      apply plugin: 'com.huawei.agconnect'
    • 方式二:在plugins中添加如下配置。
      plugins { id 'com.android.application' // 添加如下配置 id 'com.huawei.agconnect' }
  4. 点击界面上的"Sync Now"链接下载编译已完成的配置。

配置混淆脚本

编译APK前需要添加混淆配置,避免功能异常。配置步骤如下:

  1. 打开Android Studio工程的混淆配置文件proguard-rules.pro。
  2. 加入混淆配置。
    -ignorewarnings -keepattributes *Annotation* -keepattributes Exceptions -keepattributes InnerClasses -keepattributes Signature -keepattributes SourceFile,LineNumberTable -keep class com.hianalytics.android.**{*;} -keep class com.huawei.updatesdk.**{*;} -keep class com.huawei.hms.**{*;} -keep class com.huawei.gamebox.plugin.gameservice.**{*;} -keep interface com.huawei.hms.analytics.type.HAEventType{*;} -keep interface com.huawei.hms.analytics.type.HAParamType{*;} -keep class com.huawei.hms.analytics.HiAnalyticsInstance{*;} -keep class com.huawei.hms.analytics.HiAnalyticsInstance{*;} -keep class com.huawei.hms.analytics.HiAnalytics{*;}
  3. 当您启用R8资源缩减(项目级"build.gradle"文件中"shrinkResources"属性为"true")和严格引用检查("res/raw/keep.xml"文件中的"shrinkMode"为"strict")时,请您配置"keep.xml"文件手动保留layout资源,确保应用正常通过华为应用市场上架审核。
    <?xml version="1.0" encoding="utf-8"?> <resources xmlns:tools="http://schemas.android.com/tools" tools:keep="@layout/hms_download_progress,@drawable/screen_off,@layout/upsdk*,@drawable/c_buoycircle*,@drawable/hms_game*,@layout/c_buoycircle*,@layout/hms_game*,@strings/hms_game*,@strings/c_buoycircle*" tools:shrinkMode="strict" />

本次Codelab中您可以在Android Studio工程中创建一个布局页面,参照下图进行UI设计,新增三个Button。"init"点击后调用初始化接口,"showFloatWindow"点击后调用显示浮标接口,"hideFloatWindow"点击后调用隐藏浮标接口。

// 布局代码 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <Button android:id="@+id/btn_init" android:layout_width="match_parent" android:layout_height="wrap_content" android:textAllCaps="false" android:text="init"/> <Button android:id="@+id/btn_showWindow" android:layout_width="match_parent" android:layout_height="wrap_content" android:textAllCaps="false" android:text="showFloatWindow"/> <Button android:id="@+id/btn_hideWindow" android:layout_width="match_parent" android:layout_height="wrap_content" android:textAllCaps="false" android:text="hideFloatWindow"/> </LinearLayout>
  1. 在Application的onCreate方法中添加注册Activity的回调监听。
    public class MyApplication extends Application { @Override public void onCreate(){ super.onCreate(); HuaweiMobileServicesUtil.setApplication(this); } }
  2. 在启动游戏的第一个Activity中,调用JosAppsClient的init方法初始化游戏SDK。以发布中国大陆,含游戏防沉迷回调的代码为例。
  3. private void init(){ AccountAuthParams params = AccountAuthParams.DEFAULT_AUTH_REQUEST_PARAM_GAME; JosAppsClient appsClient = JosApps.getJosAppsClient(this); // 设置防沉迷提示语的context,此行必须添加 ResourceLoaderUtil.setmContext(this); Task<Void> initTask = appsClient.init(new AppParams(params, new AntiAddictionCallback() { @Override public void onExit() { // System.exit(0); // The callback will return in two situations: // 1. When a no-adult, real name user logs in to the game during the day, Huawei will pop up a box to remind the player that the game is not // allowed. The player clicks "OK" and Huawei will return to the callback // 2. The no-adult, real name user logs in the game at the time allowed by the state. At 9 p.m., Huawei will pop up a box to remind the // player that it is time. The player clicks "I know" and Huawei will return to the callback // You can realize the anti addiction function of the game here, such as saving the game, calling the account to exit the interface or // directly the game process // 该回调会在如下两种情况下返回: // 1.未成年人实名帐号在白天登录游戏,华为会弹框提示玩家不允许游戏,玩家点击"确定",华为返回回调 // 2.未成年实名帐号在国家允许的时间登录游戏,到晚上9点,华为会弹框提示玩家已到时间,玩家点击"知道了",华为返回回调 // 您可在此处实现游戏防沉迷功能,如保存游戏、调用帐号退出接口或直接游戏进程退出(如System.exit(0)) }})); initTask.addOnSuccessListener(new OnSuccessListener<Void>() { @Override public void onSuccess(Void aVoid) { showLog("init success"); hasInit = true; // 游戏初始化成功后务必成功调用过一次浮标显示接口 showFloatWindowNewWay(); } }).addOnFailureListener(new OnFailureListener() { @Override public void onFailure(Exception e) { if (e instanceof ApiException) { ApiException apiException = (ApiException) e; int statusCode = apiException.getStatusCode(); if (statusCode == JosStatusCodes.JOS_PRIVACY_PROTOCOL_REJECTED) { // Error code 7401 indicates that the user did not agree to Huawei joint operations privacy agreement // 错误码为7401时表示用户未同意华为联运隐私协议 showLog("has reject the protocol"); // You need to prohibit players from entering the game here. // 此处您需禁止玩家进入游戏 } else if (statusCode == GamesStatusCodes.GAME_STATE_NETWORK_ERROR) { // Error code 7002 indicates network error // 错误码7002表示网络异常 showLog("network error"); // 此处您可提示玩家检查网络,请不要重复调用init接口,否则断网情况下可能会造成手机高耗电。 // You can ask the player to check the network. Do not invoke the init interface repeatedly. Otherwise, the phone may consume a lot // of power if the network is disconnected. } else if (statusCode == 907135003) { // 907135003表示玩家取消HMS Core升级或组件升级 // 907135003 indicates that user rejected the installation or upgrade of HMS Core. showLog("init statusCode=" + statusCode); init(); } else { // Handle other error codes // 在此处实现其他错误码的处理 } } } }); }

  1. 初始化接口调用成功之后,需要调用一次浮标接口
  2. 在所有游戏界面Activity的onResume()方法中调用显示浮标接口,在所有游戏界面Activity的onPause()方法中调用隐藏浮标接口。
    @Override protected void onResume() { super.onResume(); showFloatWindowNewWay(); } @Override protected void onPause() { super.onPause(); hideFloatWindowNewWay(); } /** * 显示游戏浮标。 */ private void showFloatWindowNewWay() { if (hasInit) { // 请务必在init成功后,调用浮标接口 Games.getBuoyClient(this).showFloatWindow(); showLog("show floatWindow"); } } /** * 隐藏已经显示的游戏浮标。 */ private void hideFloatWindowNewWay() { Games.getBuoyClient(this).hideFloatWindow(); showLog("show hideWindow"); } public void showLog(String logLine) { Log.e(TAG,logLine); }

以下场景测试请使用EMUI9.1以下的华为设备或者三方设备,并且设备必须安装9.0以上版本华为应用市场客户端

  1. 测试游戏初始化功能:
    点击init按钮,控制台查看打印日志,在Verbose日志中搜索关键字"MainActivity",打印"init success"即初始化成功。
  2. 测试游戏浮标功能:
    初始化成功之后,会调用一次显示浮标接口,在Verbose日志中搜索关键字"MainActivity",打印"show floatWindow"即调用了显示浮标接口,在界面中显示如下图所示的图标就是游戏浮标,即显示浮标接口调用成功。
  3. 点击"hideFloatWindow"按钮,控制台查看打印日志,在Verbose日志中搜索关键字"MainActivity",打印"hide floatWindow",并且页面中游戏浮标消失,即隐藏浮标接口调用成功。
  4. 拖动游戏浮标在页面顶部显示"拖到此处隐藏",把游戏浮标拖到顶部松开,在页面中会弹出弹框提示是否隐藏图标,点击"取消"则页面中会继续显示游戏浮标;点击"隐藏",则游戏浮标隐藏,页面中不会看到游戏浮标;勾选弹框中的"不再提示",下次隐藏浮标时不会再弹出"隐藏浮标"弹窗。
  5. 游戏浮标隐藏后,快速翻转手机,游戏浮标可再次显示

恭喜您,您已经成功完成了Codelab并学到了:

您可以阅读下面链接,了解更多相关的信息。

本Codelab中所用API请参考游戏服务的相关API介绍

游戏浮标官方文档介绍,请参考浮标

相关错误码说明,请参考错误码

源码下载地址:源码下载

Code copied