Android AccountManager 账户同步管理简单介绍

Android AccountManager 账户同步管理简单介绍

前言

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;

    @Override
    public void onCreate() {
        LogUtil.debug("");
        mAuthenticator = new MyAuthenticator(this);
    }

    @Override
    public IBinder onBind(Intent intent) {
        LogUtil.debug("getBinder()...  returning the AccountAuthenticator binder for intent " + intent);
        LogUtil.debug("mAuthenticator = " + mAuthenticator);
        return mAuthenticator.getIBinder();
    }
}

一定记得要在AndroidManifest.xml注册

    <service
        android:name=".MyAuthenticationService"
        android:exported="true">
        <intent-filter>
            <action
                android:name="android.accounts.AccountAuthenticator" />
        </intent-filter>
        <meta-data
            android: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);
    }

    @Override
    public Bundle editProperties(AccountAuthenticatorResponse r, String s) {
        return null;
    }

    @Override
    public 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



   <service
            android:name=".AuthenticationService"
            android:exported="true">
            <intent-filter>
                <action
                    android:name="android.accounts.AccountAuthenticator" />
            </intent-filter>
            <meta-data
                android:name="android.accounts.AccountAuthenticator"
                android:resource="@xml/authenticator" />
        </service>

如果看到了就说明,该应用时支持账号同步功能的,如果没有就是未做适配支持。

猜你喜欢

转载自blog.csdn.net/wenzhi20102321/article/details/126631128
今日推荐