Apps können Nutzern das Erstellen und Speichern von Kontakten ermöglichen. Diese Kontakte können in der Regel an zwei Orten gespeichert werden:
- Cloud-Konto: Wenn Sie Kontakte in einem Konto speichern, das mit einem Cloud-Dienst (z. B. Google Cloud) verknüpft ist, können Sie Kontakte synchronisieren und sichern.
- Lokales Konto: Kontakte können lokal auf dem Gerät gespeichert werden.
Nutzer können ihren bevorzugten Speicherort in den Geräteeinstellungen festlegen. Dieser bevorzugte Ort wird als Standardkonto bezeichnet und beim Erstellen von Kontakten verwendet. Apps sollten diese Einstellung berücksichtigen. In diesem Dokument wird beschrieben, wie Sie mit verschiedenen Speicherorten für Kontakte arbeiten, einschließlich Cloud-Konten und lokaler Konten, und wie Sie Best Practices für die Verwaltung von Nutzereinstellungen implementieren. Das lokale Konto bezieht sich auf das Speichern von Kontakten direkt auf dem Gerät.
Standardkonto abrufen
Verwenden Sie ContactsContract.RawContacts.DefaultAccount
, um das Standardkonto für neue Kontakte festzulegen.
Rufen Sie getDefaultAccountForNewContacts()
auf, um das ContactsContrast.RawContacts.DefaultAccount.DefaultAccountAndState
-Objekt abzurufen. Dieses Objekt enthält Informationen zur Standardkontoeinstellung.
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()
);
Das DefaultAccountAndState
-Objekt enthält:
- Status: Gibt an, ob ein Standardkonto festgelegt ist und, falls ja, die Kategorie dieses Kontos (Cloud, lokal oder SIM).
- Konto: Enthält die spezifischen Kontodetails (Name und Typ), wenn der Status
DEFAULT_ACCOUNT_STATE_CLOUD or DEFAULT_ACCOUNT_STATE_SIM
ist. Für andere Bundesstaaten, einschließlichDEFAULT_ACCOUNT_STATE_LOCAL
, ist der Wert „null“.
Hier ein Beispiel für das Parsen des DefaultAccountAndState
-Objekts:
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;
}
Kontakte erstellen, ohne ein Konto anzugeben
Wenn das Standardkonto festgelegt ist, muss Ihre App beim Erstellen von Kontakten in der Regel kein Konto explizit angeben. Der neue Kontakt wird automatisch im Standardkonto gespeichert. So erstellen Sie einen Kontakt, ohne ein Konto anzugeben:
Erstellen Sie ein neues ArrayList
von ContentProviderOperation
-Objekten. Diese Liste enthält die Vorgänge zum Einfügen des Rohkontakts und der zugehörigen Daten.
Kotlin
val ops = ArrayList<ContentProviderOperation>()
Java
ArrayList<ContentProviderOperation> ops =
new ArrayList<ContentProviderOperation>();
Erstellen Sie ein neues ContentProviderOperation
, um den Rohkontakt einzufügen. Da Sie kein Konto angeben, müssen Sie ACCOUNT_TYPE
und ACCOUNT_NAME
nicht angeben.
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());
Fügen Sie der Liste der Vorgänge weitere ContentProviderOperation
-Objekte hinzu, um die Kontaktfelder (z. B. Name, Telefonnummer, E-Mail-Adresse) einzubeziehen. Führen Sie dann den Batchvorgang aus, um den Kontakt zu erstellen.
Kotlin
try {
getContentResolver().applyBatch(
ContactsContract.AUTHORITY, ops
)
} catch (e: Exception) {
// Handle exceptions
}
Java
try {
getContentResolver().applyBatch(
ContactsContract.AUTHORITY, ops
);
} catch (Exception e) {
// Handle exceptions
}
Kontakte in einem Cloud-Konto erstellen
Wenn Sie einen Kontakt in einem Cloud-Konto erstellen möchten, fügen Sie die Rohkontaktdaten in die Tabelle ContactsContract.RawContacts
ein und geben Sie das Cloud-Konto an. So gehts:
Erstellen Sie ein neues ArrayList
von ContentProviderOperation
-Objekten.
Kotlin
val ops = ArrayList<ContentProviderOperation>()
Java
ArrayList<ContentProviderOperation> ops =
new ArrayList<ContentProviderOperation>();
Erstellen Sie ein neues ContentProviderOperation
, um den Rohkontakt einzufügen. Verwenden Sie die Methode withValue()
, um den Kontotyp und den Kontonamen des ausgewählten Cloud-Kontos anzugeben.
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());
Fügen Sie der Vorgangsliste weitere ContentProviderOperation
-Objekte hinzu, um die Kontaktfelder einzubeziehen, und führen Sie den Batchvorgang aus, um den Kontakt zu erstellen.
Kontakte im lokalen Konto erstellen
Wenn Sie einen Kontakt im lokalen Konto erstellen möchten, fügen Sie der Tabelle ContactsContract.RawContacts
eine neue Rohkontaktdatenzeile hinzu und geben Sie die Kontoinformationen für das lokale Konto an:
Erstellen Sie ein neues ArrayList
von ContentProviderOperation
-Objekten.
Kotlin
val ops = ArrayList<ContentProviderOperation>()
Java
ArrayList<ContentProviderOperation> ops =
new ArrayList<ContentProviderOperation>();
Erstellen Sie ein neues ContentProviderOperation
, um den Rohkontakt einzufügen. Verwenden Sie ContactsContract.RawContacts.getLocalAccountName()
und ContactsContract.RawContacts.getLocalAccountType()
, um die Kontoinformationen für das lokale Konto anzugeben.
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());
Fügen Sie der Liste der Vorgänge weitere ContentProviderOperation
-Objekte hinzu, um die Kontaktfelder einzubeziehen, und führen Sie die Batchvorgänge aus, um den Kontakt zu erstellen.