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.