Android系统通过继承一个ContentProvider类创建一个数据提供者,创建步骤分为以下三步:
1创建一个继承ContentProvider的java类,重载类中的6个函数:
public boolean onCreate():用于初始化底层数据集和建立数据连接等工作。当其他程序第一次访问ContentProvider时,该ContentProvider会被创建出来,并调用该方法。
public int delete(Uri uri,String selection,String[] selectionArgs): 删除selection数据集。
public Uri insert(Uri uri,ContentValues values): 添加数据集
public Cursor query(Uri uri, String[] projection, String selection,String[] selectionArgs, String sortOrder): 查询数据集
public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs): 更新数据集(其实就是修改复合条件的数据集)
public String getType(Uri uri)
public class EmployeeProvider extends ContentProvider { MyDBHelper dbhelper; private static UriMatcher matcher = new UriMatcher(UriMatcher.NO_MATCH); private static final int EMPLOYEES = 2; private static final int EMPLOYEE = 1; private static final String AUTHORITY="com.mialab.providers.employprovide"; static{ matcher.addURI(AUTHORITY, "employees", EMPLOYEES); matcher.addURI(AUTHORITY, "employee/#", EMPLOYEE); } //当其他程序访问第一次访问ContentProvider时,该ContentProvider会被创建出来, @Override //并调用该onCreate()方法,建立数据连接 public boolean onCreate() { dbhelper=new MyDBHelper(this.getContext(), "HR_DB.db3", 1); return true; }
2.实现UriMatcher
前面提到的操作都需要调用者ContentResolver的调用才能触发,Conten传入Uri参数来调用这些方法。为了确定该ContentProvider实际能处理的Uri,以及确定每个方法中Uri参数所操作的数据,Android系统提供了UriMatcher工具类。UriMatcher工具类提供以下两个方法:
void addUri(String authority,String path,int code):用于向UriMather对象注册Uri。其中authority 和path组成一个Uri,而code代表该Uri对应的标志码。path随便取字符串,只要与authority组合能和ContentResolver那边的Uri一样就行,重要的是authority要取注册的。
private static final String AUTHORITY="com.mialab.providers.employprovide";
private static UriMatcher matcher = new UriMatcher(UriMatcher.NO_MATCH);
static{ matcher.addURI(AUTHORITY, "employees", EMPLOYEES); //EMPLOYEES为常数
matcher.addURI(AUTHORITY, "employee/#", EMPLOYEE); //#为数字通配符,另外可以用*来匹配任意文本
}
UriMatcher就可以帮助我们方便的过滤(比如是哪个表,多少目标。就像是彼此之间的一个约定)(利用code), 然后进行下一步查询, 如果不用UriMatcher也可以,我们就需要手动过滤字符串,用起来有点麻烦,可维护性也不好。
int match(Uri uri):根据上面注册的Uri匹配获得对应的标志码code,如果匹配不到,则返回-1。
除此之外,Android还提供了一个ContentUris工具类,用于操作Uri字符串的工具类,提供以下两个方法:
withAppendedId(uri,id):用于为路径加上ID部分; id是数据编号,用来唯一确定数据集中的一条记录,用来匹配数据集中_id字段的值,在URI中是可以省略的。
public void onCreate(SQLiteDatabase db) { System.out.println("x"); String CreateTableSql="create table Employee(" +"_id integer primary key autoincrement," +"no varchar," +"name varchar," +"sex varchar)"; db.execSQL(CreateTableSql); }
Uri uri=Uri.parse("content://com.mialab.contentprovider/data");
Uri newUri=ContentUris.withAppendedId(uri,1); //此时newUri为content://com.mialab.contentprovider/data/1
parseId(uri): 用于从指定的Uri中解析出所包含的ID值。
long dataId=ContentUris.parseId(uri); //dataId的值为1.
3.注册ContentProvider
应用程序必须在配偶之文件AndroidManifest.xml中注册ContentProvider,方可使用。注册ContentProvider只需在<application/>元素下添加<provider/>子元素即可。
<application android:allowBackup="true" android:icon="@drawable/icon" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.mialab.employeeprovider.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=".EmployeeProvider" android:authorities="com.mialab.providers.employprovide" android:exported="true"/> </application>
其中name属性值为ContentProvider类,authorities为ContentProvider授权者。
ContentResolver
当一个程序通过ContentProvider暴露数据之后,其它应用程序即可通过ContentResolver对象共享这些数据。程序开发者只需知道Uri与数据集的数据格式即可对数据进行操作。获取共享数据的操作步骤如下:
1.获取ContentResolver对象
调用getContentResolver()方法来获取ContentResolver对象。
public class MainActivity extends Activity { ContentResolver resolver; resolver=getContentResolver();
2操作数据
与ContentProvider类中数据操作(增、删、改、查)方法对应,ContentResolver对象也有4个方法,也是insert()、delete()、update()、query(),这四个方法的参数列表与ContentProvider中对应的方法一致。调用任何一个方法即可触发Uri对应的ContentProvider中的方法。