Android如何在账户设置中添加App的账户

本文详细介绍了在Android中如何实现App账户的添加和同步功能。从实现AbstractAccountAuthenticator开始,逐步讲解Service的配置、权限声明、登录界面的搭建,以及同步功能的集成,包括AbstractThreadedSyncAdapter的使用。此外,还讨论了如何通过AccountManager获取账户,分析了AccountManager的源码,并探讨了Account提高进程存活率的技巧。
摘要由CSDN通过智能技术生成

Android系统为外部服务提供了账号登录的机制,用于同步数据等作用。
进入设置->账户->添加账户,即可看到目前手机上有哪些App提供了同步服务。
这里写图片描述

接下来将会演示如何在App中定义登录服务并添加一个登录选项到这里。

账户的授权和同步

账户功能主要有2个:授权和同步。

授权:即账户登录怎么把关,可以通过验证账户密码的形式,也允许直接通过AccountManager的addAcount接口直接添加账户。
需要继承AbstractAccountAuthenticator来实现我们自己的授权机制。

同步:以邮箱账户为例,在添加一个Exchange邮箱账户后,我们可以看到它提供了联系人,日历,邮件等同步功能,这些同步服务每一项都需要App来继承实现AbstractThreadedSyncAdapter,用于该项的同步。
这里写图片描述

简单的演示

如何添加登录入口

第一步:实现AbstractAccountAuthenticator

在res/xml下添加一个xml,用于定义account-authenticator的以下属性:
sample_authenticator.xml

<account-authenticator
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:accountType="com.android.test" 
    android:icon="@drawable/ic_account_icon"
    android:smallIcon="@drawable/ic_account_icon"
    android:label="@string/account_title"/>

accountType -> 自定义的一个串,一般为包名
icon -> 图标
smallIcon -> 小图标,可能用于显示在状态栏等空间较小的地方
label -> 显示的账户名

接下来实现一个实现一个AbstractAccountAuthenticator,重点是实现addAccount方法的行为,现在先忽略具体实现。


public class TestAuthenticator extends AbstractAccountAuthenticator {
   

    Context mContext;
    AccountManager mManager;

    public TestAuthenticator(Context context) {
        super(context);
        mContext = context;
        mManager = AccountManager.get(context);
    }

    @Override
    public Bundle editProperties(AccountAuthenticatorResponse accountAuthenticatorResponse, String s) {
        throw new UnsupportedOperationException();
    }

    @Override
    public Bundle addAccount(AccountAuthenticatorResponse accountAuthenticatorResponse, String s, String s1, String[] strings, Bundle bundle) throws NetworkErrorException {
        //这里可以返回包含Login界面Intent的Bunble,在点击添加这个账户后就能进入我们自定义的登录界面
        return null;
    }

    @Override
    public Bundle confirmCredentials(AccountAuthenticatorResponse accountAuthenticatorResponse, Account account, Bundle bundle) throws NetworkErrorException {
        return null;
    }

    @Override
    public Bundle getAuthToken(AccountAuthenticatorResponse accountAuthenticatorResponse, Account account, String s, Bundle bundle) throws NetworkErrorException {
        return null;
    }

    @Override
    public String getAuthTokenLabel(String s) {
        return null;
    }

    @Override
    public Bundle updateCredentials(AccountAuthenticatorResponse accountAuthenticatorResponse, Account account, String s, Bundle bundle) throws NetworkErrorException {
        return null;
    }

    @Override
    public Bundle hasFeatures(AccountAuthenticatorResponse accountAuthenticatorResponse, Account account, String[] strings) throws NetworkErrorException {
        return null;
    }
}

第二步:实现Service和声明权限

实现一个Service,AccountManagerService需要通过Service来获取Authenticator中的的Binder,通过Binder回调来获取到我们自定义的登录行为。
在onBind中,我们需要返回TestAuthenticator中的IBinder:


public class TestAuthenticateService extends Service {
   
    TestAuthenticator mAuthenticator;

    @Override
    public void onCreate() {
        super.onCreate();
        mAuthenticator = new TestAuthenticator(this.getApplicationContext());
    }

    @Override
    public IBinder onBind(Intent intent) {
        //限制了只有在AccountManagerService绑定service
  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值