Example of Content Provide - Expenditure Provider

import android.content.ContentProvider;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.Context;
import android.content.UriMatcher;
import android.database.Cursor;
import android.net.Uri;

 * Created by root on 18/8/17.

public class ExpenditureProvider extends ContentProvider {
    private static final String PROVIDER_NAME = "com.fusion.hisab.ExpenditureProvider";
    private static final Uri CONTENT_URI = Uri.parse("content://" + PROVIDER_NAME + "/expenditures");
    private static final int EXPENDITURES = 1;
    private static final int EXPENDITURE_ID = 2;
    private static final int EXPENDITURE_MONTHLY = 3;
    private static final int EXPENDITURE_YEARLY = 4;
    private static final UriMatcher uriMatcher = getUriMatcher();
    private static UriMatcher getUriMatcher() {
        UriMatcher uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
        uriMatcher.addURI(PROVIDER_NAME, "expenditures", EXPENDITURES);
        uriMatcher.addURI(PROVIDER_NAME, "expenditures/#", EXPENDITURE_ID);
        uriMatcher.addURI(PROVIDER_NAME, "expenditures/month/", EXPENDITURE_MONTHLY);
        uriMatcher.addURI(PROVIDER_NAME, "expenditures/year/", EXPENDITURE_YEARLY);
        return uriMatcher;
    private  DatabaseHelper databaseHelper=null;
    public String getType(Uri uri) {
        switch (uriMatcher.match(uri)) {
            case EXPENDITURES:
                return "vnd.android.cursor.dir/vnd.com.fusion.hisab.provider.expenditures";
            case EXPENDITURE_ID:
                return "vnd.android.cursor.item/vnd.com.fusion.hisab.provider.expenditures";
            case EXPENDITURE_MONTHLY:
                return "vnd.android.cursor.dir/vnd.com.fusion.hisab.provider.expenditures";
            case EXPENDITURE_YEARLY:
                return "vnd.android.cursor.dir/vnd.com.fusion.hisab.provider.expenditures";
        return "";

    public boolean onCreate() {
        Context context=getContext();
        databaseHelper=new DatabaseHelper(context);
        return true;
    public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
        String id=null;
        if(uriMatcher.match(uri) == EXPENDITURE_ID) {
            //Query is for one single image. Get the ID from the URI.
            id = uri.getPathSegments().get(1);
        return databaseHelper.getExpenditures(id, projection, selection, selectionArgs, sortOrder);

    public Uri insert(Uri uri, ContentValues values) {
        try {
            long id = databaseHelper.addNewExpenditure(values);
            Uri returnUri = ContentUris.withAppendedId(CONTENT_URI, id);
            return returnUri;
        } catch(Exception e) {
            return null;

    public int delete(Uri uri, String selection, String[] selectionArgs) {
        return databaseHelper.deleteRecords(selection,selectionArgs);

    public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
        return databaseHelper.updateServerStatus(values,selection,selectionArgs);

share on whatapp


Private Policy   Terms of Service   10813