1、ContentProvider:为存储和获取数据提供统一的接口。可以在不同的应用程序之间共享数据。Android已经为常见的一些数据提供了默认的ContentProvider
2、ContentProvider提供的方法
query:查询
insert:插入
update:更新
delete:删除
getType:得到数据类型
onCreate:创建数据时调用的回调函数
3、自定义一个ContentProvider,来实现内部原理
步骤:
1、定义一个CONTENT_URI常量(里面的字符串必须是唯一)
public static String SS = "testprovader.user";
public static Uri CONTENT_USER_URI =Uri.parse("content://"+SS);
如果有子表,URI为:
Public static final Uri CONTENT_URI = Uri.parse("content://testprovader.user/users");
2、定义一个类,继承ContentProvider
Public class MyContentProvider extends ContentProvider
3、实现ContentProvider的所有方法(query、insert、update、delete、getType、onCreate)
package com.WangWeiDa.cp;
<p>public class TestContentProvider extends ContentProvider { public static String SS = "testprovader.user"; public static Uri CONTENT_USER_URI =Uri.parse("content://"+SS); private static final String TABLE_NAME="User"; SqlDataBaseHelper content; SQLiteDatabase db; public boolean onCreate() { content = new SqlDataBaseHelper(getContext()); db = content.getReadableDatabase(); return false; }</p><p> public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) { SQLiteQueryBuilder qb = new SQLiteQueryBuilder(); SQLiteDatabase db = content.getReadableDatabase(); qb.setTables(TABLE_NAME); Cursor c = qb.query(db, projection, selection, null, null, null, sortOrder); c.setNotificationUri(getContext().getContentResolver(), uri); return c; }</p><p> public String getType(Uri uri) {</p><p> return null; }</p><p> public Uri insert(Uri uri, ContentValues values) { db.insert(MySQLPackaging.User.TABLE, null, values);</p><p> return null; }</p><p> public int delete(Uri uri, String selection, String[] selectionArgs) {</p><p> return 0; }</p><p> public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {</p><p> return 0; }</p><p> /** * 创建数据库和表,字段,属性 */ public class SqlDataBaseHelper extends SQLiteOpenHelper { public static final String DATA_BASE_NAME = "test_provider.db"; public static final int VERSION = 1;</p><p> public SqlDataBaseHelper(Context context) { super(context, DATA_BASE_NAME, null, VERSION);</p><p> }</p><p> public void onCreate(SQLiteDatabase db) { String sql = "create table " + MySQLPackaging.User.TABLE + "(" + MySQLPackaging.User._ID + " INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT," + MySQLPackaging.User.NAME + " TEXT NOT NULL," + MySQLPackaging.User.PASSWORD + " TEXT NOT NULL)"; db.execSQL(sql); }</p><p> public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {</p><p> }</p><p> } } XML里面也要对应:</p>
创建数据库和表,字段,属性,用的这个类定义:
<provider android:name="com.example.testone017.contentprovider.TestContentProvider" android:authorities="testprovader.user" />
public class MySQLPackaging { public static final class Student implements BaseColumns { public static String TABLE = "student";// 版本1 public static String NAME = "name";// 版本1 public static String NUMBER = "number";// 版本1 public static String SEX = "sex";// 版本1 public static String SCORE = "score";// 版本2升级加入字段 public static String ADDRESS = "address";// 版本3升级加入字段 }
调用者ContentResoler是通过一个Uri来找到相应的ContentProvider的来进行实际操作。但是实际上是用 ContentResolver去操作:
ContentResolver resolver = getContentResolver();
/** * 读取联系人 */ public void readResolver(ContentResolver resolver) { Cursor cur = resolver.query(Contacts.People.CONTENT_URI, null, null, null, null); while (cur.moveToNext()) { String name = cur.getString(cur .getColumnIndex(Contacts.People.NAME)); int n = cur.getInt(cur.getColumnIndex(Contacts.People.NOTES)); Log.i("11", "name :" + name + "号码 :" + n); } } /** * 添加联系人 */ public void insertRasolver(ContentResolver resolver) { ContentValues values = new ContentValues(); values.put(Contacts.People.NAME, "穿着拖鞋去上学"); values.put(Contacts.People.NOTES, 10086111); Uri uri = Contacts.People.CONTENT_URI; resolver.insert(uri, values); }
//查询: public final Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder); //新增 public final Uri insert(Uri url, ContentValues values) //更新 public final int update(Uri uri, ContentValues values, String where, String[] selectionArgs) //删除 public final int delete(Uri url, String where, String[] selectionArgs) 以上操作实际是通过Uri来匹配ContentProvider, 再由ContentProvider来进行具体操作的。 操作的参数和操作sqlite各函数的参数意义是一样的。