简介
在本文中,我们可以学习如何将华为帐号套件集成到患者追踪应用中。因此,我将提供有关此患者跟踪应用程序的系列文章,在接下来的文章中,我将集成其他华为套件。
账号包
华为账号包为开发者提供了简单、安全、快捷的登录和授权功能。用户无需输入帐号、密码和等待授权。用户可以点击使用华为帐号登录按钮,快速安全地登录应用。
要求
- 任何操作系统(MacOS、Linux 和 Windows)。
- 必须有 HMS 4.0.0.300 或更高版本的华为手机。
- 必须有安装了Android Studio、JDK 1.8、SDK 平台 26 和 Gradle 4.6 及更高版本的笔记本电脑或台式机。
- 最低API 级别 24 是必需的。
- 需要 EMUI 9.0.0 及更高版本的设备。
如何集成 HMS 依赖
首先在华为开发者网站注册成为华为开发者并完成身份验证,参考注册华为ID。
在 android studio 中创建项目,请参阅创建 Android Studio 项目。
生成SHA-256 证书指纹。
生成SHA-256 证书指纹。在android项目右上角点击Gradle,选择Project Name > Tasks > android,然后点击signingReport,如下。

注意:项目名称取决于用户创建的名称。
- 在 AppGallery Connect 中创建一个应用程序。
- 从App信息中下载agconnect-services.json文件,复制粘贴到app目录下的android Project中,如下。

- 输入SHA-256证书指纹,点击保存按钮,如下。

- 单击Manage APIs 选项卡并启用Account Kit。

