package com.china.anonymous.contentproviderdemoone;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
/**
* Created by Anonymous on 2016/1/31.
*/
public class MyHelper extends SQLiteOpenHelper {
private static final String fileName = "demo.db";
private static final int version = 1;
public MyHelper(Context context) {
super(context, fileName,null, version);
}
@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {
String sql = "create table student(_ID integer primary key autoincrement,name text not null)";
sqLiteDatabase.execSQL(sql);
}
@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
}
}
To create a new class which extends ContentProvider
package com.china.anonymous.contentproviderdemoone;
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;
import android.support.annotation.Nullable;
public class MyContentProvider extends ContentProvider {
private static UriMatcher matcher = new UriMatcher(UriMatcher.NO_MATCH);
private static final int multiple_Items = 1;
private static final int single_item = 2;
private MyHelper helper = null;
static {
matcher.addURI("com.china.anonymous.contentproviderdemoone.MyContentProvider", "student", multiple_Items);
matcher.addURI("com.china.anonymous.contentproviderdemoone.MyContentProvider", "student/#", single_item);
}
@Override
public boolean onCreate() {
helper = new MyHelper(getContext());
return false;
}
@Nullable
@Override
public Cursor query(Uri uri, String[] strings, String s, String[] strings1, String s1) {
//返回一个游标
Cursor cursor = null;
SQLiteDatabase db = helper.getReadableDatabase();
int flag = matcher.match(uri);
switch (flag) {
case single_item:
long id = ContentUris.parseId(uri);
String where_id = "_ID=" + id;
cursor = db.query("student", strings, where_id, strings1, null, null, null);
case multiple_Items:
cursor = db.query("student", strings, s, strings1, null, null, null);
}
return cursor;
}
@Nullable
@Override
public String getType(Uri uri) {
int flag = matcher.match(uri);
switch (flag) {
//查询单条记录,前面那一句固定写法
case single_item:
return "vnd.android.cursor.item/item";
//查询整个表
case multiple_Items:
return "vnd.android.cursor.dir/items";
default:
throw new UnsupportedOperationException("not supported operation:" + uri);
}
}
@Nullable
@Override
public Uri insert(Uri uri, ContentValues contentValues) {
//返回一个标志,判断是多条记录(从表中操作)还是单挑记录(操作指定记录)
int flag = matcher.match(uri);
SQLiteDatabase db = helper.getWritableDatabase();
Uri newUri = null;
//因为创建的表是自动添加的,所以就不用写单条记录的
switch (flag) {
case multiple_Items:
long id = db.insert("student", null, contentValues);
//把新添加的id加入表中
newUri = ContentUris.withAppendedId(uri, id);
}
//返回一个新的uri
return newUri;
}
@Override
public int delete(Uri uri, String s, String[] strings) {
//获取指定要删除的从uri中获取id,
int flag = matcher.match(uri);
SQLiteDatabase db = helper.getWritableDatabase();
int deleteItem = 0;
switch (flag) {
case single_item:
//获取单挑的uri中的id
long id = ContentUris.parseId(uri);
String where_id = "_ID=" + id;
deleteItem = db.delete("student", where_id, strings);
case multiple_Items:
deleteItem = db.delete("student", s, strings);
}
return deleteItem;
}
@Override
public int update(Uri uri, ContentValues contentValues, String s, String[] strings) {
//update主要是获取指定要修改的的uri的id,返回修改后的id
int flag = matcher.match(uri);
SQLiteDatabase db = helper.getWritableDatabase();
int upDataItem = 0;
switch (flag) {
case single_item:
long id = ContentUris.parseId(uri);
String where_id = "_ID=" + id;
upDataItem = db.update("student", contentValues, where_id, strings);
case multiple_Items:
upDataItem = db.update("student", contentValues, s, strings);
}
return upDataItem;
}
}
在AndroidManifest.xml中添加provider
<provider
android:authorities="com.china.anonymous.contentproviderdemoone.MyContentProvider"
android:name=".MyContentProvider"/>
在主程序中应用,用contentResolver
package com.china.anonymous.contentproviderdemoone;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ContentResolver resolver = getContentResolver();
Uri uri = Uri.parse("content://com.china.anonymous.contentproviderdemoone.MyContentProvider/student");
ContentValues value = new ContentValues();
value.put("name", "change");
resolver.update(uri, value, "_ID=6", null);
resolver.delete(uri, "_ID=9", null);
}
}