Uygulamalar, kullanıcıların kişi oluşturup saklamasına izin verebilir. Bu kişiler genellikle iki yere kaydedilebilir:
- Cloud hesabı: Kişileri, senkronizasyon ve yedeklemeye izin vermek için bir bulut hizmetiyle (ör. Google Cloud) ilişkili bir hesaba kaydedin.
- Yerel hesap: Kişiler cihazda yerel olarak depolanabilir.
Kullanıcılar, cihaz ayarlarında tercih ettikleri depolama konumunu ayarlayabilir. Bu tercih edilen konum, varsayılan hesap olarak bilinir ve kişiler oluşturulurken kullanılır. Uygulamalar bu tercihe uymalıdır. Bu belgede, bulut hesapları ve yerel hesaplar da dahil olmak üzere farklı kişi depolama konumlarıyla nasıl çalışılacağı ve kullanıcı tercihlerini yönetmek için en iyi uygulamaların nasıl uygulanacağı açıklanmaktadır. Yerel hesap, kişilerin doğrudan cihazda depolanması anlamına gelir.
Varsayılan hesabı alma
Yeni kişiler için varsayılan hesabı belirlemek üzere ContactsContract.RawContacts.DefaultAccount
simgesini kullanın.
getDefaultAccountForNewContacts()
numaralı telefonu arayarak ContactsContrast.RawContacts.DefaultAccount.DefaultAccountAndState
nesnesini alın. Bu nesne, varsayılan hesap ayarıyla ilgili bilgiler içerir.
Kotlin
import ContactsContrast.RawContacts
import ContactsContrast.RawContacts.DefaultAccount
import ContactsContrast.RawContacts.DefaultAccount.DefaultAccountAndState
val defaultAccountAndState: DefaultAccountAndState =
DefaultAccount.getDefaultAccountForNewContacts(
getContentResolver()
)
Java
import ContactsContrast.RawContacts;
import ContactsContrast.RawContacts.DefaultAccount;
import ContactsContrast.RawContacts.DefaultAccount.DefaultAccountAndState;
DefaultAccountAndState defaultAccountAndState =
DefaultAccount.getDefaultAccountForNewContacts(
getContentResolver()
);
DefaultAccountAndState
nesnesi şunları içerir:
- Durum: Varsayılan bir hesabın ayarlanıp ayarlanmadığını ve ayarlandıysa bu hesabın kategorisini (bulut, yerel veya SIM) gösterir.
- Hesap: Durum
DEFAULT_ACCOUNT_STATE_CLOUD or DEFAULT_ACCOUNT_STATE_SIM
ise belirli hesap ayrıntılarını (ad ve tür) sağlar.DEFAULT_ACCOUNT_STATE_LOCAL
dahil diğer eyaletler için bu değer null olur.
DefaultAccountAndState
nesnesinin nasıl ayrıştırılacağına dair bir örneği aşağıda bulabilirsiniz:
Kotlin
// Retrieves the state of default account.
val defaultAccountState = defaultAccountAndState.state
var defaultAccountName: String? = null
var defaultAccountType: String? = null
when (defaultAccountState) {
// Default account is set to a cloud or a SIM account.
DefaultAccountState.DEFAULT_ACCOUNT_STATE_CLOUD,
DefaultAccountState.DEFAULT_ACCOUNT_STATE_SIM -> {
defaultAccountName = defaultAccountAndState.account?.name
defaultAccountType = defaultAccountAndState.account?.type
}
// Default account is set to the local account on the device.
DefaultAccountState.DEFAULT_ACCOUNT_STATE_LOCAL -> {
defaultAccountName = RawContacts.getLocalAccountType()
defaultAccountType = RawContacts.getLocalAccountName()
}
// Default account is not set.
DefaultAccountState.DEFAULT_ACCOUNT_STATE_NOT_SET -> {
}
}
Java
// Retrieves the state of default account.
var defaultAccountState = defaultAccountAndState.getState();
String defaultAccountName = null;
String defaultAccountType = null;
switch (defaultAccountState) {
// Default account is set to a cloud or a SIM account.
case DefaultAccountState.DEFAULT_ACCOUNT_STATE_CLOUD:
case DefaultAccountState.DEFAULT_ACCOUNT_STATE_SIM:
defaultAccountName = defaultAccountAndState.getAccount().name;
defaultAccountType = defaultAccountAndState.getAccount().type;
break;
// Default account is set to the local account on the device.
case DefaultAccountState.DEFAULT_ACCOUNT_STATE_LOCAL:
defaultAccountName = RawContacts.getLocalAccountType();
defaultAccountType = RawContacts.getLocalAccountName();
break;
// Default account is not set.
case DefaultAccountState.DEFAULT_ACCOUNT_STATE_NOT_SET:
break;
}
Hesap belirtmeden kişi oluşturma
Varsayılan hesap ayarlanmışsa uygulamanızın, kişiler oluştururken genellikle hesabı açıkça belirtmesi gerekmez. Sistem, yeni kişiyi varsayılan hesaba otomatik olarak kaydeder. Hesap belirtmeden nasıl kişi oluşturacağınız aşağıda açıklanmıştır.
ArrayList
/ContentProviderOperation
yeni nesne oluşturun. Bu listede, ham kişiyi ve ilişkili verilerini ekleme işlemleri yer alır.
Kotlin
val ops = ArrayList<ContentProviderOperation>()
Java
ArrayList<ContentProviderOperation> ops =
new ArrayList<ContentProviderOperation>();
Ham kişiyi eklemek için yeni bir ContentProviderOperation
oluşturun. Hesap belirtmediğiniz için ACCOUNT_TYPE
ve ACCOUNT_NAME
özelliklerini eklemeniz gerekmez.
Kotlin
val op = ContentProviderOperation.newInsert(
ContactsContract.RawContacts.CONTENT_URI
)
ops.add(op.build())
Java
ContentProviderOperation.Builder op =
ContentProviderOperation.newInsert(
ContactsContract.RawContacts.CONTENT_URI
);
ops.add(op.build());
Kişi alanlarını (ad, telefon numarası, e-posta gibi) eklemek için işlem listesine başka ContentProviderOperation
nesneleri ekleyin. Ardından, kişiyi oluşturmak için toplu işlemi yürütün.
Kotlin
try {
getContentResolver().applyBatch(
ContactsContract.AUTHORITY, ops
)
} catch (e: Exception) {
// Handle exceptions
}
Java
try {
getContentResolver().applyBatch(
ContactsContract.AUTHORITY, ops
);
} catch (Exception e) {
// Handle exceptions
}
Bulut hesabında kişi oluşturma
Bir bulut hesabında kişi oluşturmak için ham kişi satırını ContactsContract.RawContacts
tablosuna ekleyin ve bulut hesabını belirtin. Bunun için:
ArrayList
/ContentProviderOperation
yeni nesne oluşturun.
Kotlin
val ops = ArrayList<ContentProviderOperation>()
Java
ArrayList<ContentProviderOperation> ops =
new ArrayList<ContentProviderOperation>();
Ham kişiyi eklemek için yeni bir ContentProviderOperation
oluşturun. Seçilen bulut hesabının hesap türünü ve hesap adını belirtmek için withValue()
yöntemini kullanın.
Kotlin
val op = ContentProviderOperation.newInsert(
ContactsContract.RawContacts.CONTENT_URI
)
.withValue(
ContactsContract.RawContacts.ACCOUNT_TYPE,
selectedAccount.type
)
.withValue(
ContactsContract.RawContacts.ACCOUNT_NAME,
selectedAccount.name
)
ops.add(op.build())
Java
ContentProviderOperation.Builder op =
ContentProviderOperation.newInsert(
ContactsContract.RawContacts.CONTENT_URI
)
.withValue(
ContactsContract.RawContacts.ACCOUNT_TYPE,
selectedAccount.getType()
)
.withValue(
ContactsContract.RawContacts.ACCOUNT_NAME,
selectedAccount.getName()
);
ops.add(op.build());
Kişi alanlarını eklemek ve kişiyi oluşturmak için toplu işlemi yürütmek üzere işlem listesine başka ContentProviderOperation
nesneleri ekleyin.
Yerel hesapta kişiler oluşturma
Yerel hesapta kişi oluşturmak için ContactsContract.RawContacts
tablosuna yeni bir ham kişi satırı ekleyin ve yerel hesabın hesap bilgilerini belirtin:
ArrayList
/ContentProviderOperation
yeni nesne oluşturun.
Kotlin
val ops = ArrayList<ContentProviderOperation>()
Java
ArrayList<ContentProviderOperation> ops =
new ArrayList<ContentProviderOperation>();
Ham kişiyi eklemek için yeni bir ContentProviderOperation
oluşturun. Yerel hesap için hesap bilgilerini belirtmek üzere ContactsContract.RawContacts.getLocalAccountName()
ve ContactsContract.RawContacts.getLocalAccountType()
öğelerini kullanın.
Kotlin
val op = ContentProviderOperation.newInsert(
ContactsContract.RawContacts.CONTENT_URI
)
.withValue(
ContactsContract.RawContacts.ACCOUNT_TYPE,
ContactsContract.RawContacts.getLocalAccountType()
)
.withValue(
ContactsContract.RawContacts.ACCOUNT_NAME,
ContactsContract.RawContacts.getLocalAccountName()
)
ops.add(op.build())
Java
ContentProviderOperation.Builder op =
ContentProviderOperation.newInsert(
ContactsContract.RawContacts.CONTENT_URI
)
.withValue(
ContactsContract.RawContacts.ACCOUNT_TYPE,
ContactsContract.RawContacts.getLocalAccountType()
)
.withValue(
ContactsContract.RawContacts.ACCOUNT_NAME,
ContactsContract.RawContacts.getLocalAccountName()
);
ops.add(op.build());
Kişi alanlarını eklemek için işlem listesine başka ContentProviderOperation
nesneleri ekleyin ve kişiyi oluşturmak için toplu işlemleri yürütün.