1.创建类继承ContentProvider类,并实现增、删、改、查功能。
public static final String AUTHORITY = "com.diysoul.listshowdemo.providers.PersonContentProvider"; public static final int INSERT_CODE = 0; public static final int DELETE_CODE = 1; public static final int UPDATE_CODE = 2; public static final int QUARY_CODE = 3; public static final int QUARY_BY_ID_CODE = 4; public static final String INSERT = "insert"; public static final String UPDATE = "update"; public static final String DELETE = "delete"; public static final String QUERY = "query"; /*
* 根据_id查询 #为通配符,表示一个数字 *为通配符,表示任意一个字符串
*/
public static final String QUERY_BY_ID = "query/#"; private PersonSqliteHelper mySqlHelper; static UriMatcher uriMatcher; static {
uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
uriMatcher.addURI(AUTHORITY, INSERT, INSERT_CODE);
uriMatcher.addURI(AUTHORITY, UPDATE, UPDATE_CODE);
uriMatcher.addURI(AUTHORITY, DELETE, DELETE_CODE);
uriMatcher.addURI(AUTHORITY, QUERY, QUARY_CODE);
uriMatcher.addURI(AUTHORITY, QUERY_BY_ID, QUARY_BY_ID_CODE);
} @Override
public boolean onCreate() {
mySqlHelper = new PersonSqliteHelper(this.getContext());
return false;
} @Override
public Cursor query(Uri uri, String[] projection, String selection,
String[] selectionArgs, String sortOrder) {
switch (uriMatcher.match(uri)) {
case QUARY_CODE: {
SQLiteDatabase db = mySqlHelper.getReadableDatabase();
if (db.isOpen()) {
Cursor cursor = db.query("persons", projection, selection,
selectionArgs, null, null, sortOrder);
return cursor;
}
}
break;
case QUARY_BY_ID_CODE: {
SQLiteDatabase db = mySqlHelper.getReadableDatabase();
if (db.isOpen()) {
long id = ContentUris.parseId(uri);
Cursor cursor = db.query("persons", projection, "_id = ?",
new String[] { String.valueOf(id) }, null, null,
sortOrder);
return cursor;
}
}
break;
default:
throw new IllegalArgumentException("cannot match uri:" + uri);
}
return null;
} @Override
public String getType(Uri uri) {
switch(uriMatcher.match(uri)) {
case QUARY_BY_ID_CODE:
return "vnd.android.cursor.item/persons";
case QUARY_CODE:
return "vnd.android.cursor.dir/persons";
default:
break;
}
return null;
} @Override
public Uri insert(Uri uri, ContentValues values) {
switch (uriMatcher.match(uri)) {
case INSERT_CODE:
SQLiteDatabase db = mySqlHelper.getReadableDatabase();
if (db.isOpen()) {
long id = db.insert(PersonSqliteHelper.PERSON_TABLE, null,
values);
db.close();
return ContentUris.withAppendedId(uri, id);
}
break;
default:
throw new IllegalArgumentException("cannot match uri:" + uri);
}
return null;
} @Override
public int delete(Uri uri, String selection, String[] selectionArgs) {
switch (uriMatcher.match(uri)) {
case DELETE_CODE:
SQLiteDatabase db = mySqlHelper.getReadableDatabase();
if (db.isOpen()) {
int count = db.delete(PersonSqliteHelper.PERSON_TABLE,
selection, selectionArgs);
db.close();
return count;
}
break;
default:
throw new IllegalArgumentException("cannot match uri:" + uri);
}
return 0;
} @Override
public int update(Uri uri, ContentValues values, String selection,
String[] selectionArgs) {
switch (uriMatcher.match(uri)) {
case UPDATE_CODE:
SQLiteDatabase db = mySqlHelper.getReadableDatabase();
if (db.isOpen()) {
int count = db.update(PersonSqliteHelper.PERSON_TABLE, values,
selection, selectionArgs);
db.close();
return count;
}
break;
default:
throw new IllegalArgumentException("cannot match uri:" + uri);
}
return 0;
}
2.在AndroidManifest.xml中进行配置,在application根节点下增加
<provider
android:name="com.diysoul.listshowdemo.providers.PersonContentProvider"
android:authorities="com.diysoul.listshowdemo.providers.PersonContentProvider"
android:exported="true" >
</provider>
3.创建应用程序访问功能
public void runQuery() {
Log.v(TAG, "[runQuery]in");
Uri uri = Uri.parse("content://com.diysoul.listshowdemo.providers.PersonContentProvider/query");
ContentResolver resolver = this.getContext().getContentResolver();
Log.v(TAG, "[runQuery]call query");
Cursor cursor = resolver.query(uri, new String[]{"_id", "name", "tel"}, null, null, null);
Log.v(TAG, "[runQuery]cursor=" + cursor);
if(cursor != null && cursor.getCount() > 0) {
while(cursor.moveToNext()) {
int id = cursor.getInt(0);
String name = cursor.getString(1);
String tel = cursor.getString(2);
Log.v(TAG, "id=" + id + ",name=" + name + ",tel=" + tel);
}
}
}
4.监听数据改变
ContentObserver smsObserver = new ContentObserver(msmHandler) { @Override
public void onChange(boolean selfChange) {
Log.v(TAG, "[onChange]in selfChange=" + selfChange);
} @Override
public void onChange(boolean selfChange, Uri uri) {
Log.v(TAG, "[onChange]in selfChange=" + selfChange + ", uri=" + uri);
//重新从查询等操作,以改变保存的数据
readSms();
//通知事件适配器数据改变,此时会自动刷新适配器关联 的ListView
smsListAdapter.notifyDataSetChanged();
}
};
getContentResolver().registerContentObserver(Uri.parse(SMS_CONTENT), true, smsObserver);