2011-09-09 24 views
14

Tôi muốn chuẩn bị sẵn một số trường trong ứng dụng của mình để trợ giúp người dùng khi đăng ký dịch vụ bên trong ứng dụng của tôi.Android - Nhận UserData bằng cách sử dụng AccountManager/Tên và họ của chủ sở hữu điện thoại

Vậy làm thế nào tôi có thể nhận được tên và họ của chủ sở hữu thiết bị. Tôi muốn sử dụng thông tin mặc định được liên kết với tài khoản Google; cho đến nay tôi nhận điều này:

AccountManager am = AccountManager.get(this); 
Account[] accounts = am.getAccounts(); 
for (Account account : accounts) { 
    if (account.type.compareTo("com.google") == 0) 
    { 
     String possibleEmail = account.name; 
     // how to get firstname and lastname here? 
    }    
} 

Tôi sẵn sàng chấp nhận cách tiếp cận thay thế nếu bạn đề nghị họ - chỉ miễn là tôi có thể nhận được email, firstname và lastname của chủ sở hữu.

Trả lời

23

Trong Ice Cream Sandwich nhận thông tin này thật dễ dàng, vì Android bao gồm hồ sơ cá nhân đại diện cho chủ sở hữu thiết bị - tiểu sử này được gọi là hồ sơ "Tôi" và được lưu trữ trong bảng ContactsContract.Profile. Bạn có thể đọc dữ liệu từ hồ sơ của người dùng miễn là bạn yêu cầu các quyền READ_PROFILEREAD_CONTACTS trong tệp AndroidManifest.xml của mình.

Các trường có liên quan nhất cho bạn là cột DISPLAY_NAME từ Địa chỉ liên hệ và có thể là các trường StructuredName - những thứ như ảnh liên hệ của người dùng cũng có sẵn.

Có hướng dẫn về Lab mã Android cung cấp a full example of reading a user's profile, bit lõi của mã nằm trong ListProfileTask. Dưới đây là một đoạn trích được tóm lược:

Cursor c = activity.getContentResolver().query(ContactsContract.Profile.CONTENT_URI, null, null, null, null); 
int count = c.getCount(); 
String[] columnNames = c.getColumnNames(); 
boolean b = c.moveToFirst(); 
int position = c.getPosition(); 
if (count == 1 && position == 0) { 
    for (int j = 0; j < columnNames.length; j++) { 
     String columnName = columnNames[j]; 
     String columnValue = c.getString(c.getColumnIndex(columnName))); 
     ... 
     // consume the values here 
    } 
} 
c.close(); 

Tôi không nghĩ rằng có cách nhận loại dữ liệu này trước API cấp 14, thật không may.

+1

này là tốt, nhưng tôi không nghĩ rằng có bất kỳ sự đảm bảo rằng tiểu sử "Tôi" được điền. – gcl1

+2

@ gcl1 Thật vậy, không có sự đảm bảo nào và đó là lý do tại sao nếu hồ sơ của bạn không phải là SET, bạn cần lấy tên từ người quản lý tài khoản. – amar

+0

Tôi chỉ có thể nhận được 'account.name' là email của tôi chứ không phải là họ và tên. Bất kỳ ý tưởng? – Si8

1

Dưới đây là làm thế nào tôi đã làm điều đó (cũng từ api 14):

public class MainActivity implements LoaderManager.LoaderCallbacks<Cursor> { 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    getSupportLoaderManager().initLoader(0, null, this); 
} 



@Override 
public Loader<Cursor> onCreateLoader(int id, Bundle args) { 
    return new CursorLoader(this, 
      // Retrieve data rows for the device user's 'profile' contact. 
      Uri.withAppendedPath(
        ContactsContract.Profile.CONTENT_URI, 
        ContactsContract.Contacts.Data.CONTENT_DIRECTORY), 
      ProfileQuery.PROJECTION, 

      // Select only name. 
      ContactsContract.Contacts.Data.MIMETYPE + "=?", 
      new String[]{ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE}, 

      // Show primary fields first. Note that there won't be 
      // a primary fields if the user hasn't specified one. 
      ContactsContract.Contacts.Data.IS_PRIMARY + " DESC"); 
} 

@Override 
public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) { 
    User user = new User(); 
    List<String> names = new ArrayList<>(); 
    cursor.moveToFirst(); 
    String mimeType; 
    while (!cursor.isAfterLast()) { 
     mimeType = cursor.getString(ProfileQuery.MIME_TYPE); 
     switch (mimeType) { 

      case ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE: 
       String name = cursor.getString(ProfileQuery.GIVEN_NAME) + " " + cursor.getString(ProfileQuery.FAMILY_NAME); 
       if (!TextUtils.isEmpty(name)) { 
        names.add(name); 
       } 
       break; 
     } 
     cursor.moveToNext(); 
    } 

    if (!names.isEmpty()) { 
     // do with names whatever you want 
    } 
} 



@Override 
public void onLoaderReset(Loader<Cursor> loader) { 

} 

private interface ProfileQuery { 
    String[] PROJECTION = { 
      ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME, 
      ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME, 
      ContactsContract.Contacts.Data.MIMETYPE 
    }; 


    /** 
    * Column index for the family name in the profile query results 
    */ 
    int FAMILY_NAME = 0; 
    /** 
    * Column index for the given name in the profile query results 
    */ 
    int GIVEN_NAME = 1; 
    /** 
    * Column index for the MIME type in the profile query results 
    */ 
    int MIME_TYPE = 2; 
} 

Và cần có điều khoản:

<uses-permission android:name="android.permission.READ_PROFILE" /> 
<uses-permission android:name="android.permission.READ_CONTACTS" /> 
Các vấn đề liên quan