功能内容介绍:三个应用,有一个包含ContentProvider的应用(MainApp),该应用在PersonProvider类中的insert方法中包含
getContext().getContentResolver().notifyChange(insertUri, null);
即可监听,A应用通过ContentResolver类往MainApp中添加一条记录,B应用可以通过ContentResolver的query方法查找到插入的那条数据
MainApp应用的PersonProvider.java
package com.example.test_sqlite;
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 com.example.test_sqlite.service.SQLHelper;
public class PersonProvider extends ContentProvider{
private SQLHelper sqlHelper = null;
private static final UriMatcher MATCHER = new UriMatcher(UriMatcher.NO_MATCH); // NO_MATCH值为-1
private static final int PERSONS = 1;
private static final int PERSON = 2;
private final String TABLE_NAME_PERSON = "person";
static{
MATCHER.addURI("com.lipeng.providers.personprovider", "person", PERSONS);
MATCHER.addURI("com.lipeng.providers.personprovider", "person/#", PERSON);
}
@Override
public boolean onCreate() {
sqlHelper = new SQLHelper(getContext());
return true;
}
@Override
public Uri insert(Uri uri, ContentValues values) {
SQLiteDatabase db = sqlHelper.getWritableDatabase();
switch (MATCHER.match(uri)) {
case PERSONS:
long rowid = db.insert(TABLE_NAME_PERSON, "name", values); // 主键值
Uri insertUri = ContentUris.withAppendedId(uri, rowid);
getContext().getContentResolver().notifyChange(insertUri, null); // 发出数据变化通知
return insertUri;
default:
throw new IllegalArgumentException("this is Unknow Uri:" + uri);
}
}
@Override
public int delete(Uri uri, String selection, String[] selectionArgs) {
SQLiteDatabase db = sqlHelper.getWritableDatabase();
int num = 0;
switch (MATCHER.match(uri)) {
case PERSONS:
num = db.delete(TABLE_NAME_PERSON, selection, selectionArgs);
break;
case PERSON:
long rowid = ContentUris.parseId(uri);
String where = "personid=" + rowid;
if(selection != null && !"".equals(selection.trim())){
where += " and " + selection;
}
num = db.delete(TABLE_NAME_PERSON, where, selectionArgs);
break;
default:
throw new IllegalArgumentException("this is Unknow Uri:" + uri);
}
return num;
}
@Override
public int update(Uri uri, ContentValues values, String selection,
String[] selectionArgs) {
SQLiteDatabase db = sqlHelper.getWritableDatabase();
switch (MATCHER.match(uri)) {
case PERSONS:
return db.update(TABLE_NAME_PERSON, values, selection, selectionArgs);
case PERSON:
long rowid = ContentUris.parseId(uri);
String where = "personid=" + rowid;
if(selection != null && !"".equals(selection.trim())){
where += " and " + selection;
}
return db.update(TABLE_NAME_PERSON, values, where, selectionArgs);
default:
throw new IllegalArgumentException("this is Unknow Uri:" + uri);
}
}
/**
* projection 要查询的字段
*/
@Override
public Cursor query(Uri uri, String[] projection, String selection,
String[] selectionArgs, String sortOrder) {
// 约定:/person表示查询表的所有数据;/person/10表示查询id为10的数据
SQLiteDatabase db = sqlHelper.getReadableDatabase();
switch (MATCHER.match(uri)) {
case PERSONS:
return db.query(TABLE_NAME_PERSON, projection, selection, selectionArgs, null, null, sortOrder);
case PERSON:
long rowid = ContentUris.parseId(uri);
String where = "personid=" + rowid;
if(selection != null && !"".equals(selection.trim())){
where += " and " + selection;
}
return db.query(TABLE_NAME_PERSON, null, where, selectionArgs, null, null, sortOrder);
default:
throw new IllegalArgumentException("this is Unknow Uri:" + uri);
}
}
@Override
public String getType(Uri uri) {
switch (MATCHER.match(uri)) {
case PERSONS:
return "vnd.android.cursor.dir/person"; // 多条记录
case PERSON:
return "vnd.android.cursor.item/person"; // 单条记录
default:
throw new IllegalArgumentException("this is Unknow Uri:" + uri);
}
}
}
A应用:MainActivity.java
package com.example.aapp;
import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.view.Menu;
import android.view.View;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
/**
* 模板是public void onclick(View view){},返回值必须为void,参数为View
* */
public void insert(View view){
String uriStr = "content://com.lipeng.providers.personprovider/person";
Uri uri = Uri.parse(uriStr);
ContentResolver resolver = getContentResolver();
ContentValues values = new ContentValues();
values.put("name", "Aapp");
values.put("phone", "132323232323232");
values.put("amount", "800000000");
resolver.insert(uri, values);
}
}
B应用:MainActivity.java
package com.example.test;
import android.app.Activity;
import android.database.ContentObserver;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.Menu;
public class MainActivity extends Activity {
private final static String TAG = "MainActivity";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Uri uri = Uri
.parse("content://com.lipeng.providers.personprovider/person");
this.getContentResolver().registerContentObserver(uri, true,
new PersonContentObserver(new Handler()));
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
private class PersonContentObserver extends ContentObserver {
public PersonContentObserver(Handler handler) {
super(handler);
}
@Override
public void onChange(boolean selfChange, Uri uri) {
Cursor cursor = getContentResolver().query(uri, null, null, null,
"personid asc limit 1");
if (cursor.moveToFirst()) {
String name = cursor.getString(cursor.getColumnIndex("name"));
String phone = cursor.getString(cursor.getColumnIndex("phone"));
int amount = cursor.getInt(cursor.getColumnIndex("amount"));
Log.i(TAG, "name=" + name + ", phone=" + phone + ", amount=" + amount);
}
}
}
}