您的位置:首页 > 移动开发 > Android开发

android下内容提供者

2015-07-02 10:01 387 查看
首先在清单文件中定义

<provider
android:name="com.example.sqllitedemp.provider.contentprovider"  提供者名称
android:authorities="com.example.contentprovider.contentprovider"
android:exported="true"
android:readPermission="aa.bb.cc.read" //别人要想使用 读取 必需声明这个权限
android:writePermission="aa.bb.cc.write" >//写权限
</provider>
</pre><pre name="code" class="java">   <permission android:name="aa.bb.cc.read" >//这也是在清单文件里先声明这两个权限
    </permission>
    <permission android:name="aa.bb.cc.write" >
    </permission>


声明内容提供者 类要继承ContentProvider

private static final int PERSON_INSERT_CODE = 0;
private static final int PERSON_UPDATE_CODE = 1;
private static final int PERSON_DETELE_CODE = 2;
private static final int PERSON_QUERYALL_CODE = 3;
private static final int PERSON_QUERYITEM_CODE = 4;
private static UriMatcher urimatcher;
private static SqlLiteOperhelper Operhelper;
private static String authority = "com.example.contentprovider.contentprovider";
static {
urimatcher = new UriMatcher(UriMatcher.NO_MATCH);
// 添加uri(分机号)
// content://com.example.contentprovider.contentprovider/person/insert
urimatcher.addURI(authority, "person/insert", PERSON_INSERT_CODE);
// content://com.example.contentprovider.contentprovider/person/update
urimatcher.addURI(authority, "person/update", PERSON_UPDATE_CODE);
// content://com.example.contentprovider.contentprovider/person/detele
urimatcher.addURI(authority, "person/detele", PERSON_DETELE_CODE);
// content://com.example.contentprovider.contentprovider/person/queryAll
urimatcher.addURI(authority, "person/queryAll", PERSON_QUERYALL_CODE);

// content://com.example.contentprovider.contentprovider/person/#
urimatcher.addURI(authority, "person/#", PERSON_QUERYITEM_CODE);
}

@Override
public boolean onCreate() {
Operhelper = new SqlLiteOperhelper(getContext());//获取操作sqlite数据库帮助类
return true;
}

@Override
public Cursor query(Uri uri, String[] projection, String selection,
String[] selectionArgs, String sortOrder) {
SQLiteDatabase db = Operhelper.getReadableDatabase();
switch (urimatcher.match(uri)) {
case PERSON_QUERYALL_CODE:// 查询全部
if (db.isOpen()) {
Cursor cu = db.query("person", projection, selection,
selectionArgs, null, null, sortOrder);//
return cu;
// db.close();返回cursor游标结果集市不能关闭数据库
}
break;
case PERSON_QUERYITEM_CODE:// 查询单挑数据 url末尾有一个id
if (db.isOpen()) {
long id = ContentUris.parseId(uri);
return db.query("person", projection, "id=?", new String[] { id
+ "" }, null, null, null);//
}
break;
default:
throw new IllegalArgumentException("uri不匹配" + uri);
}

return null;
}

@Override
public String getType(Uri uri) {
// TODO Auto-generated method stub
return null;
}

@Override
public Uri insert(Uri uri, ContentValues values) {
SQLiteDatabase db = Operhelper.getWritableDatabase();
switch (urimatcher.match(uri)) {
case PERSON_INSERT_CODE:
if (db.isOpen()) {
long id = db.insert("person", null, values);// 返回新增数据的id

// 通知内容观察者 内容改变
getContext()
.getContentResolver()
.notifyChange(
Uri.parse("content://com.example.contentprovider.contentprovider"),
null);
db.close();
return ContentUris.withAppendedId(uri, id);//通知内容监视者 返回增加的id
}
break;
default:
throw new IllegalArgumentException("uri不匹配" + uri);
}
return null;

}

@Override
public int delete(Uri uri, String selection, String[] selectionArgs) {
SQLiteDatabase db = Operhelper.getWritableDatabase();
switch (urimatcher.match(uri)) {
case PERSON_DETELE_CODE:
if (db.isOpen()) {
int id = db.delete("person", selection, selectionArgs);//
// 通知内容观察者 内容改变
getContext()
.getContentResolver()
.notifyChange(
Uri.parse("content://com.example.contentprovider.contentprovider"),
null);
db.close();
return id;
}
break;
default:
throw new IllegalArgumentException("uri不匹配" + uri);
}
return 0;
}

@Override
public int update(Uri uri, ContentValues values, String selection,
String[] selectionArgs) {
SQLiteDatabase db = Operhelper.getWritableDatabase();
switch (urimatcher.match(uri)) {
case PERSON_UPDATE_CODE:
if (db.isOpen()) {
int id = db.update("person", values, selection, selectionArgs);//
db.close();
return id;
}
break;
default:
throw new IllegalArgumentException("uri不匹配" + uri);
}

return 0;
}


其他程序使用内容提供者 首先要声明定义内容提供者时候哪两个权限
查询所有

public void testqueryall() {
Uri uri = Uri
.parse("content://com.example.contentprovider.contentprovider/person/queryAll");
ContentResolver resolver = getContext().getContentResolver();// 内容提供者访问对象
ContentValues values = new ContentValues();//在删除和增加的时候这个可以添加参数
Cursor cu = resolver.query(uri, new String[] { "id", "name", "age" },
null, null, null);
int id;
String name;
int age;
if (cu != null && cu.getColumnCount() > 0) {
while (cu.moveToNext()) {
id = cu.getInt(0);
name = cu.getString(1);
age = cu.getInt(2);
Log.i("操作", "id" + id + ",name" + name + ",age" + age);
}
cu.close();
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: