Android AccountManager 账户同步管理简单介绍
Android AccountManager 賬戶同步管理簡(jiǎn)單介紹
文章目錄
- Android AccountManager 賬戶同步管理簡(jiǎn)單介紹
- 前言
- AccountManager 簡(jiǎn)介
- 如何讓自己的應(yīng)用顯示在可同步賬號(hào)列表中
- 1、創(chuàng)建一個(gè)自定義的AuthenticationService類(lèi)
- 2、創(chuàng)建Authentication令牌屬性的xml/xml文件
- 3、創(chuàng)建一個(gè)自定義的 Authenticator 類(lèi)
- 其他
前言
Android 界面–》設(shè)置Settings–》賬號(hào)–》添加賬號(hào),發(fā)現(xiàn)里面沒(méi)有可以同步的應(yīng)用列表,咋回事?
百度了一波,剛開(kāi)始看有點(diǎn)暈,后面多看一點(diǎn)基本理解了。
先揭曉答案,是下面這里代碼,返回的authTypes數(shù)組長(zhǎng)度為0;
AccountManager am = AccountManager.get(Context); AuthenticatorDescription[] authTypes = am.getAuthenticatorTypes();為啥返回0 ,或者如何讓他不返回0 ,后面介紹。
可以看下面兩個(gè)文章是介紹AccountManager相關(guān)知識(shí)的:
https://blog.csdn.net/dzkdxyx/article/details/78569867
https://blog.csdn.net/dzkdxyx/article/details/78632945
AccountManager 簡(jiǎn)介
AccountManager帳號(hào)管理器,集中管理apps注冊(cè)的不同類(lèi)型的帳號(hào)。
不同類(lèi)型的帳號(hào)服務(wù)會(huì)使用不同的帳號(hào)登錄和鑒權(quán)方式,所以AccountManager為不同類(lèi)型的帳號(hào)提供一個(gè)插件式authenticator模塊,
authenticators自己處理帳號(hào)登錄/認(rèn)證的具體細(xì)節(jié),也可以自己存儲(chǔ)帳號(hào)信息
Authenti…啥?
授權(quán)令牌 (Authentication Token, auth-token ) – 是由服務(wù)器提供的一個(gè)臨時(shí)的訪問(wèn)令牌。
所有需要識(shí)別用戶的請(qǐng)求,在發(fā)送到服務(wù)器時(shí)都要帶著這個(gè)令牌。
我們使用 OAuth2 ,它也是現(xiàn)在最為流行的方法。
如何讓自己的應(yīng)用顯示在可同步賬號(hào)列表中
網(wǎng)上的簡(jiǎn)單介紹:https://www.cnblogs.com/mfmdaoyou/p/6844097.html
1、創(chuàng)建一個(gè)自定義的AuthenticationService類(lèi)
public class MyAuthenticationService extends Service {MyAuthenticator mAuthenticator;@Overridepublic void onCreate() {LogUtil.debug("");mAuthenticator = new MyAuthenticator(this);}@Overridepublic IBinder onBind(Intent intent) {LogUtil.debug("getBinder()... returning the AccountAuthenticator binder for intent " + intent);LogUtil.debug("mAuthenticator = " + mAuthenticator);return mAuthenticator.getIBinder();} }一定記得要在AndroidManifest.xml注冊(cè)
<serviceandroid:name=".MyAuthenticationService"android:exported="true"><intent-filter><actionandroid:name="android.accounts.AccountAuthenticator" /></intent-filter><meta-dataandroid:name="android.accounts.AccountAuthenticator"android:resource="@xml/authenticator" /></service>2、創(chuàng)建Authentication令牌屬性的xml/xml文件
authenticator.xml
<account-authenticator xmlns:android="http://schemas.android.com/apk/res/android"android:accountType="com.example.android.samplesync"android:icon="@drawable/icon"android:smallIcon="@drawable/icon"android:label="@string/label" />上面這個(gè)信息是其他應(yīng)用可以獲取到的令牌信息。
3、創(chuàng)建一個(gè)自定義的 Authenticator 類(lèi)
MyAuthenticator 是一個(gè)繼承自AbstractAccountAuthenticator的類(lèi)。
public class MyAuthenticator extends AbstractAccountAuthenticator {public MyAuthenticator(Context context) {super(context);}@Overridepublic Bundle editProperties(AccountAuthenticatorResponse r, String s) {return null;}@Overridepublic Bundle addAccount(AccountAuthenticatorResponse r, String s, String s2, String[] strings,Bundle bundle) throws NetworkErrorException {final Bundle result = new Bundle();result.putString(AccountManager.KEY_ACCOUNT_NAME, "Test Account");result.putString(AccountManager.KEY_ACCOUNT_TYPE, s);LogUtil.debug("");return result;}... }這個(gè)是系統(tǒng)獲取對(duì)應(yīng)應(yīng)用獲取令牌信息返回的數(shù)據(jù),具體不分析了。
還有相關(guān)的Activity和相關(guān)方法就不做一一介紹了。
流程理解和回調(diào)關(guān)系可以看下這篇:
https://blog.csdn.net/wy3243996/article/details/52411139
其他
AccountManager 沒(méi)有做深入使用,這里不做詳細(xì)介紹。
Account 管理都是需要app自身登錄的前提的,系統(tǒng)點(diǎn)擊"Add Account",
選擇對(duì)應(yīng)應(yīng)用后,實(shí)際是跳轉(zhuǎn)到應(yīng)用的登錄界面。
在Android9.0發(fā)現(xiàn)火狐firefox應(yīng)用是在"Add Account"列表中的,
但是Android11.0 的"Add Account"列表中沒(méi)看到火狐應(yīng)用,咋回事?
研究發(fā)現(xiàn)是Android11版本 的新firefox沒(méi)有做賬戶管理。
那么如何查看apk中做了賬戶管理:
方式(1)使用工具解壓apk,查看AndroidManifest.xml文件
方式(2)把a(bǔ)pk拖拽到Android Studio中查看AndroidManifest.xml文件
搜索:“Authentica”,
查看是否存在如下Servie
<serviceandroid:name=".AuthenticationService"android:exported="true"><intent-filter><actionandroid:name="android.accounts.AccountAuthenticator" /></intent-filter><meta-dataandroid:name="android.accounts.AccountAuthenticator"android:resource="@xml/authenticator" /></service>如果看到了就說(shuō)明,該應(yīng)用時(shí)支持賬號(hào)同步功能的,如果沒(méi)有就是未做適配支持。
總結(jié)
以上是生活随笔為你收集整理的Android AccountManager 账户同步管理简单介绍的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: SDUTOJ 3034 ——炸学校
- 下一篇: Windows 组策略修改 之 初始化文