Profil Pengguna
Android telah mendukung enumerasi kontak dengan penyedia ContactsContract sejak API Level 5. Misalnya, mencantumkan kontak semampu menggunakan kelas ContactContracts.Contacts seperti yang ditunjukkan dalam contoh kode berikut:
// 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());
}
}
Dimulai dengan Android 4 (API Level 14), kelas ContactsContact.Profile tersedia melalui ContactsContract
penyedia. menyediakan ContactsContact.Profile
akses ke profil pribadi untuk pemilik perangkat, yang mencakup data kontak seperti nama pemilik perangkat dan nomor telepon.
Izin yang Diperlukan
Untuk membaca dan menulis data kontak, aplikasi harus meminta READ_CONTACTS
izin dan WRITE_CONTACTS
masing-masing.
Selain itu, untuk membaca dan mengedit profil pengguna, aplikasi harus meminta READ_PROFILE
izin dan WRITE_PROFILE
.
Memperbarui Data Profil
Setelah izin ini ditetapkan, aplikasi dapat menggunakan teknik Android normal untuk berinteraksi dengan data profil pengguna. Misalnya, untuk memperbarui nama tampilan profil, panggil ContentResolver.Update dengan yang Uri
diambil melalui properti ContactsContract.Profile.ContentRawContactsUri , seperti yang ditunjukkan di bawah ini:
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);
Membaca Data Profil
Mengeluarkan kueri ke ContactsContact.Profile.ContentUri membaca kembali data profil. Misalnya, kode berikut akan membaca nama tampilan profil pengguna:
// 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])));
}
}
Menavigasi ke Profil Pengguna
Terakhir, untuk menavigasi ke profil pengguna, buat Niat dengan ActionView
tindakan dan ContactsContract.Profile.ContentUri
kemudian teruskan ke StartActivity
metode seperti ini:
var intent = new Intent (Intent.ActionView,
ContactsContract.Profile.ContentUri);
StartActivity (intent);
Saat menjalankan kode di atas, profil pengguna ditampilkan seperti yang diilustrasikan dalam cuplikan layar berikut:
Bekerja dengan profil pengguna mirip dengan berinteraksi dengan data lain di Android, dan menawarkan tingkat personalisasi perangkat tambahan.