Sobes.tech
Junior — Middle

Can you explain the working principle of Content Provider in Android and how it interacts with other application components?

sobes.tech AI

Answer from AI

Content Provider in Android is a component that provides a mechanism for data sharing between applications. It encapsulates access to data (such as a SQLite database, files, or other sources) and offers a standard interface for reading and writing data via URIs.

Working principle:

  • Data is accessible through URIs that identify specific resources.
  • Methods used for data access include: query(), insert(), update(), delete(), getType().
  • Other components (or applications) access the Content Provider through ContentResolver, which acts as an intermediary.

Interaction with other components:

  • An application wishing to access data uses ContentResolver and the URI of the Content Provider.
  • The Content Provider processes requests and returns data in the form of a Cursor or other formats.
  • This allows safe and standardized data exchange between applications.

Example of using ContentResolver to read contacts:

val cursor = contentResolver.query(
    ContactsContract.Contacts.CONTENT_URI,
    null, null, null, null
)
if (cursor != null) {
    while (cursor.moveToNext()) {
        val name = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME))
        println("Contact name: $name")
    }
    cursor.close()
}
Can you explain the working principle of Content… - sobes.tech