Kişi depolama alanlarını yönetme

Uygulamalar, kullanıcıların kişi oluşturmasına ve depolamasına izin verebilir. Bu kişiler genellikle iki konuma kaydedilebilir:

  1. Bulut hesabı: Kişilerin senkronizasyonuna ve yedeklenmesine izin vermek için kişileri bir bulut hizmetiyle (ör. Google Cloud) ilişkili bir hesaba kaydedin.
  2. Yerel hesap: Kişiler, cihazda yerel olarak depolanabilir.

Kullanıcılar, tercih ettikleri depolama konumunu cihaz ayarlarından belirleyebilir. Bu tercih edilen konum, varsayılan hesap olarak bilinir ve kişi oluşturulurken kullanılır. Uygulamalar bu tercihe uymalıdır. Bu dokümanda, bulut hesapları ve yerel hesaplar 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ını ifade eder.

Varsayılan hesabı alma

Yeni kişiler için varsayılan hesabı belirlemek üzere ContactsContract.RawContacts.DefaultAccount

ContactsContrast.RawContacts.DefaultAccount.DefaultAccountAndState nesnesini almak için getDefaultAccountForNewContacts() yöntemini çağırın. Bu nesne, varsayılan hesap ayarı hakkında bilgi 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 ayarlanmışsa bu hesabın kategorisini (bulut, yerel veya SIM) belirtir.
  • 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 olmak üzere diğer eyaletler için boş 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 genellikle kişi oluştururken açıkça bir hesap belirtmesi gerekmez. Sistem, yeni kişiyi otomatik olarak varsayılan hesaba kaydeder. Hesap belirtmeden kişi oluşturmak için aşağıdaki adımları uygulayın.

ContentProviderOperation nesneden oluşan yeni bir ArrayList oluşturun. Bu liste, ham kişiyi ve ilişkili verilerini ekleme işlemlerini içerir.

Kotlin

val ops = ArrayList<ContentProviderOperation>()

Java

ArrayList<ContentProviderOperation> ops =
        new ArrayList<ContentProviderOperation>();

Ham kişiyi eklemek için yeni bir ContentProviderOperation oluşturun. Bir hesap belirtmediğiniz için ACCOUNT_TYPE ve ACCOUNT_NAME değerlerini 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());

İletişim 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

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:

ContentProviderOperation nesneden oluşan yeni bir ArrayList 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 için işlem listesine başka ContentProviderOperation nesneleri ekleyin ve kişiyi oluşturmak için toplu işlemi yürütün.

Yerel hesapta kişi 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:

ContentProviderOperation nesneden oluşan yeni bir ArrayList 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 hesabın hesap bilgilerini belirtmek için ContactsContract.RawContacts.getLocalAccountName() ve ContactsContract.RawContacts.getLocalAccountType() 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 diğer ContentProviderOperation nesnelerini ekleyin ve kişiyi oluşturmak için toplu işlemleri yürütün.