<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical">
<!--ToDo 添加华为登录按钮,id为HuaweiIdAuthLoginButton -->
<com.huawei.hms.support.hwid.ui.HuaweiIdAuthButton
android:layout_gravity="center_horizontal"
android:id="@+id/HuaweiIdAuthLoginButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<Button
android:id="@+id/HuaweiIdSignOutButton"
android:layout_width="200dp"
android:background="@color/huawei_red"
android:layout_height="40dp"
android:textColor="@color/white"
android:layout_marginTop="15dp"
android:layout_gravity="center_horizontal"
android:text="@string/sign_out"/>
<Button
android:id="@+id/HuaweiIdCancelAuthButton"
android:layout_width="200dp"
android:background="@color/huawei_red"
android:layout_height="40dp"
android:textColor="@color/white"
android:layout_marginTop="15dp"
android:layout_gravity="center_horizontal"
android:text="@string/cancel_authorization"/>
<EditText
android:id="@+id/LogText"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
main
package com.huawei.quickstartdemo;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import com.huawei.hmf.tasks.OnFailureListener;
import com.huawei.hmf.tasks.OnSuccessListener;
import com.huawei.hmf.tasks.Task;
import com.huawei.hms.common.ApiException;
import com.huawei.hms.support.account.AccountAuthManager;
import com.huawei.hms.support.account.request.AccountAuthParams;
import com.huawei.hms.support.account.request.AccountAuthParamsHelper;
import com.huawei.hms.support.account.result.AuthAccount;
import com.huawei.hms.support.account.service.AccountAuthService;
import com.huawei.hms.support.api.entity.common.CommonConstant;
import com.huawei.hms.support.hwid.ui.HuaweiIdAuthButton;
public class MainActivity extends AppCompatActivity {
// 华为帐号登录授权服务,提供静默登录接口silentSignIn,获取前台登录视图getSignInIntent,登出signOut等接口
private AccountAuthService mAuthService;
// 华为帐号登录授权参数
private AccountAuthParams mAuthParam;
// 用户自定义signInIntent请求码
private static final int REQUEST_CODE_SIGN_IN = 1000;
// 用户自定义日志标记
private static final String TAG = "Account";
private TextView logTextView;
private Button mHuaweiIdSignOutButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// activity_main为自定义布局文件名称
setContentView(R.layout.activity_main);
//ToDo 找到id为HuaweiIdAuthLoginButton的华为登录按钮HuaweiIdAuthButton,并给其添加点击事件, 点击事件内调用静默登录方法:silentSignInByHwId
findViewById(R.id.HuaweiIdAuthLoginButton).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
silentSignInByHwId();
}
});
findViewById(R.id.HuaweiIdSignOutButton).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
signOut();
}
});
findViewById(R.id.HuaweiIdCancelAuthButton).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
cancelAuthorization();
}
});
logTextView = (TextView) findViewById(R.id.LogText);
}
/**
* 静默登录,如果设备上的华为帐号系统帐号已经登录,并且用户已经授权过,无需再拉起登录页面和授权页面,
* 将直接静默登录成功,在成功监听器中,返回帐号信息;
* 如果华为帐号系统帐号未登录或者用户没有授权,静默登录会失败,需要显示拉起前台登录授权视图。
*/
private void silentSignInByHwId() {
// 1、配置登录请求参数AccountAuthParams,包括请求用户id(openid、unionid)、email、profile(昵称、头像)等。
// 2、DEFAULT_AUTH_REQUEST_PARAM默认包含了id和profile(昵称、头像)的请求。
// ToDo 使用请求参数构造华为帐号登录授权服务AccountAuthService,请求参数中不需要获取用户的邮箱
mAuthParam = new AccountAuthParamsHelper(AccountAuthParams.DEFAULT_AUTH_REQUEST_PARAM)
.createParams();
// ToDo 使用静默登录进行华为帐号登录
Task<AuthAccount> task = mAuthService.silentSignIn();
// ToDo 添加成功登录的监听,并且调用dealWithResultOfSignIn处理返回的帐号对象AuthAccount,获取帐号信息
task.addOnSuccessListener(new OnSuccessListener<AuthAccount>() {
@Override
public void onSuccess(AuthAccount authAccount) {
// 静默登录成功,处理返回的帐号对象AuthAccount,获取帐号信息
dealWithResultOfSignIn(authAccount);
}
});
// ToDo 添加登录失败的监听,如果失败则打印失败信息,且使用getSignInIntent()获取Intent后进行前台显式登录
task.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(Exception e) {
// 静默登录失败,使用getSignInIntent()方法进行前台显式登录
if (e instanceof ApiException) {
ApiException apiException = (ApiException) e;
Intent signInIntent = mAuthService.getSignInIntent();
// 如果应用是全屏显示,即顶部无状态栏的应用,需要在Intent中添加如下参数:
// intent.putExtra(CommonConstant.RequestParams.IS_FULL_SCREEN, true);
// 具体详情可以参见应用调用登录接口的时候是全屏页面,为什么在拉起登录页面的过程中顶部的状态栏会闪一下?应该如何解决?
signInIntent.putExtra(CommonConstant.RequestParams.IS_FULL_SCREEN, true);
startActivityForResult(signInIntent, REQUEST_CODE_SIGN_IN);
}else{
showLog(e.getMessage());
}
}
});
}
/**
* 处理返回的AuthAccount,获取帐号信息
*
* @param authAccount AccountAccount对象,用于记录帐号信息
*/
private void dealWithResultOfSignIn(AuthAccount authAccount) {
//获取帐号信息
Log.i(TAG, "display name:" + authAccount.getDisplayName());
Log.i(TAG, "photo uri string:" + authAccount.getAvatarUriString());
Log.i(TAG, "photo uri:" + authAccount.getAvatarUri());
Log.i(TAG, "email:" + authAccount.getEmail());
Log.i(TAG, "openid:" + authAccount.getOpenId());
Log.i(TAG, "unionid:" + authAccount.getUnionId());
showLog("display name:" + authAccount.getDisplayName() + "photo uri string:" + authAccount.getAvatarUriString() +
"email:" + authAccount.getEmail() + "openid:" + authAccount.getOpenId() + "unionid:" + authAccount.getUnionId());
// ToDo 获取用户信息后业务逻辑,将帐号信息传递给业务逻辑方法doBussinessLogic
doBussinessLogic(authAccount);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_CODE_SIGN_IN) {
Log.i(TAG, "onActivitResult of sigInInIntent, request code: " + REQUEST_CODE_SIGN_IN);
Task<AuthAccount> authAccountTask = AccountAuthManager.parseAuthResultFromIntent(data);
//ToDo 判断是否登录成功, 如果登录成功,获取到登录帐号信息对象authAccount后调用用dealWithResultOfSignIn处理业务逻辑
if (authAccountTask.isSuccessful()) {
// 登录成功,获取到登录帐号信息对象authAccount
AuthAccount authAccount = authAccountTask.getResult();
dealWithResultOfSignIn(authAccount);
Log.i(TAG, "onActivitResult of sigInInIntent, request code: " + REQUEST_CODE_SIGN_IN);
} else {
// 登录失败,status code标识了失败的原因,请参见API参考中的错误码了解详细错误原因
Log.e(TAG, "sign in failed : " +((ApiException)authAccountTask.getException()).getStatusCode());
}
}
}
private void signOut() {
if (mAuthService == null) {
return;
}
//ToDo 使用AuthService进行登出操作, 并且调用showLog方法打印响应的成功或失败的日志。
Task<Void> signOutTask = mAuthService.signOut();
signOutTask.addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void aVoid) {
Log.i(TAG, "signOut Success");
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(Exception e) {
Log.i(TAG, "signOut fail");
}
});
}
private void cancelAuthorization() {
if (mAuthService == null) {
return;
}
Task<Void> task = mAuthService.cancelAuthorization();
task.addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void aVoid) {
Log.i(TAG, "cancelAuthorization success");
showLog("cancelAuthorization success");
}
});
task.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(Exception e) {
Log.i(TAG, "cancelAuthorization failure:" + e.getClass().getSimpleName());
showLog("cancelAuthorization failure:" + e.getClass().getSimpleName());
}
});
}
private void showLog(String log) {
logTextView.setText("log:" + "\n" + log);
}
private void doBussinessLogic(AuthAccount authAccount){
//具体的业务逻辑代码,此处省略。
}
}