本章节主要介绍接入华为帐号的客户端开发步骤,以帮助开发者快速了解华为帐号提供的客户端接口及其调用方法。
开发者需要完成如下开发步骤:
1、初始化Agent
2、调用signIn/signOut接口登录登出帐号
HMS Agent 套件提供了对HMS SDK原生接口的二次封装,处理了部分必要的接入逻辑而不再将它们暴露给开发者,使用封装后的Agent接口接入HMS SDK,可大大提升接入效率,降低出错几率。
调用init
请务必在application的onCreate方法中初始化HMS Agent套件。如果没有自己的application类,请创建MyApplication类,并在manifest文件中配置application节点的name属性。
示例代码:
- /** *初始化HMS Agent
- */
- public class MyApplication extends Application {
- @Override
- public void onCreate() {
- super.onCreate();
- HMSAgent.init(this);
- }
- }
- <application
- android:name=".MyApplication"
- android:allowBackup="true"
- android:icon="@mipmap/ic_launcher"
- android:label="@string/app_name"
- android:roundIcon="@mipmap/ic_launcher_round"
- android:supportsRtl="true"
- android:theme="@style/AppTheme">
调用connect
请务必在应用启动后的首个activity的onCreate方法中调用connect接口,确保HMS SDK和HMS APK的连接。
示例代码:
- /**
- * SDK连接HMS
- */
- HMSAgent.connect(this, new ConnectHandler() {
- @Override
- public void onConnect(int rst) {
- showLog("HMS connect end:" + rst);
- }
- });
接口名:void HMSAgent.Hwid.signIn(forceLogin, new SignInHandler()),简称signIn接口,接口详细定义请参见signIn。
调用signIn接口及处理回调,接口详细定义请参见signIn,实现登录的示例代码如下:
- HMSAgent.Hwid.signIn(forceLogin, new SignInHandler() {
- @Override
- public void onResult(int rtnCode, SignInHuaweiId signInResult) {
- if (rtnCode == HMSAgent.AgentResultCode.HMSAGENT_SUCCESS && signInResult != null) {
- //可以获取帐号的 openid,昵称,头像 at信息
- showLog("登录成功=========");
- showLog("昵称:" + signInResult.getDisplayName());
- showLog("openid:" + signInResult.getOpenId());
- showLog("accessToken:" + signInResult.getAccessToken());
- showLog("头像url:" + signInResult.getPhotoUrl());
- } else {
- showLog("登录---error: " + rtnCode);
- }
- }
- });
接口名:void HMSAgent.Hwid.signOut(new SignOutHandler(),简称signOut接口,接口详细定义请参见signOut。
示例代码:
- HMSAgent.Hwid.signOut(new SignOutHandler() {
- @Override
- public void onResult(int rtnCode, SignOutResult signInResult) {
- if (rtnCode == HMSAgent.AgentResultCode.HMSAGENT_SUCCESS && signInResult != null) {
- showLog("退出登录成功");
- } else {
- showLog("退出登录失败:" + rtnCode);
- }
- }
- });