0

I'd like to create an account in my Android app programatically. I've read thing about the AccountManager class and tried to implement it.

Unfortunately, I get a SecurityException when I try to add the account.

Account account = new Account(user.getLogin(), 'packageName');

The thing I don't understand is about the type (in the second parameter).

What do I've to put in second parameter in order to make my code working.

Moreover, I'd like to know if I can add an acount simply with theses lines of code, or should I use a service like many example show ?

Thanks

EDIT - Full Exception Message

java.lang.SecurityException: caller uid 10047 is different than the authenticator's uid
at android.os.Parcel.readException(Parcel.java:1425)
at android.os.Parcel.readException(Parcel.java:1379)
at android.accounts.IAccountManager$Stub$Proxy.addAccount(IAccountManager.java:580)
at android.accounts.AccountManager.addAccountExplicitly(AccountManager.java:565)
at fr.opendev.elisaG.activity.LoginActivity.isUserAuthorized(LoginActivity.java:113)
at fr.opendev.elisaG.activity.LoginActivity.access$500(LoginActivity.java:27)
at fr.opendev.elisaG.activity.LoginActivity$1.onClick(LoginActivity.java:70)
at android.view.View.performClick(View.java:4084)
at android.view.View$PerformClick.run(View.java:16964)
at android.os.Handler.handleCallback(Handler.java:615)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4745)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
QuentR
  • 5,227
  • 3
  • 13
  • 12
  • Can you paste the full exception, please – Zharf Jul 09 '15 at 11:54
  • `addAccountExplicitly` can be only used to create account for your account type(defined in the same app or using the same UID) ... *This method requires the caller to hold the permission AUTHENTICATE_ACCOUNTS and to have the same UID as the added account's authenticator.* ... so you cannot create fx google account with it ... you need to use `addAccount` which launch the "sign-up wizard" from google apps – Selvin Jul 09 '15 at 11:55
  • Well, maybe it's not possible to do what i'd like... I' like to create a user account locally, after the user has been authenticate on my server. So the first time, the user put his credentials, and if it's ok, the account is created on the device, and don't have to log in each time the application is lauched... – QuentR Jul 09 '15 at 12:03
  • [similar question](http://stackoverflow.com/questions/10300534/securityexception-when-trying-to-add-an-account) with answers you might find useful – Zharf Jul 09 '15 at 12:13
  • So I've to implement this functionnality like this ? http://blog.udinic.com/2013/04/24/write-your-own-android-authenticator/ – QuentR Jul 09 '15 at 12:24
  • the value of the second parameter is the one you set on your Authenticator metadata file with this attribute `android:accountType` which is located res/xml/authenticator.xml. See https://developer.android.com/training/sync-adapters/creating-authenticator.html#CreateAuthenticatorFile – chip Jul 29 '15 at 09:06

0 Answers0