Android AccountManager 账户同步管理简单介绍
Android AccountManager 賬戶同步管理簡單介紹
文章目錄
- Android AccountManager 賬戶同步管理簡單介紹
- 前言
- AccountManager 簡介
- 如何讓自己的應用顯示在可同步賬號列表中
- 1、創建一個自定義的AuthenticationService類
- 2、創建Authentication令牌屬性的xml/xml文件
- 3、創建一個自定義的 Authenticator 類
- 其他
前言
Android 界面–》設置Settings–》賬號–》添加賬號,發現里面沒有可以同步的應用列表,咋回事?
百度了一波,剛開始看有點暈,后面多看一點基本理解了。
先揭曉答案,是下面這里代碼,返回的authTypes數組長度為0;
AccountManager am = AccountManager.get(Context); AuthenticatorDescription[] authTypes = am.getAuthenticatorTypes();為啥返回0 ,或者如何讓他不返回0 ,后面介紹。
可以看下面兩個文章是介紹AccountManager相關知識的:
https://blog.csdn.net/dzkdxyx/article/details/78569867
https://blog.csdn.net/dzkdxyx/article/details/78632945
AccountManager 簡介
AccountManager帳號管理器,集中管理apps注冊的不同類型的帳號。
不同類型的帳號服務會使用不同的帳號登錄和鑒權方式,所以AccountManager為不同類型的帳號提供一個插件式authenticator模塊,
authenticators自己處理帳號登錄/認證的具體細節,也可以自己存儲帳號信息
Authenti…啥?
授權令牌 (Authentication Token, auth-token ) – 是由服務器提供的一個臨時的訪問令牌。
所有需要識別用戶的請求,在發送到服務器時都要帶著這個令牌。
我們使用 OAuth2 ,它也是現在最為流行的方法。
如何讓自己的應用顯示在可同步賬號列表中
網上的簡單介紹:https://www.cnblogs.com/mfmdaoyou/p/6844097.html
1、創建一個自定義的AuthenticationService類
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注冊
<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、創建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" />上面這個信息是其他應用可以獲取到的令牌信息。
3、創建一個自定義的 Authenticator 類
MyAuthenticator 是一個繼承自AbstractAccountAuthenticator的類。
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;}... }這個是系統獲取對應應用獲取令牌信息返回的數據,具體不分析了。
還有相關的Activity和相關方法就不做一一介紹了。
流程理解和回調關系可以看下這篇:
https://blog.csdn.net/wy3243996/article/details/52411139
其他
AccountManager 沒有做深入使用,這里不做詳細介紹。
Account 管理都是需要app自身登錄的前提的,系統點擊"Add Account",
選擇對應應用后,實際是跳轉到應用的登錄界面。
在Android9.0發現火狐firefox應用是在"Add Account"列表中的,
但是Android11.0 的"Add Account"列表中沒看到火狐應用,咋回事?
研究發現是Android11版本 的新firefox沒有做賬戶管理。
那么如何查看apk中做了賬戶管理:
方式(1)使用工具解壓apk,查看AndroidManifest.xml文件
方式(2)把apk拖拽到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>如果看到了就說明,該應用時支持賬號同步功能的,如果沒有就是未做適配支持。
總結
以上是生活随笔為你收集整理的Android AccountManager 账户同步管理简单介绍的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: SDUTOJ 3034 ——炸学校
- 下一篇: android sina oauth2.