Sobes.tech
Junior — Middle

Kas saaksite selgitada Content Provider tööpõhimõtet Androidis ja kuidas see suhtleb teiste rakenduse komponentidega?

sobes.tech AI

Vastus AI-lt

Content Provider in Android is a component that provides a mechanism for data exchange 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 via URIs that identify specific resources.
  • Methods such as query(), insert(), update(), delete(), getType() are used to access data.
  • Other components (or applications) access the Content Provider through the ContentResolver, which acts as an intermediary.

Interaction with other components:

  • The application that wants to obtain data uses the 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 for secure 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()
}