ניהול המיקומים לאחסון אנשי הקשר

אפליקציות יכולות לאפשר למשתמשים ליצור ולאחסן אנשי קשר. בדרך כלל אפשר לשמור את אנשי הקשר האלה בשני מיקומים:

  1. חשבון Cloud: שמירת אנשי הקשר בחשבון שמשויך לשירות ענן (כמו Google Cloud) כדי לאפשר סנכרון וגיבוי של אנשי הקשר.
  2. חשבון מקומי: אפשר לשמור את אנשי הקשר באופן מקומי במכשיר.

המשתמשים יכולים להגדיר את מיקום האחסון המועדף עליהם בהגדרות המכשיר. המיקום המועדף הזה נקרא חשבון ברירת המחדל, והוא משמש כאשר יוצרים אנשי קשר. האפליקציות צריכות לפעול בהתאם להעדפה הזו. במסמך הזה מוסבר איך לעבוד עם מיקומים שונים לאחסון אנשי קשר, כולל חשבונות בענן וחשבונות מקומיים, ואיך ליישם שיטות מומלצות לניהול ההעדפות של המשתמשים. החשבון המקומי מתייחס לאחסון אנשי הקשר ישירות במכשיר.

אחזור של חשבון ברירת המחדל

כדי לקבוע את חשבון ברירת המחדל לאנשי קשר חדשים, משתמשים ב-ContactsContract.RawContacts.DefaultAccount

קוראים לפונקציה getDefaultAccountForNewContacts() כדי לקבל את האובייקט ContactsContrast.RawContacts.DefaultAccount.DefaultAccountAndState. האובייקט הזה מכיל מידע על הגדרת ברירת המחדל של החשבון.

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 מכיל:

  • מצב: מציין אם הוגדר חשבון ברירת מחדל, ואם כן, את הקטגוריה של החשבון הזה (ענן, מקומי או SIM).
  • Account: פרטי החשבון הספציפיים (שם וסוג) אם המצב הוא DEFAULT_ACCOUNT_STATE_CLOUD or DEFAULT_ACCOUNT_STATE_SIM. הערך יהיה null במדינות אחרות, כולל DEFAULT_ACCOUNT_STATE_LOCAL.

דוגמה לניתוח של אובייקט DefaultAccountAndState:

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;
}

יצירת אנשי קשר בלי לציין חשבון

אם חשבון ברירת המחדל מוגדר, בדרך כלל אין צורך לציין חשבון באופן מפורש כשיוצרים אנשי קשר באפליקציה. המערכת שומרת את איש הקשר החדש בחשבון ברירת המחדל באופן אוטומטי. כך יוצרים איש קשר בלי לציין חשבון:

יוצרים ArrayList חדש של אובייקטים מסוג ContentProviderOperation. הרשימה הזו מכילה את הפעולות להוספת איש הקשר הגולמי והנתונים המשויכים אליו.

Kotlin

val ops = ArrayList<ContentProviderOperation>()

Java

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

יוצרים ContentProviderOperation חדש כדי להוסיף את איש הקשר הגולמי. מכיוון שלא מציינים חשבון, אין צורך לכלול את ACCOUNT_TYPE ו-ACCOUNT_NAME.

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());

מוסיפים אובייקטים אחרים מסוג ContentProviderOperation לרשימת האדמינים כדי לכלול את שדות איש הקשר (כמו שם, מספר טלפון, אימייל). לאחר מכן, מריצים את פעולת האצווה כדי ליצור את איש הקשר.

Kotlin

try {
    getContentResolver().applyBatch(
        ContactsContract.AUTHORITY, ops
    )
} catch (e: Exception) {
    // Handle exceptions
}

Java

try {
    getContentResolver().applyBatch(
        ContactsContract.AUTHORITY, ops
    );
} catch (Exception e) {
    // Handle exceptions
}

יצירת אנשי קשר בחשבון בענן

כדי ליצור איש קשר בחשבון בענן, מוסיפים את שורת איש הקשר הגולמי לטבלה ContactsContract.RawContacts ומציינים את חשבון הענן. לשם כך:

יוצרים ArrayList חדש של אובייקטים מסוג ContentProviderOperation.

Kotlin

val ops = ArrayList<ContentProviderOperation>()

Java

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

יוצרים ContentProviderOperation חדש כדי להוסיף את איש הקשר הגולמי. משתמשים בשיטה withValue() כדי לציין את סוג החשבון ואת שם החשבון של חשבון הענן שנבחר.

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());

מוסיפים אובייקטים אחרים מסוג ContentProviderOperation לרשימת הפעולות כדי לכלול את שדות איש הקשר ומפעילים את פעולת האצווה כדי ליצור את איש הקשר.

יצירת אנשי קשר בחשבון המקומי

כדי ליצור איש קשר בחשבון המקומי, מוסיפים שורה חדשה של איש קשר גולמי לטבלה ContactsContract.RawContacts ומציינים את פרטי החשבון של החשבון המקומי:

יוצרים ArrayList חדש של אובייקטים מסוג ContentProviderOperation.

Kotlin

val ops = ArrayList<ContentProviderOperation>()

Java

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

יוצרים ContentProviderOperation חדש כדי להוסיף את איש הקשר הגולמי. משתמשים ב-ContactsContract.RawContacts.getLocalAccountName() וב-ContactsContract.RawContacts.getLocalAccountType() כדי לציין את פרטי החשבון של החשבון המקומי.

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());

מוסיפים אובייקטים אחרים מסוג ContentProviderOperation לרשימת הפעולות כדי לכלול את שדות איש הקשר, ומפעילים את פעולות האצווה כדי ליצור את איש הקשר.