ユーザー プロファイル

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 は、デバイス所有者の個人プロファイルへのアクセスを提供します。これには、デバイス所有者の名前や電話番号などの連絡先データが含まれます。

必要なアクセス許可

連絡先データの読み取りと書き込みを行うには、アプリケーションが READ_CONTACTSWRITE_CONTACTS のアクセス許可をそれぞれ要求する必要があります。 さらに、ユーザー プロファイルの読み取りと編集を行うには、アプリケーションが READ_PROFILEWRITE_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])));
    }
}

最後に、ユーザー プロファイルに移動するには、ActionView アクションと ContactsContract.Profile.ContentUri を使用して Intent を作成し、次のように StartActivity メソッドにそれを渡します。

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

上記のコードを実行すると、次のスクリーンショットに示すようにユーザー プロファイルが表示されます。

Screenshot of profile displaying the John Doe user profile

ユーザー プロファイルの操作は Android 内の他のデータとやり取りするのと似ており、きめ細かいデバイスのパーソナル化が提供されます。