addAccount(finalStringaccountType,finalStringauthTokenType,finalString[]requiredFeatures,finalBundleaddAccountOptions,finalActivityactivity,AccountManagerCallback

亚洲免费在线-亚洲免费在线播放-亚洲免费在线观看-亚洲免费在线观看视频-亚洲免费在线看-亚洲免费在线视频

基于Andoird 4.2.2的Account Manager源代碼分析

系統 1689 0

AccountManager.addAccount()

?

        public AccountManagerFuture<Bundle> addAccount(final String accountType,

            final String authTokenType, final String[] requiredFeatures,

            final Bundle addAccountOptions,

            final Activity activity, AccountManagerCallback<Bundle> callback, Handler handler) {

        ...

    }
  

?

?

在程序中創建指定類型的系統帳號,需要提供一個AccountManagerCallback類型的回調,后面會講到其作用。

本方法要求用戶添加指定類型的帳號。
此種帳號類型對應的authenticator將加載對應的UI來處理這個請求。
方法返回一個AccountManagerFuture對象,可解析出一個Bundle,包含以下信息:
- KEY_ACCOUNT_NAME: 創建的帳號的名稱
- KEY_ACCOUNT_TYPE: 帳號類型

本方法創建一個匿名AmsTask實例并啟動:

?

            return new AmsTask(activity, handler, callback) {

            public void doWork() throws RemoteException {

                mService.addAcount(mResponse, accountType, authTokenType,

                        requiredFeatures, activity != null, optionsIn);

            }

        }.start();
  


這里,以異步的方式請求AccountManagerService.addAccount()
start()方法立即返回,返回值是AccountManagerFuture類型的。

?

AccountManagerService.addAccount()

這個方法中,創建一個Session類型的匿名實例,并調用其bind()方法,最終捆綁到應用程序提供的authenticator service:

?

                new Session(accounts, response, accountType, expectActivityLaunch,

                    true /* stripAuthTokenFromResult */) {

                public void run() throws RemoteException {

                    mAuthenticator.addAccount(this, mAccountType, authTokenType, requiredFeatures,

                            options);

                }



                protected String toDebugString(long now) {

                    return super.toDebugString(now) + ", addAccount"

                            + ", accountType " + accountType

                            + ", requiredFeatures "

                            + (requiredFeatures != null

                              ? TextUtils.join(",", requiredFeatures)

                              : null);

                }

            }.bind();
  


這是Session.bind()方法的相關細節:

?

?

            void bind() {

            ...

            if (!bindToAuthenticator(mAccountType)) {

                Log.d(TAG, "bind attempt failed for " + toDebugString());

                onError(AccountManager.ERROR_CODE_REMOTE_EXCEPTION, "bind failure");

            }

        }


  


bindToAuthenticator()方法找到對應的組件名稱(應用程序中定義的相關service),并且對Service發起綁定:

?

?

            private boolean bindToAuthenticator(String authenticatorType) {

            final AccountAuthenticatorCache.ServiceInfo<AuthenticatorDescription> authenticatorInfo;

            authenticatorInfo = mAuthenticatorCache.getServiceInfo(

                    AuthenticatorDescription.newKey(authenticatorType), mAccounts.userId);

            ...



            Intent intent = new Intent();

            intent.setAction(AccountManager.ACTION_AUTHENTICATOR_INTENT);

            intent.setComponent(authenticatorInfo.componentName);

            ...



            if (!mContext.bindService(intent, this, Context.BIND_AUTO_CREATE, mAccounts.userId)) {

                ...

            }



            return true;

        }
  


Session類實現了ServiceConnection接口,因此,當成功綁定到對應的應用程序中的Service,其實現的onServiceConnected()方法將被調用:

?

?

            public void onServiceConnected(ComponentName name, IBinder service) {

            mAuthenticator = IAccountAuthenticator.Stub.asInterface(service);

            try {

                run();

            } catch (RemoteException e) {

                onError(AccountManager.ERROR_CODE_REMOTE_EXCEPTION,

                        "remote exception");

            }

        }
  


這里的service,即是AbstractAuthenticator抽象類提供的IBinder:

?

?

    public abstract class AbstractAccountAuthenticator {

    ...



    private class Transport extends IAccountAuthenticator.Stub {

        public void addAccount(IAccountAuthenticatorResponse response, String accountType,

                String authTokenType, String[] features, Bundle options)

                throws RemoteException {

            ...



            try {

                final Bundle result = AbstractAccountAuthenticator.this.addAccount(

                    new AccountAuthenticatorResponse(response),

                        accountType, authTokenType, features, options);

                ...

            }

            ...

        }

        ...

    }

    ...

    private Transport mTransport = new Transport();

    

    /**

     * @return the IBinder for the AccountAuthenticator

     */

    public final IBinder getIBinder() {

        return mTransport.asBinder();

    }

    ...

}
  


AbstractAccountAuthenticator的內部類Transport是IAccountAuthenticator接口的一個實現。后者規定了Authenticator的一組行為。
以添加帳號的操作為例,作為接口實現的Transport的addAccount()方法調用了AbstractAccountAuthenticator類的addAccount()抽象方法,這個方法的具體實現,則由應用程序中定義的authenticator子類來完成。

這里涉及到IPC,應用程序是服務端,提供服務的實現,而AccountManagerService則是客戶端,負責通過代理對象發起調用。

?

Email的authenticator實現:

?

        class PopImapAuthenticator extends AbstractAccountAuthenticator {

        ...



        @Override

        public Bundle addAccount(AccountAuthenticatorResponse response, String accountType,

                String authTokenType, String[] requiredFeatures, Bundle options)

                throws NetworkErrorException {

            // There are two cases here:

            // 1) We are called with a username/password; this comes from the traditional email

            //    app UI; we simply create the account and return the proper bundle

            if (options != null && options.containsKey(OPTIONS_PASSWORD)

                    && options.containsKey(OPTIONS_USERNAME)) {

                final Account account = new Account(options.getString(OPTIONS_USERNAME),

                        AccountManagerTypes.TYPE_POP_IMAP);

                AccountManager.get(PopImapAuthenticatorService.this).addAccountExplicitly(

                            account, options.getString(OPTIONS_PASSWORD), null);



                ...



                Bundle b = new Bundle();

                b.putString(AccountManager.KEY_ACCOUNT_NAME, options.getString(OPTIONS_USERNAME));

                b.putString(AccountManager.KEY_ACCOUNT_TYPE, AccountManagerTypes.TYPE_POP_IMAP);

                return b;

            // 2) The other case is that we're creating a new account from an Account manager

            //    activity.  In this case, we add an intent that will be used to gather the

            //    account information...

            } else {

                Bundle b = new Bundle();

                Intent intent =

                    AccountSetupBasics.actionSetupPopImapIntent(PopImapAuthenticatorService.this);

                intent.putExtra(AccountManager.KEY_ACCOUNT_AUTHENTICATOR_RESPONSE, response);

                b.putParcelable(AccountManager.KEY_INTENT, intent);

                return b;

            }

        }
  


1) 如果是Email應用程序內部添加新的電子郵件帳號,此時已經取得了帳號的用戶名和密碼,那么直接創建對應的系統帳號,并調用AccountManager.addAccountExplicitly()將其添加到系統帳號數據庫中,并返回帳號名稱和類型。
2) 如果是從外部,比如系統設置中添加Email帳號,則創建指向Email應用中創建帳號對應的activity的Intent,并返回。這樣,AmsTask實例在完成時會通過Handler機制調用AddAccountSettings活動提交的一個回調:

