1.新建一个内容提供者,在mainfest文件中注册
<provider android:name="com.example.person.PersonDBProvider"
android:authorities="com.example.person.personprovider"
android:exported="true" > </provider>
package com.example.person;
import android.content.ContentProvider;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
public class PersonDBProvider extends ContentProvider {
private static UriMatcher matcher = new UriMatcher(UriMatcher.NO_MATCH);
private static final int INSERT = 1;
private static final int DELETE = 2;
private static final int UPDATE = 3;
private static final int QUERY = 4;
private static final int QUERYONE = 5;
private PersonSQLiteOpenHelper helper;
//添加匹配器
static {
matcher.addURI("com.example.person.personprovider", "insert", INSERT);
matcher.addURI("com.example.person.personprovider", "update", UPDATE);
matcher.addURI("com.example.person.personprovider", "delete", DELETE);
matcher.addURI("com.example.person.personprovider", "query", QUERY);
matcher.addURI("com.example.person.personprovider", "query/#", QUERYONE);
}
//内容提供者创建时调用
@Override
public boolean onCreate() {
helper = new PersonSQLiteOpenHelper(getContext());
return false;
}
@Override
public Cursor query(Uri uri, String[] projection, String selection,
String[] selectionArgs, String sortOrder) {
if (matcher.match(uri) == QUERY) {
SQLiteDatabase db = helper.getReadableDatabase();
Cursor cursor = db.query("person", projection, selection,
selectionArgs, null, null, sortOrder);
return cursor;
} else if (matcher.match(uri) == QUERYONE) {
// 返回所对应的id
long id = ContentUris.parseId(uri);
SQLiteDatabase db = helper.getReadableDatabase();
Cursor cursor = db.query("person", projection, "id=?",
new String[] { id + "" }, null, null, sortOrder);
return cursor;
} else {
throw new IllegalArgumentException("路径不合法");
}
}
@Override
public String getType(Uri uri) {
if (matcher.match(uri) == QUERY) {
//返回多条数据
return "vnd.android.cursor.dir/person";
} else if (matcher.match(uri) == QUERYONE) {
//返回一条数据
return "vnd.android.cursor.item/person";
}
return null;
}
@Override
public Uri insert(Uri uri, ContentValues values) {
if (matcher.match(uri) == INSERT) {
SQLiteDatabase db = helper.getWritableDatabase();
db.insert("person", null, values);
} else {
throw new IllegalArgumentException("路径不合法");
}
return null;
}
@Override
public int delete(Uri uri, String selection, String[] selectionArgs) {
if (matcher.match(uri) == DELETE) {
SQLiteDatabase db = helper.getWritableDatabase();
db.delete("person", selection, selectionArgs);
} else {
throw new IllegalArgumentException("路径不合法");
}
return 0;
}
@Override
public int update(Uri uri, ContentValues values, String selection,
String[] selectionArgs) {
if (matcher.match(uri) == UPDATE) {
SQLiteDatabase db = helper.getWritableDatabase();
db.update("person", values, selection, selectionArgs);
} else {
throw new IllegalArgumentException("路径不合法");
}
return 0;
}
}
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.person"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.person.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<provider
android:name="com.example.person.PersonDBProvider"
android:authorities="com.example.person.personprovider"
android:exported="true" >
</provider>
</application>
</manifest>