目录

本章节主要介绍接入华为帐号的客户端开发步骤,以帮助开发者快速了解华为帐号提供的客户端接口及其调用方法。

开发者需要完成如下开发步骤:

1、初始化Agent

2、调用signIn/signOut接口登录登出帐号

1. 初始化Agent

HMS Agent 套件提供了对HMS SDK原生接口的二次封装,处理了部分必要的接入逻辑而不再将它们暴露给开发者,使用封装后的Agent接口接入HMS SDK,可大大提升接入效率,降低出错几率。

1.1 开发步骤

调用init

请务必在application的onCreate方法中初始化HMS Agent套件。如果没有自己的application类,请创建MyApplication类,并在manifest文件中配置application节点的name属性。

示例代码:

  1. /** *初始化HMS Agent
  2. */
  3. public class MyApplication extends Application {
  4.        @Override
  5.        public void onCreate() {        
  6.                super.onCreate();
  7.                HMSAgent.init(this);
  8.        }
  9. }
  1. <application
  2.    android:name=".MyApplication"
  3.    android:allowBackup="true"
  4.    android:icon="@mipmap/ic_launcher"
  5.    android:label="@string/app_name"
  6.    android:roundIcon="@mipmap/ic_launcher_round"
  7.    android:supportsRtl="true"
  8.    android:theme="@style/AppTheme">

调用connect

请务必在应用启动后的首个activity的onCreate方法中调用connect接口,确保HMS SDK和HMS APK的连接。

示例代码:

  1. /**
  2. * SDK连接HMS
  3. */
  4. HMSAgent.connect(this, new ConnectHandler() {
  5.    @Override
  6.    public void onConnect(int rst) {
  7.        showLog("HMS connect end:" + rst);
  8.    }
  9. });

2. 调用signIn接口

2.1 接口说明

接口名:void HMSAgent.Hwid.signIn(forceLogin, new SignInHandler()),简称signIn接口,接口详细定义请参见signIn。    

2.2 开发步骤

调用signIn接口及处理回调,接口详细定义请参见signIn,实现登录的示例代码如下:

  1. HMSAgent.Hwid.signIn(forceLogin, new SignInHandler() {
  2.    @Override
  3.     public void onResult(int rtnCode, SignInHuaweiId signInResult) {
  4.         if (rtnCode == HMSAgent.AgentResultCode.HMSAGENT_SUCCESS && signInResult != null) {
  5.             //可以获取帐号的 openid,昵称,头像 at信息
  6.            showLog("登录成功=========");
  7.            showLog("昵称:" + signInResult.getDisplayName());
  8.            showLog("openid:" + signInResult.getOpenId());
  9.            showLog("accessToken:" + signInResult.getAccessToken());
  10.            showLog("头像url:" + signInResult.getPhotoUrl());
  11.        } else {
  12.            showLog("登录---error: " + rtnCode);
  13.        }
  14.   }
  15. });

3. 调用signOut接口

3.1 接口说明

接口名:void HMSAgent.Hwid.signOut(new SignOutHandler(),简称signOut接口,接口详细定义请参见signOut。    

3.2 开发步骤

示例代码:

  1. HMSAgent.Hwid.signOut(new SignOutHandler() {
  2.    @Override
  3.     public void onResult(int rtnCode, SignOutResult signInResult) {
  4.         if (rtnCode == HMSAgent.AgentResultCode.HMSAGENT_SUCCESS && signInResult != null) {
  5.             showLog("退出登录成功");
  6.        } else {
  7.            showLog("退出登录失败:" + rtnCode);
  8.        }
  9.    }
  10. });