1.创建类继承ContentProvider类,并实现增、删、改、查功能。
1 public static final String AUTHORITY = "com.diysoul.listshowdemo.providers.PersonContentProvider"; 2 3 public static final int INSERT_CODE = 0; 4 5 public static final int DELETE_CODE = 1; 6 7 public static final int UPDATE_CODE = 2; 8 9 public static final int QUARY_CODE = 3; 10 11 public static final int QUARY_BY_ID_CODE = 4; 12 13 public static final String INSERT = "insert"; 14 15 public static final String UPDATE = "update"; 16 17 public static final String DELETE = "delete"; 18 19 public static final String QUERY = "query"; 20 21 /* 22 * 根据_id查询 #为通配符,表示一个数字 *为通配符,表示任意一个字符串 23 */ 24 public static final String QUERY_BY_ID = "query/#"; 25 26 private PersonSqliteHelper mySqlHelper; 27 28 static UriMatcher uriMatcher; 29 30 static { 31 uriMatcher = new UriMatcher(UriMatcher.NO_MATCH); 32 uriMatcher.addURI(AUTHORITY, INSERT, INSERT_CODE); 33 uriMatcher.addURI(AUTHORITY, UPDATE, UPDATE_CODE); 34 uriMatcher.addURI(AUTHORITY, DELETE, DELETE_CODE); 35 uriMatcher.addURI(AUTHORITY, QUERY, QUARY_CODE); 36 uriMatcher.addURI(AUTHORITY, QUERY_BY_ID, QUARY_BY_ID_CODE); 37 } 38 39 @Override 40 public boolean onCreate() { 41 mySqlHelper = new PersonSqliteHelper(this.getContext()); 42 return false; 43 } 44 45 @Override 46 public Cursor query(Uri uri, String[] projection, String selection, 47 String[] selectionArgs, String sortOrder) { 48 switch (uriMatcher.match(uri)) { 49 case QUARY_CODE: { 50 SQLiteDatabase db = mySqlHelper.getReadableDatabase(); 51 if (db.isOpen()) { 52 Cursor cursor = db.query("persons", projection, selection, 53 selectionArgs, null, null, sortOrder); 54 return cursor; 55 } 56 } 57 break; 58 case QUARY_BY_ID_CODE: { 59 SQLiteDatabase db = mySqlHelper.getReadableDatabase(); 60 if (db.isOpen()) { 61 long id = ContentUris.parseId(uri); 62 Cursor cursor = db.query("persons", projection, "_id = ?", 63 new String[] { String.valueOf(id) }, null, null, 64 sortOrder); 65 return cursor; 66 } 67 } 68 break; 69 default: 70 throw new IllegalArgumentException("cannot match uri:" + uri); 71 } 72 return null; 73 } 74 75 @Override 76 public String getType(Uri uri) { 77 switch(uriMatcher.match(uri)) { 78 case QUARY_BY_ID_CODE: 79 return "vnd.android.cursor.item/persons"; 80 case QUARY_CODE: 81 return "vnd.android.cursor.dir/persons"; 82 default: 83 break; 84 } 85 return null; 86 } 87 88 @Override 89 public Uri insert(Uri uri, ContentValues values) { 90 switch (uriMatcher.match(uri)) { 91 case INSERT_CODE: 92 SQLiteDatabase db = mySqlHelper.getReadableDatabase(); 93 if (db.isOpen()) { 94 long id = db.insert(PersonSqliteHelper.PERSON_TABLE, null, 95 values); 96 db.close(); 97 return ContentUris.withAppendedId(uri, id); 98 } 99 break; 100 default: 101 throw new IllegalArgumentException("cannot match uri:" + uri); 102 } 103 return null; 104 } 105 106 @Override 107 public int delete(Uri uri, String selection, String[] selectionArgs) { 108 switch (uriMatcher.match(uri)) { 109 case DELETE_CODE: 110 SQLiteDatabase db = mySqlHelper.getReadableDatabase(); 111 if (db.isOpen()) { 112 int count = db.delete(PersonSqliteHelper.PERSON_TABLE, 113 selection, selectionArgs); 114 db.close(); 115 return count; 116 } 117 break; 118 default: 119 throw new IllegalArgumentException("cannot match uri:" + uri); 120 } 121 return 0; 122 } 123 124 @Override 125 public int update(Uri uri, ContentValues values, String selection, 126 String[] selectionArgs) { 127 switch (uriMatcher.match(uri)) { 128 case UPDATE_CODE: 129 SQLiteDatabase db = mySqlHelper.getReadableDatabase(); 130 if (db.isOpen()) { 131 int count = db.update(PersonSqliteHelper.PERSON_TABLE, values, 132 selection, selectionArgs); 133 db.close(); 134 return count; 135 } 136 break; 137 default: 138 throw new IllegalArgumentException("cannot match uri:" + uri); 139 } 140 return 0; 141 }
2.在AndroidManifest.xml中进行配置,在application根节点下增加
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
<provider android:name="com.diysoul.listshowdemo.providers.PersonContentProvider" android:authorities="com.diysoul.listshowdemo.providers.PersonContentProvider" android:exported="true" > </provider>
3.创建应用程序访问功能
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 public void runQuery() { 2 Log.v(TAG, "[runQuery]in"); 3 Uri uri = Uri.parse("content://com.diysoul.listshowdemo.providers.PersonContentProvider/query"); 4 ContentResolver resolver = this.getContext().getContentResolver(); 5 Log.v(TAG, "[runQuery]call query"); 6 Cursor cursor = resolver.query(uri, new String[]{"_id", "name", "tel"}, null, null, null); 7 Log.v(TAG, "[runQuery]cursor=" + cursor); 8 if(cursor != null && cursor.getCount() > 0) { 9 while(cursor.moveToNext()) { 10 int id = cursor.getInt(0); 11 String name = cursor.getString(1); 12 String tel = cursor.getString(2); 13 Log.v(TAG, "id=" + id + ",name=" + name + ",tel=" + tel); 14 } 15 } 16 }
4.监听数据改变
1 ContentObserver smsObserver = new ContentObserver(msmHandler) { 2 3 @Override 4 public void onChange(boolean selfChange) { 5 Log.v(TAG, "[onChange]in selfChange=" + selfChange); 6 } 7 8 @Override 9 public void onChange(boolean selfChange, Uri uri) { 10 Log.v(TAG, "[onChange]in selfChange=" + selfChange + ", uri=" + uri); 11 //重新从查询等操作,以改变保存的数据 12 readSms(); 13 //通知事件适配器数据改变,此时会自动刷新适配器关联 的ListView 14 smsListAdapter.notifyDataSetChanged(); 15 } 16 }; 17 getContentResolver().registerContentObserver(Uri.parse(SMS_CONTENT), true, smsObserver);