?

?

       private AccountManagerCallback<Bundle> mCallback = new AccountManagerCallback<Bundle>() {

        public void run(AccountManagerFuture<Bundle> future) {

            boolean done = true;

            try {

                Bundle bundle = future.getResult();

                //bundle.keySet();

                Intent intent = (Intent) bundle.get(AccountManager.KEY_INTENT);

                if (intent != null) {

                    done = false;

                    Bundle addAccountOptions = new Bundle();

                    addAccountOptions.putParcelable(KEY_CALLER_IDENTITY, mPendingIntent);

                    addAccountOptions.putBoolean(EXTRA_HAS_MULTIPLE_USERS,

                            Utils.hasMultipleUsers(AddAccountSettings.this));

                    intent.putExtras(addAccountOptions);

                    startActivityForResult(intent, ADD_ACCOUNT_REQUEST);

            ...

    }
  


這樣就會啟動Email創建帳號的activity,之后又會走到1)中的步驟了。

?



?

基于Andoird 4.2.2的Account Manager源代碼分析學習:創建選定類型的系統帳號


更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 成人激情小视频 | 亚洲人成绝费网站色ww | 国产成+人+亚洲+欧美+日韩 | 超级乱淫视频aⅴ播放视频 超级乱淫视频播放日韩 | 精品国产一二三区在线影院 | 中文字幕不卡 | 51啪影院 | 91热成人精品国产免费 | 欧美乱大交xxxxx在线观看 | 久久99精品久久久 | 性夜黄a爽爽免费视频国产 性夜影院爽黄a爽免费看网站 | 国产九九热视频 | 成人国产午夜在线视频 | 欧美特欧美特级一片 | videoxxoo欧美老师 | 久久线看观看精品香蕉国产 | 欧美在线一区视频 | 免费中文字幕一级毛片 | 在线激情网址 | 国产一区二区播放 | 99精品视频在线播放2 | 奇米成人 | 夜夜女人国产香蕉久久精品 | 久久草在线视频免费 | 日日干影院 | 久久成人网18网站 | 免费一级毛毛片 | 亚洲综合色秘密影院秘密影院 | h在线免费视频 | 日本欧美国产精品 | 欧美国产亚洲精品a第一页 欧美国产亚洲精品高清不卡 | 亚洲成人在线免费 | 国产午夜亚洲精品不卡福利 | 精品午夜寂寞影院在线观看 | 99热在线免费播放 | 95视频在线观看在线分类h片 | 羞羞视频网页 | 欧美性猛交xxxxbbbb | 激情综合五月 | 99热久久国产精品免费观看 | 日本操操操 |