tags:

views:

223

answers:

1

I have android.permission.READ_OWNER_DATA but I can't find any reliable code that would explain how can I read email address of device's owner. And please don't turn this into 'why you wanna do that' thread.

Thanks for help!

+3  A: 

Why you wanna do that?

import android.accounts.Account;
import android.accounts.AccountManager;
import android.content.Context;

/**
 * This class uses the AccountManager to get the primary email address of the
 * current user.
 */
public class UserEmailFetcher {

  static String getEmail(Context context) {
    AccountManager accountManager = AccountManager.get(context); 
    Account account = getAccount(accountManager);

    if (account == null) {
      return null;
    } else {
      return account.name;
    }
  }

  private static Account getAccount(AccountManager accountManager) {
    Account[] accounts = accountManager.getAccountsByType("com.google");
    Account account;
    if (accounts.length > 0) {
      account = accounts[0];      
    } else {
      account = null;
    }
    return account;
  }
}

In your AnroidManifest.xml

<uses-permission android:name="android.permission.GET_ACCOUNTS" />
Jim Blackler
This looks very nice... but I need something that works with Android 1.5? Do you have any class for older API in your sleeve?
kape123
This won't work in Android 1.* I'm afraid. If it's any comfort you can still use this in an app that targets 1.5, and cleanly detect when the functionality isn't available with try { String name = UserEmailFetcher.getEmail(this); } catch (VerifyError e) { // Happens if the AccountManager is not available (e.g. 1.x) }You would need <uses-sdk android:minSdkVersion="3" />in your manifest.
Jim Blackler
What about this -> http://www.mailinglistarchive.com/html/[email protected]/2009-06/msg01032.html (I just can't seem to find where that GoogleLoginServiceHelper is defined?)
kape123
It's hidden, you'll have to use reflection to get it. Don't if you _really_ need to.
alexanderblom