Content Providers & Broadcasts Interview Questions

ANDROID › Components

What is a ContentProvider, and when would you actually write one today?

What a strong answer covers: A ContentProvider exposes structured data to other apps through content:// URIs with query, insert, update and delete, with permissions. You'd write one to share data with other apps, to support FileProvider-style file sharing, for search suggestions, or for widgets. Libraries also use them for automatic startup initialisation (which is why App Startup exists). For in-app data, use Room directly.

What is a BroadcastReceiver, and what's the difference between registering one in the manifest and in code?

What a strong answer covers: A BroadcastReceiver responds to system or app broadcasts. Manifest-registered receivers can wake the app but, since Android 8, most implicit broadcasts can't be received that way (exceptions include BOOT_COMPLETED and locale changes). Context-registered receivers only work while registered, must be unregistered to avoid leaks, and since Android 14 must state RECEIVER_EXPORTED or RECEIVER_NOT_EXPORTED.

How do you share a file with another app securely?

What a strong answer covers: Use FileProvider to create a content:// URI for the file, put it in the intent with FLAG_GRANT_READ_URI_PERMISSION, and the receiving app gets temporary access to that one file. Exposing file:// URIs throws FileUriExposedException since Android 7, and making files world-readable is insecure.

onReceive runs on the main thread with a short time limit. How do you do longer work in response to a broadcast?

What a strong answer covers: Return quickly from onReceive: enqueue a WorkManager job for anything significant, or use goAsync() to get a PendingResult and finish within about 10 seconds on a background thread. Don't start long coroutines in GlobalScope from onReceive: the process can be killed after onReceive returns because nothing is keeping it alive.

What security issues come with exported components like providers and receivers, and how do you lock them down?

What a strong answer covers: Exported components can be called by any app: a provider can leak data or be vulnerable to SQL injection through selection arguments or path traversal through file paths; a receiver can be sent spoofed broadcasts. Mitigations: android:exported=false unless needed, signature-level permissions for trusted apps, validate all input, use parameterised queries, avoid grantUriPermissions on whole directories, and use explicit intents for in-app broadcasts.

Back to Content Providers & Broadcasts