My application needs to synchronize some data from server. I added necessary classes (similarly to SampleSyncAdapter) now I can add account via "Settings/Sync and Accounts". But I want to have already added my account and working synchronization just after application is installed (I do not want user to do any manual changes in settings). How to do this?
Asked
Active
Viewed 9,675 times
3 Answers
7
A bit late but...
Account account = new Account("Title", "com.package.nom");
String password = "password";
AccountManager accountManager = AccountManager.get(context);
accountManager.addAccountExplicitly(account, password, null);

Ian Elliott
- 7,588
- 5
- 35
- 42
-
Unfortunately, it don't seems as simple as that (it will fail with `java.lang.SecurityException: caller uid 10007 is different than the authenticator's uid`)... see Udinic answer's bellow for a more complete answer. (maybe I miss something, but I really don't understand why this answer is accepted and upvoted ?) – ben75 Aug 28 '13 at 08:46
-
If you follow Ian's suggestion, please consider the following: https://stuff.mit.edu/afs/sipb/project/android/docs/training/id-auth/custom_auth.html#Security – Cray Jul 30 '15 at 11:48
-
@IanElliott : I am facing same issue like ben75. I tried adding authenticator.xml also. – Pawan Sep 04 '16 at 11:16
-
@ben75 : I am facing the same issue, Could you please guide what should be my next step. I tried decrypting my password & added account-authenticator/> also. – Pawan Sep 04 '16 at 11:32
-
@Pawan check this http://stackoverflow.com/questions/24227274/how-to-add-programmatically-a-custom-account-in-android/24228529#24228529 – ben75 Sep 04 '16 at 12:30
1
There is Android AtLeap library which contains helper classes to use Account Authenticator. Have a look at it https://github.com/blandware/android-atleap

Andrey Grebnev
- 226
- 2
- 3
1
Don't you have to add to the following code posted by Evan Elliott :
Account account = new Account("Title", "com.package.nom");
String password = "password";
AccountManager accountManager = AccountManager.get(context);
accountManager.addAccountExplicitly(account, password, null);
The following: ?
authenticator.xml
<?xml version="1.0" encoding="utf-8"?>
<account-authenticator xmlns:android="http://schemas.android.com/apk/res/android"
android:accountType="@string/account_type"
android:icon="@drawable/icon_hdpi"
android:smallIcon="@drawable/icon_hdpi"
android:label="@string/authenticator_label"
/>
and permissions?

Li3ro
- 1,837
- 2
- 27
- 35
-
do you mean , we need to add this code, permissions and this xml file and then account will be added. I am trying to add account but i am getting error. where should I include this xml file? Do I need to refer to this file in the code? Can you please explain it? :( – blackfyre Sep 11 '12 at 14:19
-
Google won't let you add account from code. the user must do this by himself. I gave up trying to use those methods and used the existing ones. – Li3ro Sep 12 '12 at 06:38
-
@Li3ro, you think so? How did Whatsapp, Facebook, Linkedin etc did managed this? I'm still searching for the answer btw.. :-) – Tom Sep 17 '12 at 15:16
-
whatsapp,facebook, linkedin etc are not googles account.. you can programaticly open your own account with the same code – Li3ro Sep 19 '12 at 07:01