Sobes.tech
Junior — Middle

Explain what a Content Provider is in the context of Android development and how it is used for data exchange between applications.

sobes.tech AI

Answer from AI

Content Provider in Android is a component that allows applications to share data with each other in a secure and standardized way. It encapsulates access to data (such as an SQLite database, files, or other sources) and provides an interface for CRUD operations (create, read, update, delete).

Main points:

  • Content Provider implements a URI that other applications can use to access data.
  • Access to Content Provider is done through ContentResolver.
  • It allows controlling access rights to data via the manifest and permissions.

Example of usage: Application A provides a contact list via Content Provider, and Application B can access it using ContentResolver and the corresponding URI.

Example code for querying data from Content Provider:

Uri uri = ContactsContract.Contacts.CONTENT_URI;
Cursor cursor = getContentResolver().query(uri, null, null, null, null);
if (cursor != null) {
    while (cursor.moveToNext()) {
        String name = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
        // data processing
    }
    cursor.close();
}