Advanced Android Content Provider and File Access Techniques

Monitoring Content Changes with ContentObserver

The ContentObserver class provides a mechanism to track data modifications within a ContentProvider. By registering an observer with a specific URI, your application receives callbacks whenever the underlying dataset is updated.


public class SmsWatcher extends ContentObserver {
    private final Context context;

    public SmsWatcher(Context context) {
        super(new Handler(Looper.getMainLooper()));
        this.context = context;
    }

    @Override
    public void onChange(boolean selfChange, @Nullable Uri uri) {
        if (uri == null || uri.toString().startsWith("content://sms/raw")) return;

        try (Cursor cursor = context.getContentResolver().query(
                uri, new String[]{"address", "body"}, null, null, "date DESC")) {
            if (cursor != null && cursor.moveToFirst()) {
                String sender = cursor.getString(cursor.getColumnIndexOrThrow("address"));
                String content = cursor.getString(cursor.getColumnIndexOrThrow("body"));
                Log.i("SmsMonitor", "New SMS from " + sender + ": " + content);
            }
        }
    }
}

Sending Multimedia Messages (MMS)

To send an MMS, you initiate an Intent with ACTION_SEND. When attaching media, it is critical to use the appropriate MIME type and grant read URI permissions to the target application.


private void dispatchMms(String recipient, String subject, String body, Uri imageUri) {
    Intent mmsIntent = new Intent(Intent.ACTION_SEND);
    mmsIntent.setType("image/*");
    mmsIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
    mmsIntent.putExtra("address", recipient);
    mmsIntent.putExtra("subject", subject);
    mmsIntent.putExtra("sms_body", body);
    mmsIntent.putExtra(Intent.EXTRA_STREAM, imageUri);
    startActivity(mmsIntent);
}

Secure File Access with FileProvider

Starting with Android 7.0 (API 24), direct access to file:// URIs is restricted. FileProvider facilitates secure sharing of files by generating a content:// URI, which allows temporary access permissions to the receiving application.


public Uri getSecureUri(Context context, File file) {
    return FileProvider.getUriForFile(
            context, 
            context.getPackageName() + ".provider", 
            file
    );
}

Programmatic APK Installation

Installing a application from a file requires the REQUEST_INSTALL_PACKAGES permission. For modern Android versions (Android 11+), you may also need to request broad storage managemant permissions if acecssing files outside of scoped directories.


private void installPackage(File apkFile) {
    Uri uri = FileProvider.getUriForFile(this, getPackageName() + ".provider", apkFile);
    Intent intent = new Intent(Intent.ACTION_VIEW);
    intent.setDataAndType(uri, "application/vnd.android.package-archive");
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_GRANT_READ_URI_PERMISSION);
    startActivity(intent);
}

Tags: Android contentprovider FileProvider MMS ContentObserver

Posted on Sun, 09 Aug 2026 16:21:28 +0000 by l_evans