In the SDK provided by Android, there is an example called Samplesyncadapter in the Samples directory, which is an instance of the account and synchronization, such as Google's original Android phone can use Google account to synchronize data. For example, if you want to sync your contacts to the server in real time, you can use this example to learn about the synchronization mechanism provided by Android to achieve your own synchronization capabilities. Let's introduce the management section of the account. As for the account Management code is mainly in the authenticator package under the three classes, there is a call authenticator XML file.
AuthenticationService class
AuthenticationService is a service that inherits services that are actually provided to other processes, and the Android system passes "Android.accounts.AccountAuthenticator" This action finds it and uses it to register our own account with the "settings", which is actually a aidl use, which is a cross-process call. Here is the registration in manifest:
<service android:name= ". Authenticator. 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 >
The service returns a IBinder to the client process in the Onbind method, as follows:
@Override public IBinder onbind (Intent Intent) { if (log.isloggable (tag, log.verbose)) { LOG.V (tag, " Getbinder () ... Returning the Accountauthenticator binder for intent " + intent); } return Mauthenticator.getibinder (); }
Authenticator class
Authenticator is a class inherited from the Abstractaccountauthenticator, Abstractaccountauthenticator is a virtual class, it defines the processing phone "settings" in "Account and synchronization" The basic interface of the Add, delete, and verify functions of the account, and implements some basic functions. Inside the Abstractaccountauthenticator, there is an inner class that inherits from the Iaccountauthenticator.stub, which is used to wrap the Abstractaccountauthenticator remote interface calls. We can return the IBinder form of the inner class through the Abstractaccountauthenticator Getibinder () method to make a remote call to this class, such as the call in the preceding code Onbind method. The source location of the abstractaccountauthenticator is in the Frameworks\base\core\java\android\accounts directory. Authenticator only need to inherit and implement a few methods of abstractaccountauthenticator, like we introduced the Samplesyncadapter instance of the main inheritance of two methods, as follows
</pre><p></p></div><div style= "font-family: ' Helvetica Neue '; font-size:14px "><p><pre name=" code "class=" Java >//when adding an account in Settings, this method is called to jump to the Add Account page @override public Bundle AddAccount (accountauthenticatorresponse response, String accounttype, String Authtokentype, string[] Req Uiredfeatures, Bundle options) {log.v (TAG, "AddAccount ()");//Specify Authenticatoractivity as the page for adding accounts, as described below. Final Intent Intent = new Intent (Mcontext, Authenticatoractivity.class); Intent.putextra (Accountmanager.key_account_authenticator_response, RESPONSE); Final bundle bundle = new bundle (); Bundle.putparcelable (Accountmanager.key_intent, INTENT); return bundle; }//when the Maccountmanager.blockinggetauthtoken (Account,constants.authtoken_type, notify_auth_failure) is executed, the method is called. @Override public Bundle Getauthtoken (accountauthenticatorresponse response, account account, String Authtoke NType, Bundle loginoptions) throws Networkerrorexception {log.v (TAG, "Getauthtoken ()");//Constants.authtoken_type if via Blockinggetauthtoken method (!authtokentype.equals (Constants.authtoken_type)) {Final Bundle result = new bundle (); Result.putstring (accountmanager.key_error_message, "Invalid Authtokentype"); return result; }final Accountmanager am = Accountmanager.get (mcontext); Final String password = am.getpassword (account); if (password! = null) {final String AuthToken = networkutilities.authenticate (account.name, password); if (! Textutils.isempty (AuthToken)) {//If the account has been authenticated to the server and saved to Accountmanager final bundle result = new bundle (); Result.putstring (Accountmanager.key_account_name, account.name); Result.putstring (Accountmanager.key_account_type, Constants.account_type); Result.putstring (Accountmanager.key_authtoken, AUTHTOKEN); return result; } }//if the account has not been verified to the server and saved to Accountmanager, then re-add the Account page to verify. Final Intent Intent = new Intent (Mcontext, Authenticatoractivity.class); Intent.putextra (Authenticatoractivity.param_username, account.name); Intent.putextra (Authenticatoractivity.param_authtoken_type, Authtokentype); Intent.putextra (Accountmanager.key_account_authenticator_response, RESPONSE); Final bundle bundle = new bundle (); Bundle.putparcelable (Accountmanager.key_intent, INTENT); return bundle; }
Authenticatoractivity class
Authenticatoractivity is an inherited from the accountauthenticatoractivity activity,accountauthenticatoractivity source code is also in the frameworks\ Base\core\java the \android\accounts directory. One of the main methods of Authenticatoractivity is Handlelogin (view view), which is called when the Sign In button is clicked, which initiates an asynchronous task to request the server to authenticate the user's account. After successful verification, there is
An important way to do this:
/** * Called when response are received from the server for authentication * request. See Onauthenticationresult (). Sets the * Accountauthenticatorresult which is sent back to the caller. We store the * AuthToken that's returned from the server as the ' password ' for this * account-so we ' re never sto Ring the user ' s actual password locally. * * @param result the confirmcredentials. */private void Finishlogin (String authToken) {log.i (TAG, "Finishlogin ()"); Final Account Account = new Account (Musername, constants.account_type); if (mrequestnewaccount) {//Add an account maccountmanager.addaccountexplicitly directly to Accountmanager, Mpassword , null); Setup allows this account to automatically sync contentresolver.setsyncautomatically (accounts, contactscontract.authority, true); } else {Maccountmanager.setpassword (account, Mpassword); } final Intent Intent = new Intent (); Intent.putextra (AccountmanaGer. Key_account_name, Musername); Intent.putextra (Accountmanager.key_account_type, Constants.account_type); Setaccountauthenticatorresult (Intent.getextras ()); Setresult (RESULT_OK, intent); Finish (); }
Authenticator.xml
In the above AuthenticationService registration there is a meta-data name of Android.accounts.AccountAuthenticator, it points to the XML file is Authenticator.xml, its contents are as follows:
<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"/>
The account type is Com.example.android.samplesync, which is the value of Constants.account_type. This is a bit like a widget that requires an XML to provide the information you want.
Android Account management mechanism