I am trying to create an account for my app, where I will be able to have my contacts against my account like facebook, viber, whatsapp etc. I want my account to be visible
here a code snipped I am doing it ( sorry for german commetns ) don't forget to set the propper permissions in the manifest file.
/**
* ueberprueft, ob es den account fuer diese app schon gibt und legt ihn
* gegebenenfalls an.
*
* @param none
* @return void
*/
public void verifyAccount() {
if (debug)
Log.i(TAG, "verifyAccount() ");
boolean bereitsAngelegt = false;
String accountType;
accountType = this.getPackageName();
AccountManager accountManager = AccountManager
.get(getApplicationContext());
Account[] accounts = accountManager.getAccounts();
for (int i = 0; i < accounts.length; i++) {
if (debug)
Log.v(TAG, accounts[i].toString());
if ((accounts[i].type != null)
&& (accounts[i].type.contentEquals(accountType))) {
bereitsAngelegt = true;
if (debug)
Log.v(TAG, "verifyAccount(): bereitsAngelegt "
+ accounts[i].type);
}
}
if (!bereitsAngelegt) {
if (debug)
Log.v(TAG, "verifyAccount(): !bereitsAngelegt ");
// This is the magic that addes the account to the Android Account
// Manager
AccountManager accMgr = AccountManager.get(this);
String password = "some_password";
if (debug)
Log.d(TAG, "verifyAccount(): ADD: accountName: "
+ Konst.accountName + " accountType: " + accountType
+ " password: " + password);
final Account account = new Account(Konst.accountName, accountType);
if (debug)
Log.v(TAG, "verifyAccount(): nach final Account account ");
try {
accMgr.addAccountExplicitly(account, password, null);
} catch (Exception e1) {
if (debug)
Log.v(TAG, "verifyAccount(): Exception e1 " + e1.toString());
this.finish();
}
if (debug)
Log.v(TAG,
"verifyAccount(): nach accMgr.addAccountExplicitly() ");
} else {
if (debug)
Log.v(TAG, "verifyAccount(): bereitsAngelegt ");
}
} // end of public void verifyAccount()
i hope this helps a little bit.