Android: Broadcast receiver and content provider

Broadcast Receiver

A broadcast receiver is a component that responds to a specific broadcast and does not have a user interface.

Broadcasts include time zone changes, low battery notifications, and language setting changes. If a broadcast is received in connection with such an event, the broadcast receiver processes it and enters standby mode again.

Broadcast receivers only guarantee work within 10 seconds, so if you need long time operation, you have to implement it as a separate thread or service.

If you want your Java class to act as a broadcast receiver, you can inherit the BroadcastReceiver class and create the desired behavior in the onReceive() method.

public class MyReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
    }
}

Content provider

The Content Provider provides a standardized interface for sharing data between applications. The actual data provided by the content provider may reside in a file system or database.

The content provider can define read, write, modify, and delete functions.

public class MyProvider extends ContentProvider {
    public int delete(Uri uri, String selection, String[] selectionArgs) {
        return 0;
    }
    public String getType(Uri uri) {
        return null;
    }
    public Uri insert(Uri uri, ContentValues values) {
        return null;
    }
    public boolean onCreate() {
        return false;
    }
    public Cursor query(Uri uri, String[] projection, String selection,
            String[] selectionArgs, String sortOrder) {
        return null;
    }
    public int update(Uri uri, ContentValues values, String selection,
            String[] selectionArgs) {
        return 0;
    }
}

An already implemented content provider can be used by invoking a method implemented in another application.

Share This Post

댓글 남기기

이메일은 공개되지 않습니다. 필수 입력창은 * 로 표시되어 있습니다

Latest Post