- 在buildscript、dependencies和allprojects的存储库下的build.gradle(Project)文件中添加以下 maven URL ,请参阅添加配置。
maven { url 'http://developer.huawei.com/repo/' }
classpath 'com.huawei.agconnect:agcp:1.6.0.300'
- 在build.gradle(Module)文件 中添加以下插件和依赖项。
apply plugin: id 'com.huawei.agconnect'
// Huawei AGC
implementation 'com.huawei.agconnect:agconnect-core:1.6.0.300'
// Huawei Account Kit
implementation 'com.huawei.hms:hwid:6.3.0.301'
- 现在同步 gradle。
- 将所需的权限添加到AndroidManifest.xml文件。
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
让我们开始开发
我已经在 Android Studio 上创建了一个空活动项目,让我们开始编码。
在MainActivity.kt我们可以找到业务逻辑。
class MainActivity : AppCompatActivity() {
// Account Kit variables
private var mAuthManager: AccountAuthService? = null
private var mAuthParam: AccountAuthParams? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// Account kit button click Listener
btn_login.setOnClickListener(mOnClickListener)
}
// Account kit, method to send an authorization request.
private fun signIn() {
mAuthParam = AccountAuthParamsHelper(AccountAuthParams.DEFAULT_AUTH_REQUEST_PARAM)
.setIdToken()
.setAccessToken()
.setProfile()
.createParams()
mAuthManager = AccountAuthManager.getService(this@MainActivity, mAuthParam)
startActivityForResult(mAuthManager?.signInIntent, 1002)
}
private val mOnClickListener: View.OnClickListener = object : View.OnClickListener {
override fun onClick(v: View?) {
when (v?.id) {
R.id.btn_login -> signIn()
}
}
}
// Process the authorization result
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (requestCode == 1002 ) {
val authAccountTask = AccountAuthManager.parseAuthResultFromIntent(data)
if (authAccountTask.isSuccessful) {
Toast.makeText(this, "SigIn success", Toast.LENGTH_LONG).show()
val intent = Intent(this@MainActivity, Home::class.java)
startActivity(intent)
} else {
Toast.makeText(this, "SignIn failed: " + (authAccountTask.exception as ApiException).statusCode, Toast.LENGTH_LONG).show()
}
}
}
}
在activity_main.xml我们可以创建 UI 屏幕。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
tools:ignore="ExtraText,MissingConstraints">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="62dp"
android:layout_marginTop="35dp"
android:layout_marginRight="62dp"
android:gravity="center"
android:paddingTop="14dp"
android:paddingBottom="14dp"
android:text="Login"
android:textColor="@color/black"
android:textSize="28sp"
android:textStyle="bold">
</TextView>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="62dp"
android:layout_marginTop="32dp"
android:layout_marginRight="62dp"
android:background="@drawable/blue_border_rounded_cornwe">
<EditText
android:id="@+id/edt_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/username_icon"
android:background="@android:color/transparent"
android:hint="Enter Username "
android:inputType="textEmailAddress"
android:maxLines="1"
android:paddingLeft="17dp"
android:paddingTop="15dp"
android:paddingBottom="15dp"
android:textSize="13sp">
</EditText>
<ImageView
android:id="@+id/username_icon"
android:layout_width="20dp"
android:layout_height="17dp"
android:layout_centerVertical="true"
android:layout_marginLeft="17dp"
android:src="@drawable/username" />
</RelativeLayout>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="62dp"
android:layout_marginTop="13dp"
android:layout_marginRight="62dp"
android:background="@drawable/blue_border_rounded_cornwe">
<EditText
android:id="@+id/edt_pass"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/pass_icon"
android:background="@android:color/transparent"
android:hint="Password"
android:inputType="textPassword"
android:maxLength="10"
android:maxLines="1"
android:paddingLeft="17dp"
android:paddingTop="15dp"
android:paddingBottom="15dp"
android:textSize="13sp">
</EditText>
<ImageView
android:id="@+id/pass_icon"
android:layout_width="20dp"
android:layout_height="17dp"
android:layout_centerVertical="true"
android:layout_marginLeft="17dp"
android:src="@drawable/password" />
</RelativeLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_marginTop="12dp"
android:layout_marginRight="63dp"
android:paddingTop="14dp"
android:paddingBottom="14dp"
android:text="Forgot Password?"
android:textAllCaps="false"
android:textColor="#0A0A0B"
android:textSize="14sp">
</TextView>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="62dp"
android:layout_marginTop="19dp"
android:layout_marginRight="62dp"
android:background="@drawable/blue_fill__rounded_color"
android:gravity="center"
android:paddingTop="14dp"
android:paddingBottom="14dp"
android:text="Login"
android:textColor="@color/white"
android:textSize="15sp">
</TextView>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginBottom="45dp"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="16dp"
android:gravity="center"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="Sign in with Social Networks"
android:textColor="#0E0E0E"
android:textSize="15sp">
</TextView>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:layout_marginBottom="55dp"
android:gravity="center"
android:orientation="horizontal">
<ImageView
android:id="@+id/btn_login"
android:layout_width="40dp"
android:layout_height="40dp"
android:src="@drawable/huawei_icon" />
<ImageView
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:src="@drawable/google" />
<ImageView
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_marginRight="20dp"
android:src="@drawable/instagram_icon" />
</LinearLayout>
</LinearLayout>
</RelativeLayout>
演示

技巧和窍门
- 确保您已经注册为华为开发者。
- 将 minSDK 版本设置为 24 或更高版本,否则会出现AndriodManifest合并问题。
- 确保您已将agconnect-services.json文件添加到 app 文件夹。
- 确保您已添加SHA-256 指纹,但不会失败。
- 确保正确添加了所有依赖项。
结论
在本文中,我们学习了如何将华为帐号工具包集成到患者追踪应用程序中。因此,我将提供有关此患者跟踪应用程序的系列文章,在接下来的文章中,我将集成其他华为套件。
如果觉得有帮助,请点赞和评论,有需要Android的资料的可以扫描下面微信二维码即可免费领取。

本文详细介绍了如何将华为帐号套件集成到Android患者追踪应用中,包括所需条件、步骤和代码实现。通过华为帐号包,开发者可以实现简单、安全的登录功能。文章还涵盖了创建SHA-256证书指纹、在AppGalleryConnect中注册应用、添加依赖以及处理授权结果的代码示例。
3163

被折叠的 条评论
为什么被折叠?



