1.内容提供者(ContentProivder)的作用,
每个进程之间是不能相互访问的,但是如果需要在两个进程之间实现数据的共享,就可以使用内容提供者,所以简单的说内容提供者让我们可以在一个程序访问到另外一个程序的数据.
下面是如何使用内容提供者(ContentProvider)
1.首先在清单的文件中声明具体的代码如下
// android:exported="true"是否可以被其他的应用程序所访问
<provider
android:exported="true"
android:authorities="com.qianfeng.mycontentpridver"
android:name=".MyContentPrivder"/>
name一般的情况下为自己的类名
exported属性表示是否可以被其他的应用程序所访问
authorities 一般是自己的全称类名(加包名),以保证唯一性.
2.创建自己MyProvider 继承ContentProvider具体的代码如下:
public class MyContentPrivder extends ContentProvider {
private MyOperHelper helper;
private static final int QUERY = 1;
private static final int INSERT = 2;
private static final int UPDATE = 3;
private static final int DELETE = 4;
/**
* 给当前的程序使用,
* 当ContentProvider 被创建的时候调用的方法
*
* @return
*/
private static UriMatcher uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
static {
String authority = "com.qianfeng.mycontentpridver";
uriMatcher.addURI(authority, "query", QUERY);
uriMatcher.addURI(authority, "insert", INSERT);
uriMatcher.addURI(authority, "delete", DELETE);
uriMatcher.addURI(authority, "update", UPDATE);
}
@Override
public boolean onCreate() {
helper = new MyOperHelper(getContext());
return true;
}
@Override
public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
Cursor cursor = null;
if (uriMatcher.match(uri) == QUERY) {
SQLiteDatabase database = helper.getReadableDatabase();
cursor = database.query("person", projection, selection, selectionArgs, null, null, sortOrder);
}
return cursor;
}
@Override
public String getType(Uri uri) {
return null;
}
@Override
public Uri insert(Uri uri, ContentValues values) {
if (uriMatcher.match(uri) == INSERT) {
SQLiteDatabase database = helper.getReadableDatabase();
database.insert("person", null, values);
}
return null;
}
@Override
public int delete(Uri uri, String selection, String[] selectionArgs) {
int flag = 0;
if (uriMatcher.match(uri) == DELETE) {
SQLiteDatabase database = helper.getReadableDatabase();
flag = database.delete("person", selection, selectionArgs);
}
return flag;
}
@Override
public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
int flag = 0;
if (uriMatcher.match(uri) == UPDATE) {
SQLiteDatabase database = helper.getReadableDatabase();
flag = database.update("person", values, selection, selectionArgs);
}
return flag;
}
}
当然这里也要写自己定义的SQlite的帮助类,即,//这里的帮助类主要是为了创建数据库的时候,还有返回操作数据类(SQLIte
Database的对象使用
)
*/
public class MyOperHelper extends SQLiteOpenHelper {
private static String NAMESQLITE = "info.db";
private static int VERSION = 1;
public MyOperHelper(Context context) {
super(context, NAMESQLITE, null, VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
String sql = "create table person(_id integer primary key autoincrement ,name varchar(20),age integer(20))";
db.execSQL(sql);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
3. 在另外一个程序就可以通过暴露出来的URi来访问数据了,代码如下:
public class MainActivity extends AppCompatActivity {
private ListView lv;
private ContentResolver resolver;
//值得注意的是,这里的URi一定要和刚刚定义的一样
private String queryUri = "content://com.qianfeng.mycontentpridver/query";
private String insertUri = "content://com.qianfeng.mycontentpridver/insert";
private String deleteUri = "content://com.qianfeng.mycontentpridver/delete";
private String updateUri = "content://com.qianfeng.mycontentpridver/update";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lv = (ListView) findViewById(R.id.lv);
resolver = getContentResolver();
Cursor cursor = resolver.query(Uri.parse(queryUri), null, null, null, null);
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.item, cursor, new String[]{"name", "age"}, new int[]{R.id.tv_name, R.id.tv_age});
lv.setAdapter(adapter);
}
public void onclick(View view) {
switch (view.getId()) {
case R.id.bt_add:
addData();
break;
case R.id.bt_delete:
deleteData();
break;
case R.id.bt_update:
updateData();
break;
}
}
public void addData() {
for (int i = 0; i < 10; i++) {
ContentValues values = new ContentValues();
values.put("name","zhangsan"+i);
values.put("age","2"+i);
resolver.insert(Uri.parse(insertUri), values);
}
}
public void deleteData() {
int delete = resolver.delete(Uri.parse(deleteUri), "_id = ?", new String[]{"" + 1});
if (delete > 0) {
Toast.makeText(this, "删除成功", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "删除失败", Toast.LENGTH_SHORT).show();
}
}
public void updateData() {
ContentValues values = new ContentValues();
values.put("name", "lisi");
values.put("age", 100);
int update = resolver.update(Uri.parse(updateUri), values, "_id = ?", new String[]{"" + 2});
if (update > 0) {
Toast.makeText(this, "修改成功", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "修改失败", Toast.LENGTH_SHORT).show();
}
}
}