接上文
成都创新互联公司-专业网站定制、快速模板网站建设、高性价比永川网站开发、企业建站全套包干低至880元,成熟完善的模板库,直接使用。一站式永川网站制作公司更省心,省钱,快速模板网站建设找我们,业务覆盖永川地区。费用合理售后完善,十载实体公司更值得信赖。
- 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;
- }
- }
- }
- Android:name="**.XXAuthService"
- android:exported="true"
- android:process=":core">
- android:name="android.accounts.AccountAuthenticator"/>
- android:name="android.accounts.AccountAuthenticator"
- android:resource="@xml/authenticator"/>
其中authenticator为:
- 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
网页名称:一种提高Android应用进程存活率新方法(下)
网页地址:http://www.gawzjz.com/qtweb2/news14/16264.html
网站建设、网络推广公司-创新互联,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等
声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联