Component Content Provider creation and registration, contentprovider

Source: Internet
Author: User

Component Content Provider creation and registration, contentprovider

1. Create a provider

public class DemoProvider extends ContentProvider {    public static final int DEMO_DIR=0;    public static final int DEMO_ITEM=1;    public static final String AUTHORITIES="com.demo.provider";    private static UriMatcher sUriMatcher;    private DemoHelper mDemoHelper;    static {        sUriMatcher=new UriMatcher(UriMatcher.NO_MATCH);        sUriMatcher.addURI(AUTHORITIES,"xx",DEMO_DIR);        sUriMatcher.addURI(AUTHORITIES,"xx/#",DEMO_ITEM);    }    @Override    public int delete(Uri uri, String selection, String[] selectionArgs) {        SQLiteDatabase db=mDemoHelper.getWritableDatabase();        int deletedRows=0;        switch (sUriMatcher.match(uri)){            case DEMO_DIR:                deletedRows=db.delete("name",selection,selectionArgs);                break;            case DEMO_ITEM:                String id=uri.getPathSegments().get(1);                deletedRows=db.delete("name","id=?",new String[]{id});                break;        }        return deletedRows;    }    @Override    public String getType(Uri uri) {       switch (sUriMatcher.match(uri)){           case DEMO_DIR:              return "vnd.android.cursor.dir/vnd.com.demo.provider.name";           case DEMO_ITEM:               String id=uri.getPathSegments().get(1);              return "vnd.android.cursor.item/vnd.com.demo.provider.name";       }       return null;    }    @Override    public Uri insert(Uri uri, ContentValues values) {        SQLiteDatabase db=mDemoHelper.getWritableDatabase();        Uri returnUri=null;        switch (sUriMatcher.match(uri)){            case DEMO_DIR:            case DEMO_ITEM:               long newId=db.insert("tablename",null,values);                returnUri=Uri.parse("content://"+AUTHORITIES+"/tablename/"+newId);                break;        }        return returnUri;    }    @Override    public boolean onCreate() {        mDemoHelper=new DemoHelper(getContext(),"xxxx.db",null,1);        return true;    }    @Override    public Cursor query(Uri uri, String[] projection, String selection,String[] selectionArgs, String sortOrder)    {        SQLiteDatabase db=mDemoHelper.getWritableDatabase();        Cursor cursor=null;        switch (sUriMatcher.match(uri)){            case DEMO_DIR:                cursor=db.query("tablename",projection,selection,selectionArgs,null,null,sortOrder);                break;            case DEMO_ITEM:                String id=uri.getPathSegments().get(1);                cursor=db.query("tablename",projection,"id=?",new String[]{id},null,null,sortOrder);                break;        }        return cursor;    }    @Override    public int update(Uri uri, ContentValues values, String selection,                      String[] selectionArgs) {        SQLiteDatabase db=mDemoHelper.getWritableDatabase();      int updateRows=0;        switch (sUriMatcher.match(uri)){            case DEMO_DIR:                updateRows=db.update("name",values,selection,selectionArgs);                break;            case DEMO_ITEM:                String id=uri.getPathSegments().get(1);                updateRows=db.update("name",values,"id=?",new String[]{id});                break;        }        return updateRows;    }}

2. Register provider

<provider android:name=".DemoProvider" android:authorities="com.demo.provider" android:enabled="true" android:exported="true">       </provider>

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.