做网站的目标是什么,产品宣传片制作公司,网站设计的国际专业流程,简洁汽车配件网站模板目录
✅ 0. 需求
#x1f4c2; 1. 前言
#x1f531; 2. 使用
2.1 账户体系前提
2.2 创建账户服务
2.3 操作账户-增删改查
#x1f4a0; 3. 源码流程 ✅ 0. 需求 试想#xff0c;自己去实现一个账号管理体系#xff0c;该如何做呢#xff1f; ——————————…目录
✅ 0. 需求 1. 前言 2. 使用
2.1 账户体系前提
2.2 创建账户服务
2.3 操作账户-增删改查 3. 源码流程 ✅ 0. 需求 试想自己去实现一个账号管理体系该如何做呢 —————————— 最近有遇到这样一个需求AR眼镜只支持同时与一个手机绑定即AR眼镜已与手机绑定过的话不再支持与其他手机绑定如要绑定其他手机需要先解绑当前手机才能重新绑定。 理解一下需求即是眼镜端需要账号管理体系主要用来存储已绑定过手机的token。 AR眼镜绑定手机时序图如下所示 1. 前言 我们知道Android的账号管理体系是用来管理用户在Android设备上的身份验证和授权的系统包括了对账号的创建、授权、修改和删除等操作的管理。 那么我们为什么要使用Android的账号管理体系 —————————— 尽管我们可以自己使用SP、MMKV、文件或数据库等方式来存储、更新、删除账户、密码或AuthToken但其实涉及到跨进程通信实现起来其实是稍显麻烦的并且对于数据安全信息加密这块的可靠性也有待商榷。 另外从本文第一张图可见我们在架构设计中规划的账号体系服务是寄生于Android启动时system_server开启的服务然后通过binder方式提供给其他进程使用。 然而在技术预研时发现强大的Android早已想到了这点在Android 2.0开始就加入了新包android.accounts为我们准备好了这样一个服务ACCOUNT_SERVICE。 而且该包功能已十分强大我们可以直接拿来使用功能主要包括
集中式的账户管理API可以安全地存储和访问认证的令牌和密码可以在同一个设备中管理同一应用的多个不同账号能够自动批量的同步服务器更新账户甚至可以和不同服务器进行数据同步和安全认证把账户的验证过程、AuthToken的获取过程分离出来降低程序的耦合性并且会在”设置”应用中添加一个账户入口方便应用间账号共享。 2. 使用
2.1 账户体系前提 1账号体系共享前提使用相同的签名。 2权限申明
uses-permission android:nameandroid.permission.GET_ACCOUNTS /
2.2 创建账户服务 步骤一定义一个action为android.accounts.AccountAuthenticator的Intent的Service并在meta-data的resource属性指定该Account基本显示信息的xml文件authenticator模版代码如下
serviceandroid:name.portal.feature.account.service.AccountServiceandroid:enabledtrueandroid:exportedtrueintent-filteraction android:nameandroid.accounts.AccountAuthenticator //intent-filtermeta-dataandroid:nameandroid.accounts.AccountAuthenticatorandroid:resourcexml/authenticator /
/service 步骤二在res下的xml文件夹中新建authenticator.xml文件
?xml version1.0 encodingutf-8?
account-authenticator xmlns:androidhttp://schemas.android.com/apk/res/androidandroid:accountTypecom.agg.accountandroid:icondrawable/ic_testandroid:labelAggAccountandroid:smallIcondrawable/ic_test / 注android:accountType表示的是Account类型它必须是唯一的一般是包名。 步骤三创建继承自AbstractAccountAuthenticator的Authenticator文件如下
class Authenticator(context: Context) : AbstractAccountAuthenticator(context) {override fun editProperties(response: AccountAuthenticatorResponse?, accountType: String?): Bundle? {return null}override fun addAccount(response: AccountAuthenticatorResponse?,accountType: String?,authTokenType: String?,requiredFeatures: Arrayout String?,options: Bundle?): Bundle? {return null}override fun getAuthToken(response: AccountAuthenticatorResponse?,account: Account,authTokenType: String?,options: Bundle?): Bundle? {return null}override fun confirmCredentials(response: AccountAuthenticatorResponse?, account: Account?, options: Bundle?): Bundle? {return null}override fun getAuthTokenLabel(authTokenType: String?): String {return }override fun updateCredentials(response: AccountAuthenticatorResponse?,account: Account?,authTokenType: String?,options: Bundle?): Bundle? {return null}override fun hasFeatures(response: AccountAuthenticatorResponse?, account: Account?, features: Arrayout String?): Bundle? {return null}} 步骤四创建帐户Service并在Service的onBind中调AbstractAccountAuthenticator的getIBinder()返回其用于远程调用的IBinder如下所示
class AccountService : Service() {private var authenticator: Authenticator? nulloverride fun onCreate() {super.onCreate()authenticator Authenticator(this)}override fun onBind(intent: Intent?): IBinder? {return authenticator?.iBinder}} 注运行起来程序后在“设置”应用-“帐户”-“添加帐户”列表中就已可以发现自己的app了。
2.3 操作账户-增删改查 1获取所有账号 /*** 获取所有账号*/fun getAllAccount(context: Context) {val accountManager context.getSystemService(Context.ACCOUNT_SERVICE) as AccountManagerLog.e(TAG, getAllAccount: size ${accountManager.accounts.size})for (account in accountManager.accounts) {Log.e(TAG, getAllAccount: $account)}} 2添加账号 /*** 添加账号*/fun addAccount(context: Context, account: Account, password: String, authToken: String) {val bundle Bundle()bundle.putString(AccountManager.KEY_ACCOUNT_NAME, account.name)bundle.putString(AccountManager.KEY_ACCOUNT_TYPE, account.type)bundle.putString(AccountManager.KEY_AUTHTOKEN, authToken)bundle.putString(AccountManager.KEY_PASSWORD, password)AccountManager.get(context).addAccountExplicitly(account, password, bundle)} 3更新某个账号 /*** 更新某个账号*/fun updateAccount(context: Context, account: Account, password: String , authToken: String ) {var updatePassword passwordvar updateAuthToken authTokenAccountManager.get(context).apply {if (updatePassword.isEmpty()) {updatePassword getUserData(account, AccountManager.KEY_PASSWORD)}if (updateAuthToken.isEmpty()) {updateAuthToken getUserData(account, AccountManager.KEY_AUTHTOKEN)}removeAccountExplicitly(account)}addAccount(context, account, updatePassword, updateAuthToken)} 4删除某个账号 /*** 删除某个账号*/fun delAccount(context: Context, account: Account) {val isRemoveSuccess AccountManager.get(context).removeAccountExplicitly(account)Log.e(TAG, delAllAccount: isRemoveSuccess $isRemoveSuccess, $account)} 5删除所有账号 /*** 删除所有账号*/fun delAllAccount(context: Context) {val accountManager AccountManager.get(context)for (account in accountManager.accounts) {val isRemoveSuccess accountManager.removeAccountExplicitly(account)Log.e(TAG, delAllAccount: isRemoveSuccess $isRemoveSuccess, $account)}} 3. 源码流程
从系统启动system_server进程进而启动ACCOUNT_SERVICE服务开始AccountManager是一个面向应用程序开发的组件它提供了一套对应于IAccountManager协议的应用程序接口这组接口通过Binder机制与系统服务AccountManagerService进行通信协作完成帐号相关的操作同时AccountManager接收authenticators提供的回调以便在帐号操作完成之后向调用此帐号服务的业务返回对应的接口同时触发这个业务对结果的处理。