Adding account through Account manager
Asked Answered
S

1

8

I'm new to android development so sorry if im asking a really obvious question.For my app I want to add a google account automatically using account manager. Do I want to use addAccount or addAccountExplicitly and how do i go about this assuming the account name was "platinum" and the password is "software".

Sloven answered 19/3, 2012 at 18:51 Comment(0)
S
13

try this :

Permission required :

<uses-permission android:name="android.permission.MANAGE_ACCOUNTS"/></uses-permission> 
<uses-permission android:name="android.permission.ACCOUNT_MANAGER"></uses-permission> 
<uses-permission android:name="android.permission.GET_ACCOUNTS"></uses-permission> 
<uses-permission android:name="android.permission.AUTHENTICATE_ACCOUNTS"></uses-permission>

AuthenticatorActivity.java

public class SleepyAccountAuthenticatorActivity extends AccountAuthenticatorActivity  
{  
protected void onCreate(Bundle icicle)  
{  
    super.onCreate(icicle);  
    setContentView(R.layout.new_account);  
    final Button done = (Button) findViewById(R.id.new_account_done);  
    final EditText server = (EditText) findViewById(R.id.new_account_server);  
    final EditText username = (EditText) findViewById(R.id.new_account_username);  
    final EditText password = (EditText) findViewById(R.id.new_account_password);  
    final Activity self = this;  
    done.setOnClickListener(new OnClickListener() {  
        public void onClick(View v)  
        {  
            //Account
            Account account=new Account(username.getText().  
            toString(), getString(R.string.ACCOUNT_TYPE));  

            Bundle userdata = new Bundle();   
            userdata.putString("SERVER", server.getText().toString());  
            //AccountManager  
            AccountManager am = AccountManager.get(self);  

            if (am.addAccountExplicitly(account, password.  
            getText().toString(), userdata))  
            {  
                Bundle result = new Bundle();  
                result.putString(AccountManager.KEY_ACCOUNT_NAME, username.getText().toString());  
                result.putString(AccountManager.KEY_ACCOUNT_TYPE,getString(R.string.ACCOUNT_TYPE));  
                setAccountAuthenticatorResult(result);  
            }  
            finish();  
        }  
    });  
}  
}

\res\xml\ authenticator.xml

<?xml version="1.0" encoding="utf-8"?> 
 <account-authenticator xmlns:android="http://schemas.android.com/apk/res/android" 
 android:accountType="com.yarin.AccountType" 
 android:icon="@drawable/icon" 
 android:smallIcon="@drawable/icon" 
 android:label="@string/ACCOUNT_LABEL" 
 android:accountPreferences="@xml/account_preferences" 
 />
Salutatory answered 19/3, 2012 at 19:27 Comment(5)
@imran Can you please post your full source code? This will help me too.Tanney
setAccountAuthenticatorResult is method defined in AccountAuthenticatorActivity class see here developer.android.com/reference/android/accounts/…Depalma
Can you provide some help links?Tanney
I have tried exactly the same. I am getting the exception java.lang.SecurityException: caller uid 10039 is different than the authenticator's uid everytime.Is it to do with the android:accountType ? Is it that u have given the package name there ?Remnant
though this did not resolve my query i m going to give an upvote for the simplicity of the ans and for the help :) :)Remnant

© 2022 - 2024 — McMap. All rights reserved.