谁有没有安卓4.4.2版本的mediapro proviter

android的五种存储方式分别是:
ContentProvider
大多数ContentProvider使用Android文件系统或者SQLite数据库来保持数据,但是也可以以任何方式来存储。
在清单文件中注册一个自定义ContentProviter
android:name=".content_provider.TestContentProvider"
android:authorities="com.lh.knowledge.contentprovider"
android:exported="true" /&
重写一个TestContentProvider类继承ContentProviter
public class TestContentProvider extends ContentProvider {
public static final String AUTHORITY = "com.lh.knowledge.contentprovider";
private static final int MATCH_ALL_CODE = 100;
private static final int MATCH_ONE_CODE = 101;
private static UriMatcher uriM
private Cursor cursor = null;
List&TestBean& testBeanList = new ArrayList&&();
public boolean onCreate() {
initData();
uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
* uriMatcher.addURI(authority, path, code); 其中
* authority:主机名(用于唯一标示一个ContentProvider,这个需要和清单文件中的authorities属性相同)
* path:路径路径(可以用来表示我们要操作的数据,路径的构建应根据业务而定)
* code:返回值(用于匹配uri的时候,作为匹配成功的返回值)
uriMatcher.addURI(AUTHORITY, "student", MATCH_ALL_CODE);
uriMatcher.addURI(AUTHORITY, "student/#", MATCH_ONE_CODE);
return false;
private void initData() {
TestBean testBean1 = new TestBean();
testBean1.setId(1);
testBean1.setName("aaa");
testBean1.setPrice(5.2);
testBean1.setSex(false);
testBeanList.add(testBean1);
TestBean testBean2 = new TestBean();
testBean1.setId(2);
testBean1.setName("bbb");
testBean1.setPrice(6.2);
testBean1.setSex(true);
testBeanList.add(testBean2);
TestBean testBean3 = new TestBean();
testBean1.setId(3);
testBean1.setName("ccc");
testBean1.setPrice(0.2);
testBean1.setSex(false);
testBeanList.add(testBean3);
* projection
* selection
* selectionArgs
* sortOrder
public Cursor query(@NonNull Uri uri, @Nullable String[] projection, @Nullable String selection, @Nullable String[] selectionArgs, @Nullable String sortOrder) {
return null;
public String getType(@NonNull Uri uri) {
return null;
public Uri insert(@NonNull Uri uri, @Nullable ContentValues values) {
int match = uriMatcher.match(uri);
if (match != MATCH_ALL_CODE) {
throw new IllegalArgumentException("Unkwon Uri:" + uri.toString());
int indexId = values.getAsInteger("indexId");
Uri insertUri = ContentUris.withAppendedId(uri, indexId);
if (indexId & 0) {
TestBean testBean = new TestBean();
testBean.setId(indexId);
testBean.setName("content provider userid " + indexId);
testBeanList.add(testBean);
return insertU
return null;
public int delete(@NonNull Uri uri, @Nullable String selection, @Nullable String[] selectionArgs) {
switch (uriMatcher.match(uri)) {
case MATCH_ALL_CODE:
int i = testBeanList.size();
testBeanList.clear();
case MATCH_ONE_CODE:
int postion = Integer.parseInt(selection);
testBeanList.remove(postion);
public int update(@NonNull Uri uri, @Nullable ContentValues values, @Nullable String selection, @Nullable String[] selectionArgs) {
int postion = Integer.parseInt(selection);
if (postion & testBeanList.size() - 1) return 0;
TestBean testBean = testBeanList.get(postion);
if (testBean == null) return 0;
String name = values.getAsString("name");
testBean.setName(name);
如何使用ContentProvider来存储数据
public class ContentProviderActivity extends Activity {
private ContentResolver contentR
private final Uri STUDENT_ALL_URI = Uri.parse("content://" + TestContentProvider.AUTHORITY + "/student");
private final Uri STUDENT_ONE_URI = Uri.parse("content://" + TestContentProvider.AUTHORITY + "/student/#");
private Handler handler = new Handler() {
public void handleMessage(Message msg) {
* projection
* selection
* selectionArgs
* sortOrder
Cursor cursorAll = contentResolver.query(STUDENT_ALL_URI, null, null, null, null);
Cursor cursorOne = contentResolver.query(STUDENT_ONE_URI, null, "1", null, null);
int i = contentResolver.delete(STUDENT_ONE_URI, "2", null);
ContentValues contentValues = new ContentValues();
contentValues.put("name", "test");
int updateNumber = contentResolver.update(STUDENT_ONE_URI, contentValues, "1", null);
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
contentResolver = getContentResolver();
contentResolver.registerContentObserver(STUDENT_ALL_URI, true, new PersonOberserver(handler));
* 观察者,如果找到了当前uri的提供者,则会回调
public class PersonOberserver extends ContentObserver {
public PersonOberserver(Handler handler) {
super(handler);
this.handler =
public void onChange(boolean selfChange) {
super.onChange(selfChange);
Message msg = new Message();
handler.sendMessage(msg);
ContentProviter能够跨进程的存储数据,通过调用相对应的方法能够增、删、改、查。
Android之ContentProvide(内容提供者)
ContentProvider简介
深入理解Android四大组件之一ContentProvider
没有更多推荐了,}

我要回帖

更多关于 mediahousepro 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信