Sobes.tech
Middle

Tell me about receivers and content providers in Android.

sobes.tech AI

Answer from AI

Broadcast Receivers respond to system-wide broadcast messages, such as low battery notifications, network connection, or taking photos. They do not have a visual interface and perform background tasks.

There are two main types:

  • Statically registered: Declared in AndroidManifest.xml. The system launches the application upon receiving the corresponding intent, even if it is not running.
  • Dynamically registered: Registered in code using Context.registerReceiver(). They work only as long as the component (e.g., activity) exists.

Each receiver implements the method onReceive(Context context, Intent intent), where the incoming intent is processed.

Content Providers manage shared access to structured data. They provide a standardized CRUD (Create, Read, Update, Delete) interface for interacting with data stored in various sources (SQLite databases, files, network data, etc.).

Main methods of a content provider:

  • onCreate(): Initialization of the provider.
  • query(): Retrieve data from the source.
  • insert(): Add new data.
  • update(): Update existing data.
  • delete(): Delete data.
  • getType(): Returns the MIME type of data for a given URI.

Access to a content provider is done through ContentResolver. URI (Unified Resource Identifier) is used to identify a specific data set.

Example of using ContentResolver to query data:

// Uri for contacts table
Uri contactsUri = ContactsContract.Contacts.CONTENT_URI;

String[] projection = {
    ContactsContract.Contacts._ID,
    ContactsContract.Contacts.DISPLAY_NAME
};

// Query data
Cursor cursor = getContentResolver().query(
    contactsUri,
    projection,
    null, // selection
    null, // selectionArgs
    null  // sortOrder
);

if (cursor != null) {
    while (cursor.moveToNext()) {
        long contactId = cursor.getLong(cursor.getColumnIndexOrThrow(ContactsContract.Contacts._ID));
        String displayName = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME));
        // Process data
    }
    cursor.close();
}

Example of registering a static receiver in AndroidManifest.xml:

<receiver
    android:name=".MyBroadcastReceiver"
    android:enabled="true"
    android:exported="true">
    <intent-filter>
        <action android:name="android.intent.action.BATTERY_LOW" />
    </intent-filter>
</receiver>

Example implementation of a receiver:

public class MyBroadcastReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent.getAction() != null && intent.getAction().equals(Intent.ACTION_BATTERY_LOW)) {
            // Perform actions on low battery
        }
    }
}

Key differences:

Characteristic Broadcast Receivers Content Providers
Purpose React to broadcast events Manage data access
Interface onReceive() CRUD methods via ContentResolver
Visual display None None
Access Receive intents from the system Provide data upon request