--- /dev/null
+package com.enqualcomm.support.provider;
+
+import android.content.ContentProvider;
+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.NonNull;
+import android.support.annotation.Nullable;
+
+public class FZDMessageProvider extends ContentProvider {
+
+ //添加规则
+ private static final String PATH = "message";
+ private static final String AUTHORITY = "com.ic.fzd.we-talk";
+ private static final String MESSAGES = "content://" + AUTHORITY + "/" + PATH;
+
+ private FZDDatabaseHelper mDatabase;
+
+
+ private static final int MATH_CODE_TABLE = 1;
+
+ private static UriMatcher sUriMatcher;
+
+ static {
+ sUriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
+ sUriMatcher.addURI(AUTHORITY, PATH, MATH_CODE_TABLE);
+ }
+
+
+ @Override
+ public boolean onCreate() {
+ mDatabase = new FZDDatabaseHelper(getContext());
+ return false;
+ }
+
+ @Nullable
+ @Override
+ public Cursor query(@NonNull Uri uri,
+ @Nullable String[] projection,
+ @Nullable String selection,
+ @Nullable String[] selectionArgs,
+ @Nullable String sortOrder) {
+ int match = sUriMatcher.match(uri);
+ if (match == MATH_CODE_TABLE) {
+ //处理查询
+ SQLiteDatabase readableDatabase = mDatabase.getReadableDatabase();
+ return readableDatabase.query(FZDDatabaseHelper.tMessage, projection,
+ selection, selectionArgs, sortOrder, null, null);
+ }
+ return null;
+ }
+
+ @Nullable
+ @Override
+ public String getType(@NonNull Uri uri) {
+ return null;
+ }
+
+ @Nullable
+ @Override
+ public Uri insert(@NonNull Uri uri,
+ @Nullable ContentValues values) {
+ int match = sUriMatcher.match(uri);
+ if (match == MATH_CODE_TABLE) {
+ SQLiteDatabase writableDatabase = mDatabase.getWritableDatabase();
+ //处理插入
+ long insert = writableDatabase.insert(FZDDatabaseHelper.tMessage, null, values);
+ writableDatabase.close();
+ return Uri.parse(AUTHORITY + "/" + PATH + "/" + insert);
+ }
+ return null;
+ }
+
+ @Override
+ public int delete(@NonNull Uri uri,
+ @Nullable String selection,
+ @Nullable String[] selectionArgs) {
+ int match = sUriMatcher.match(uri);
+ if (match == MATH_CODE_TABLE) {
+ //删除数据
+ SQLiteDatabase writableDatabase = mDatabase.getWritableDatabase();
+ int delete = writableDatabase.delete(FZDDatabaseHelper.tMessage, selection, selectionArgs);
+ writableDatabase.close();
+ return delete;
+ }
+ return 0;
+ }
+
+ @Override
+ public int update(@NonNull Uri uri, @Nullable ContentValues values, @Nullable String selection, @Nullable String[] selectionArgs) {
+ int match = sUriMatcher.match(uri);
+ if (match == MATH_CODE_TABLE) {
+ //更新数据
+ SQLiteDatabase writableDatabase = mDatabase.getWritableDatabase();
+ int update = writableDatabase.update(FZDDatabaseHelper.tMessage, values, selection, selectionArgs);
+ writableDatabase.close();
+ return update;
+ }
+ return 0;
+ }
+}