我有一個帳戶類型「mypackage.account」和一個內容管理「mypackage」。我有一個Service
,其提供的AbstractAccountAuthenticator
一個實施方式中,addAccount
方法被實現這樣的:使用Android AccountManager的自定義帳戶類型
/**
* The user has requested to add a new account to the system. We return an intent that will launch our login
* screen if the user has not logged in yet, otherwise our activity will just pass the user's credentials on to
* the account manager.
*/
@Override
public Bundle addAccount(AccountAuthenticatorResponse response, String account_type, String auth_token_type,
String[] required_features, Bundle options) throws NetworkErrorException {
final Intent intent = new Intent(_context, ConnectAccountActivity.class);
intent.putExtra(AccountManager.KEY_ACCOUNT_AUTHENTICATOR_RESPONSE, response);
final Bundle reply = new Bundle();
reply.putParcelable(AccountManager.KEY_INTENT, intent);
return reply;
}
我提供一種authenticator.xml
<account-authenticator xmlns:android="http://schemas.android.com/apk/res/android"
android:accountType="mypackage.account"
android:icon="@drawable/ic_launcher"
android:smallIcon="@drawable/ic_launcher"
android:label="@string/app_name"
android:accountPreferences="@xml/account_preferences" />
和我在AndroidManifest.xml
定義該Service
這樣的:
<!-- Account authentication service that provides the methods for authenticating KeepandShare accounts to the
AccountManager framework -->
<service android:exported="true" android:name=".authenticator.AccountAuthenticatorService" android:process=":auth" tools:ignore="ExportedService">
<intent-filter>
<action android:name="android.accounts.AccountAuthenticator"/>
</intent-filter>
<meta-data android:name="android.accounts.AccountAuthenticator" android:resource="@xml/authenticator"/>
</service>
這就是設置,現在當我想要一個屏幕機智^ h我的帳戶類型的帳戶列表在設備上與動作添加一個新的帳戶,我已經添加帳戶的行動看起來像這樣:
final Intent intent = new Intent(Settings.ACTION_ADD_ACCOUNT);
intent.putExtra(Settings.EXTRA_AUTHORITIES, new String[]{ "mypackage" });
startActivity(intent);
在這一點上,我帶領到一個帳戶類型選擇器顯示「mypackage.account」和「anotherpackage.account」作爲選項。 (「anotherpackage.account」在我工作的另一個應用程序中定義)這似乎不像預期的行爲。我查了大約20次,這兩個應用定義的權限是不同的 - 他們是。有人能告訴我我錯過了什麼嗎?
這是一個非常全面和有用的答案這個問題。我已經在SDK的「SampleSyncAdaptor」示例中添加了帳戶管理功能,雖然它在點擊「添加帳戶」時跳轉到了我的應用,但添加的帳戶不會顯示在手機設置中。我添加了Dandre的代碼和賓果 - 它的工作原理。不過請注意「Ln」 - 你不需要使用roboguice來完成這項工作 - 可能應該從代碼中刪除。 –
@TimT感謝您的評論。我實際上是從Roboguice中撕下了'Ln',我個人使用Dagger。看起來我只在這裏使用'Ln',所以我會編輯它以使用'Log'。 –
似乎很奇怪,即使您沒有使用同步適配器,也需要添加同步適配器。 嘗試按照此帖子上的說明創建您自己的身份驗證器,然後查看問題是否仍然存在: http://udinic.wordpress.com/2013/04/24/write-your-own-android-authenticator – Udinic