尽管将其注销,但听起来您正在寻找 AccountManager。AndroidSDK 中的旧版 SampleSyncAdapter 项目包含一个完整示例,说明如何将其用于您描述的那种凭证和会话管理:
/**
* This class is an implementation of AbstractAccountAuthenticator for
* authenticating accounts in the com.example.android.samplesync domain. The
* interesting thing that this class demonstrates is the use of authTokens as
* part of the authentication process. In the account setup UI, the user enters
* their username and password. But for our subsequent calls off to the service
* for syncing, we want to use an authtoken instead - so we're not continually
* sending the password over the wire. getAuthToken() will be called when
* SyncAdapter calls AccountManager.blockingGetAuthToken(). When we get called,
* we need to return the appropriate authToken for the specified account. If we
* already have an authToken stored in the account, we return that authToken. If
* we don't, but we do have a username and password, then we'll attempt to talk
* to the sample service to fetch an authToken. If that fails (or we didn't have
* a username/password), then we need to prompt the user - so we create an
* AuthenticatorActivity intent and return that. That will display the dialog
* that prompts the user for their login information.
*/
(但另请参阅有关该样本失败的答案。)
它通过 Web 服务进行身份验证,处理加密,否则听起来就像您正在寻找的那样。如果有特定原因您认为它不合适,请详细说明。