Share via


사용자 프로필

Android는 API 수준 5부터 ContactsContract 공급자와의 연락처 열거를 지원했습니다. 예를 들어 연락처 나열은 다음 코드 예제와 같이 ContactContracts.Contacts 클래스를 사용하는 것만큼 간단합니다.

// Get the URI for the user's contacts:
var uri = ContactsContract.Contacts.ContentUri;

// Setup the "projection" (columns we want) for only the ID and display name:
string[] projection = {
    ContactsContract.Contacts.InterfaceConsts.Id,
    ContactsContract.Contacts.InterfaceConsts.DisplayName };

// Use a CursorLoader to retrieve the user's contacts data:
CursorLoader loader = new CursorLoader(this, uri, projection, null, null, null);
ICursor cursor = (ICursor)loader.LoadInBackground();

// Print the contact data to the console if reading back succeeds:
if (cursor != null)
{
    if (cursor.MoveToFirst())
    {
        do
        {
            Console.WriteLine("Contact ID: {0}, Contact Name: {1}",
                               cursor.GetString(cursor.GetColumnIndex(projection[0])),
                               cursor.GetString(cursor.GetColumnIndex(projection[1])));
        } while (cursor.MoveToNext());
    }
}

Android 4(API 수준 14) 부터 ContactsContact.Profile 클래스는 공급자를 ContactsContract 통해 사용할 수 있습니다. 디바이스 ContactsContact.Profile 소유자의 이름 및 전화 번호와 같은 연락처 데이터를 포함하는 디바이스 소유자의 개인 프로필에 대한 액세스를 제공합니다.

필요한 권한

연락처 데이터를 읽고 쓰려면 애플리케이션에서 각각 해당 및 WRITE_CONTACTS 권한을 요청 READ_CONTACTS 해야 합니다. 또한 사용자 프로필을 읽고 편집하려면 애플리케이션에서 해당 및 WRITE_PROFILE 권한을 요청 READ_PROFILE 해야 합니다.

프로필 데이터 업데이트

이러한 사용 권한이 설정되면 애플리케이션은 일반 Android 기술을 사용하여 사용자 프로필의 데이터와 상호 작용할 수 있습니다. 예를 들어 프로필의 표시 이름을 업데이트하려면 아래와 같이 ContactsContract.Profile.ContentRawContactsUri 속성을 통해 검색된 내용으로 Uri ContentResolver.Update를 호출합니다.

var values = new ContentValues ();
values.Put (ContactsContract.Contacts.InterfaceConsts.DisplayName, "John Doe");

// Update the user profile with the name "John Doe":
ContentResolver.Update (ContactsContract.Profile.ContentRawContactsUri, values, null, null);

프로필 데이터 읽기

ContactsContact.Profile.ContentUri에 쿼리를 실행하면 프로필 데이터를 다시 읽습니다. 예를 들어 다음 코드는 사용자 프로필의 표시 이름을 읽습니다.

// Read the profile
var uri = ContactsContract.Profile.ContentUri;

// Setup the "projection" (column we want) for only the display name:
string[] projection = {
    ContactsContract.Contacts.InterfaceConsts.DisplayName };

// Use a CursorLoader to retrieve the data:
CursorLoader loader = new CursorLoader(this, uri, projection, null, null, null);
ICursor cursor = (ICursor)loader.LoadInBackground();
if (cursor != null)
{
    if (cursor.MoveToFirst ())
    {
        Console.WriteLine(cursor.GetString (cursor.GetColumnIndex (projection [0])));
    }
}

마지막으로 사용자 프로필로 이동하려면 작업을 ContactsContract.Profile.ContentUri 사용하여 의도를 ActionView 만든 다음 다음과 같이 메서드에 StartActivity 전달합니다.

var intent = new Intent (Intent.ActionView,
    ContactsContract.Profile.ContentUri);
StartActivity (intent);

위의 코드를 실행할 때 다음 스크린샷에 표시된 대로 사용자 프로필이 표시됩니다.

John Doe 사용자 프로필을 표시하는 프로필의 스크린샷

사용자 프로필로 작업하는 것은 Android의 다른 데이터와 상호 작용하는 것과 유사하며 추가 수준의 디바이스 개인 설정을 제공합니다.