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>

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

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
好的,你想了解银行账户管理系统的C++代码,这是一个比较复杂的项目,需要设计多个类和函数,我将给出一个大致的框架供您参考: ```c++ #include <iostream> #include <string> #include <vector> using namespace std; // 账户类 class Account { private: string name; int number; double balance; public: Account(string n, int num, double bal); // 构造函数 void deposit(double amount); // 存款 void withdraw(double amount); // 取款 void display(); // 显示账户信息 }; // 账户管理类 class AccountManager { private: vector<Account> accounts; // 存储账户的容器 public: void addAccount(Account account); // 添加账户 void removeAccount(int accountNumber); // 删除账户 Account * findAccount(int accountNumber); // 查找账户 void displayAll(); // 显示所有账户信息 }; // 主函数 int main() { AccountManager manager; // 添加账户 Account a1("张三", 1001, 10000); manager.addAccount(a1); Account a2("李四", 1002, 20000); manager.addAccount(a2); // 查找账户并操作 Account * account = manager.findAccount(1001); if (account != NULL) { account->deposit(5000); account->withdraw(2000); } // 显示所有账户信息 manager.displayAll(); return 0; } // 实现账户Account::Account(string n, int num, double bal) { name = n; number = num; balance = bal; } void Account::deposit(double amount) { balance += amount; } void Account::withdraw(double amount) { if (balance >= amount) { balance -= amount; } } void Account::display() { cout << "姓名:" << name << endl; cout << "账号:" << number << endl; cout << "余额:" << balance << endl; } // 实现账户管理类 void AccountManager::addAccount(Account account) { accounts.push_back(account); } void AccountManager::removeAccount(int accountNumber) { for (vector<Account>::iterator it = accounts.begin(); it != accounts.end(); it++) { if (it->getNumber() == accountNumber) { accounts.erase(it); break; } } } Account * AccountManager::findAccount(int accountNumber) { for (vector<Account>::iterator it = accounts.begin(); it != accounts.end(); it++) { if (it->getNumber() == accountNumber) { return &(*it); } } return NULL; } void AccountManager::displayAll() { for (vector<Account>::iterator it = accounts.begin(); it != accounts.end(); it++) { it->display(); } } ``` 这个框架包括了账户类和账户管理类两个主要的类,其中账户类包含了姓名、账号和余额等信息,而账户管理类则包含了添加账户、删除账户、查找账户和显示所有账户信息等操作。在主函数中,我们可以看到如何使用这个账户管理系统,包括添加账户、查找账户并进行操作以及显示所有账户信息。需要注意的是,这个代码只是一个框架,实际使用时还需要根据需求进行修改和完善。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

峥嵘life

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值