接上文
- public class XXAuthService extends Service {
- private XXAuthenticator mAuthenticator;
- @Override
- public void onCreate() {
- mAuthenticator = new XXAuthenticator(this);
- }
- private XXAuthenticator getAuthenticator() {
- if (mAuthenticator == null)
- mAuthenticator = new XXAuthenticator(this);
- return mAuthenticator;
- }
- @Override
- public IBinder onBind(Intent intent) {
- return getAuthenticator().getIBinder();
- }
- class XXAuthenticator extends AbstractAccountAuthenticator {
- private final Context context;
- private AccountManager accountManager;
- public XXAuthenticator(Context context) {
- super(context);
- this.context = context;
- accountManager = AccountManager.get(context);
- }
- @Override
- public Bundle addAccount(AccountAuthenticatorResponse response, String accountType, String authTokenType, String[] requiredFeatures, Bundle options)
- throws NetworkErrorException {
- // 添加账号 示例代码
- final Bundle bundle = new Bundle();
- final Intent intent = new Intent(context, AuthActivity.class);
- intent.putExtra(AccountManager.KEY_ACCOUNT_AUTHENTICATOR_RESPONSE, response);
- bundle.putParcelable(AccountManager.KEY_INTENT, intent);
- return bundle;
- }
- @Override
- public Bundle getAuthToken(AccountAuthenticatorResponse response, Account account, String authTokenType, Bundle options)
- throws NetworkErrorException {
- // 认证 示例代码
- String authToken = accountManager.peekAuthToken(account, getString(R.string.account_token_type));
- //if not, might be expired, register again
- if (TextUtils.isEmpty(authToken)) {
- final String password = accountManager.getPassword(account);
- if (password != null) {
- //get new token
- authToken = account.name + password;
- }
- }
- //without password, need to sign again
- final Bundle bundle = new Bundle();
- if (!TextUtils.isEmpty(authToken)) {
- bundle.putString(AccountManager.KEY_ACCOUNT_NAME, account.name);
- bundle.putString(AccountManager.KEY_ACCOUNT_TYPE, account.type);
- bundle.putString(AccountManager.KEY_AUTHTOKEN, authToken);
- return bundle;
- }
- //no account data at all, need to do a sign
- final Intent intent = new Intent(context, AuthActivity.class);
- intent.putExtra(AccountManager.KEY_ACCOUNT_AUTHENTICATOR_RESPONSE, response);
- intent.putExtra(AuthActivity.ARG_ACCOUNT_NAME, account.name);
- bundle.putParcelable(AccountManager.KEY_INTENT, intent);
- return bundle;
- }
- @Override
- public String getAuthTokenLabel(String authTokenType) {
- // throw new UnsupportedOperationException();
- return null;
- }
- @Override
- public Bundle editProperties(AccountAuthenticatorResponse response, String accountType) {
- return null;
- }
- @Override
- public Bundle confirmCredentials(AccountAuthenticatorResponse response, Account account, Bundle options)
- throws NetworkErrorException {
- return null;
- }
- @Override
- public Bundle updateCredentials(AccountAuthenticatorResponse response, Account account, String authTokenType, Bundle options)
- throws NetworkErrorException {
- return null;
- }
- @Override
- public Bundle hasFeatures(AccountAuthenticatorResponse response, Account account, String[] features)
- throws NetworkErrorException {
- return null;
- }
- }
- }
- <service
- android:name="**.XXAuthService"
- android:exported="true"
- android:process=":core">
- <intent-filter>
- <action
- android:name="android.accounts.AccountAuthenticator"/>
- </intent-filter>
- <meta-data
- android:name="android.accounts.AccountAuthenticator"
- android:resource="@xml/authenticator"/>
- </service>
其中authenticator为:
- <?xml version="1.0" encoding="utf-8"?>
- <account-authenticator xmlns:android="http://schemas.android.com/apk/res/android"
- android:accountType="@string/account_auth_type"
- android:icon="@drawable/icon"
- android:smallIcon="@drawable/icon"
- android:label="@string/app_name"
- />
同SyncAdapter,通过AccountManager使用
。申请Token主要是通过 AccountManager.getAuthToken)系列方法
。添加账号则通过 AccountManager.addAccount)
。查看是否存在账号通过 AccountManager.getAccountsByType)
Refs
春节将至,原本欢乐祥和的气氛,却被各地散发的零星疫情给搞没了。特别是那些准...
斯坦福教授 Christopher Manning 用一页纸的篇幅介绍 AI 领域的核心概念。 在「A...
2019年,你想赶赶时髦,买一部5G手机吗?但从目前的分析来看,它可能会价格高、耗...
2020财年第四季度业绩: 销售额:122亿美元 o 同比下降9% 每股收益:根据GAAP每...
随着世界的不断无线化,世界也变得更加有线化。当今移动网络的背后是数十万英里...
博物馆是文化、教育和创造力的重要中心。无论您是参观古希腊展览还是惊叹于法老...
教育是百年树人的大业,应该有前瞻性。 在近日举办的国际人工智能与教育会议上,...
近日,美国总统川普大帝宣布批准6G试验,引起美国朝野震动,世界一片哗然,懂王...
本文转载自微信公众号「微月人话」,作者卫剑钒。转载本文请联系微月人话公众号...
XDA Developers 称谷歌正在开发用于虚拟机的精简版 Android,名为 MicroDroid。...