Content provider and provider

Source: Internet
Author: User

Content provider and provider
Content Provider writing

Content providers provide interfaces for third-party application calls to add, delete, modify, and query requests.

Step 1: Create a class to implement ContentProvider
Package com. miquan. demo; import android. content. contentProvider; import android. content. contentUris; import android. content. contentValues; import android. content. uriMatcher; import android. database. cursor; import android.net. uri; public class MyContentProvider extends ContentProvider {// defines a MATCHER private static final UriMatcher = new UriMatcher (UriMatcher. NO_MATCH); private static final int MATCH _ WORDS = 1; // match all WORDS private static final int MATCH_WORD = 2; // match a word static {MATCHER. addURI ("com. miquan. myprovider "," word ", MATCH_WORDS); // # represents a number, * represents all characters MATCHER. addURI ("com. miquan. myprovider "," word/# ", MATCH_WORD);} public MyContentProvider () {}@ Override public boolean onCreate () {return false ;}@ Override public String getType (Uri uri Uri) {// when you need to obtain the data type, implement this method switch (MATCHER. match (Uri) {case MATCH_WORDS: // you get a set. The first part is the fixed android return "vnd. android. cursor. dir/word "; case MATCH_WORD: // an item return" vnd. android. cursor. item/word "; default: {// mismatched throw new IllegalArgumentException (" Uri mismatch: "+ uri) ;}}@ Override public Uri insert (Uri uri, contentValues values) {switch (MATCHER. match (uri) {case MATCH_WORDS: String word = values. getAsString ("word"); // already exists After obtaining the data, save it. Return ContentUris. withAppendedId (uri, 1); default: {// mismatched throw new IllegalArgumentException ("Uri mismatch:" + uri) ;}}@ Override public int delete (Uri uri, string selection, String [] selectionArgs) {switch (MATCHER. match (uri) {case MATCH_WORDS: // delete all records return 0; case MATCH_WORD: // delete a record return 0; default: {// mismatched throw new IllegalArgumentException ("Uri mismatch:" + uri) ;}}@ Override public Cursor query (Uri uri, String [] projection, String selection, string [] selectionArgs, String sortOrder) {throw new UnsupportedOperationException ("Not yet implemented") ;}@ Override public int update (Uri uri Uri, ContentValues values, String selection, string [] selectionArgs) {throw new UnsupportedOperationException ("Not yet implemented ");}}
Step 2: register in the list.
<provider    android:name=".MyContentProvider"    android:authorities="com.miquan.myprovider"    android:enabled="true"    android:exported="true" ></provider>
Step 3: Call it in a third-party application.
Uri uri = Uri. parse ("content: // com. miquan. myprovider/word "); ContentResolver resolver = getContentResolver (); ContentValues values = new ContentValues (); values. put ("word", "No secret Captain"); resolver. insert (uri, values );

Close.

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.