才会被初始化。
query(*)*从内容提供器中查询数据,uri参数来确定查询那张表,查询的结果存
返回值返回。
getType()根据传入的uri来返回相应的MIME类型,一个内容URI对应的
最后接上vnd.< authority>.< path>
对于
package com.xiaoxu.administrator.save2doc.contentprovider;
import android.content.ContentProvider;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.net.Uri;
import android.support.annotation.Nullable;
import com.xiaoxu.administrator.save2doc.DataHelper;
/** * Created by Administrator on 2016/4/19. */
public class CountryProvider extends ContentProvider {
SQLiteOpenHelper sqLiteOpenHelper;
static UriMatcher uriMatcher;
private static final String AUTHORITY = "com.xiaoxu.administrator.save2doc.contentprovider";
//定义静态常量,表示要访问Country表中的全部数据,或者是单条数据。
private static final int COUNRY_DIR = 0;
private static final int COUNRY_ITEM = 1;
static {
//将UriMatcher初始化,将希望匹配的集中uri类型添加进去,参数1 权限,参数2 要访问表的path, 参数3 访问的类型。
uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
uriMatcher.addURI(AUTHORITY, "Country", COUNRY_DIR);
uriMatcher.addURI(AUTHORITY, "Country/#", COUNRY_ITEM);
}
@Override
public boolean onCreate() {
//创建DatabaseHelpter的实例,返回true表示初始化成功,这时数据库已经完成了创建和升级。
sqLiteOpenHelper = new DataHelper(getContext(), "CountryInfo", null, 1);
return true;
}
@Nullable
@Override
public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
SQLiteDatabase db = sqLiteOpenHelper.getReadableDatabase();
Cursor cursor = null;
//获取传入的uri 的要访问的类型。
switch (uriMatcher.match(uri)) {
case COUNRY_DIR:
cursor = db.query("Country", projection, selection, selectionArgs, null, null, sortOrder);
break;
case COUNRY_ITEM:
String countryId = uri.getPathSegments().get(1);
cursor = db.query("Country", projection, "id = ?", new String[]{countryId}, null, null, sortOrder);
break;
}
return cursor;
}
@Nullable
@Override
public String getType(Uri uri) {
switch (uriMatcher.match(uri)){
case COUNRY_DIR:
return "vnd.android.cursor.dir/vnd.com.xiaoxu.administrator.save2doc.contentprovider.Country" ;
case COUNRY_ITEM:
return "vnd.android.cursor.item/vnd.com.xiaoxu.administrator.save2doc.contentprovider.Country" ;
}
return null;
}
@Nullable
@Override
public Uri insert(Uri uri, ContentValues values) {
SQLiteDatabase db = sqLiteOpenHelper.getWritableDatabase();
Uri uriReturn = null;
switch (uriMatcher.match(uri)) {
case COUNRY_DIR:
case COUNRY_ITEM:
//得到新增数据的ID
Long newCountryId = db.insert("Country", null, values);
//拼接字符串将新的uri对象返回。
uriReturn = Uri.parse("content://" + AUTHORITY + "/Country/" + newCountryId);
break;
}
return uriReturn;
}
@Override
public int delete(Uri uri, String selection, String[] selectionArgs) {
SQLiteDatabase db = sqLiteOpenHelper.getWritableDatabase();
int deleteLine = 0;
switch (uriMatcher.match(uri)) {
case COUNRY_DIR:
deleteLine = db.delete("Country", selection, selectionArgs);
break;
case COUNRY_ITEM:
//uri.getPathSegments(),它会将权限后面的部分以/分隔,并把分割后的结果存放到字符串表中,第0位 是路径,第1位 是id
String ItemId = uri.getPathSegments().get(1);
deleteLine = db.delete("Country", "id = ?", new String[]{ItemId});
break;
}
return deleteLine;
}
@Override
public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
SQLiteDatabase db = sqLiteOpenHelper.getWritableDatabase();
int newId = 0;
switch (uriMatcher.match(uri)) {
case COUNRY_DIR:
newId = db.update("Country", values, selection, selectionArgs);
break;
case COUNRY_ITEM:
//uri.getPathSegments(),它会将权限后面的部分以/分隔,并把分割后的结果存放到字符串表中,第0位 是路径,第1位 是要查询的行数 id
String updateId = uri.getPathSegments().get(1);
newId = db.update("Country", values, "id = ?", new String[]{updateId});
break;
}
return newId;
}
}
<provider android:authorities="com.xiaoxu.administrator.save2doc.contentprovider" android:name=".contentprovider.CountryProvider" android:exported="true"/>
android:exported 可以被其他程序访问
接下来新建一个项目B来获取项目A中的表中的数据。
Uri uri = Uri.parse("content://com.xiaoxu.administrator.save2doc.contentprovider/Country");
Cursor cursor = MainActivity.this.getContentResolver().query(uri,null,null,null,null);
StringBuilder builder = new StringBuilder();
if(cursor != null){
while (cursor.moveToNext()){
String countryName = cursor.getString(cursor.getColumnIndex("name"));
builder.append(countryName +"\n");
}
Uri itemUri = Uri.parse("content://com.xiaoxu.administrator.save2doc.contentprovider/Country/4");
Cursor itemCursor = MainActivity.this.getContentResolver().query(itemUri,null,null,null,null);
StringBuilder areaBuild = new StringBuilder();
if(itemCursor != null){
while (itemCursor.moveToNext()){
areaBuild.append(itemCursor.getInt(itemCursor.getColumnIndex("area")) +"\n");
}
queryAreaTv.setText(areaBuild.toString());
}
结果 获取到了列名为 name列的数据。