Android ContentProvider之自定义内容提供者以及UriMatcher使用
来源:互联网 发布:ubuntu如何设置ip地址 编辑:程序博客网 时间:2024/05/22 13:36
内容提供者
- 应用的数据库是不允许其他应用访问的
- 内容提供者的作用就是让别的应用访问到你的私有数据
自定义内容提供者,继承ContentProvider类,重写增删改查方法,在方法中写增删改查数据库的代码,举例增方法
@Overridepublic Uri insert(Uri uri, ContentValues values) { db.insert("person", null, values); return uri;}
在清单文件中定义内容提供者的标签,注意必须要有authorities属性,这是内容提供者的主机名,功能类似地址
<provider android:name="com.cy.contentprovider.PersonProvider" android:authorities="com.cy.person" android:exported="true" ></provider>
创建一个其他应用,访问自定义的内容提供者,实现对数据库的插入操作
public void click(View v){ //得到内容分解器对象 ContentResolver cr = getContentResolver(); ContentValues cv = new ContentValues(); cv.put("name", "小方"); cv.put("phone", 138856); cv.put("money", 3000); //url:内容提供者的主机名 cr.insert(Uri.parse("content://com.cy.person"), cv);}
UriMatcher
- 用于判断一条uri跟指定的多条uri中的哪条匹配
添加匹配规则
//指定多条urium.addURI("com.cy.person", "person", PERSON_CODE);um.addURI("com.cy.person", "company", COMPANY_CODE);//#号可以代表任意数字um.addURI("com.cy.person", "person/#", QUERY_ONE_PERSON_CODE);
通过Uri匹配器可以实现操作不同的表
@Overridepublic Uri insert(Uri uri, ContentValues values) { if(um.match(uri) == PERSON_CODE){ db.insert("person", null, values); } else if(um.match(uri) == COMPANY_CODE){ db.insert("company", null, values); } else{ throw new IllegalArgumentException(); } return uri;}
如果路径中带有数字,把数字提取出来的api
int id = (int) ContentUris.parseId(uri);
内容提供者
* Android四大组件
* ContentProvider
* 把私有数据共享给其他应用访问
* 使用内容提供者可以自己定义访问规则,选择私有数据中哪些共享出去,哪些不共享
<provider android:name="com.cy.mycontentprovider.PersonProvider" android:authorities="com.cy.people" android:exported="true" > </provider>
public class PersonProvider extends ContentProvider { private SQLiteDatabase db; //创建uri匹配器 UriMatcher um = new UriMatcher(UriMatcher.NO_MATCH); { //添加匹配规则 //arg0:主机名 //arg1:路径 //arg2:匹配码 um.addURI("com.cy.people", "person", 1);//content://com.cy.people/person um.addURI("com.cy.people", "handsome", 2);//content://com.cy.people/handsome um.addURI("com.cy.people", "person/#", 3);//content://com.cy.people/person/10 } //内容提供者创建时调用 @Override public boolean onCreate() { MyOpenHelper oh = new MyOpenHelper(getContext()); db = oh.getWritableDatabase(); return false; } //values:其他应用要插的数据 @Override public Uri insert(Uri uri, ContentValues values) { if(um.match(uri) == 1){ db.insert("person", null, values); //数据库改变了,内容提供者发出通知 //arg0:通知发到哪个uri上,注册在这个uri上的内容观察者都可以收到通知 getContext().getContentResolver().notifyChange(uri, null); } else if(um.match(uri) == 2){ db.insert("handsome", null, values); getContext().getContentResolver().notifyChange(uri, null); } else{ throw new IllegalArgumentException("uri传错啦傻逼"); } return uri; } @Override public int delete(Uri uri, String selection, String[] selectionArgs) { int i = 0; if(um.match(uri) == 1){ i = db.delete("person", selection, selectionArgs); } else if(um.match(uri) == 2){ i = db.delete("handsome", selection, selectionArgs); } else{ throw new IllegalArgumentException("uri又传错啦傻逼"); } return i; } @Override public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) { int i = db.update("person", values, selection, selectionArgs); return i; } @Override public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) { Cursor cursor = null; if(um.match(uri) == 1){ cursor = db.query("person", projection, selection, selectionArgs, null, null, sortOrder, null); } else if(um.match(uri) == 2){ cursor = db.query("handsome", projection, selection, selectionArgs, null, null, sortOrder, null); } else if(um.match(uri) == 3){ //取出uri末尾携带的数字 long id = ContentUris.parseId(uri); cursor = db.query("person", projection, "_id = ?", new String[]{"" + id}, null, null, sortOrder, null); } return cursor; } //返回通过指定uri获取的数据的mimetype @Override public String getType(Uri uri) { if(um.match(uri) == 1){ return "vnd.android.cursor.dir/person"; } else if(um.match(uri) == 2){ return "vnd.android.cursor.dir/handsome"; } else if(um.match(uri) == 3){ return "vnd.android.cursor.item/person"; } return null; }}
public class MyOpenHelper extends SQLiteOpenHelper { public MyOpenHelper(Context context) { super(context, "people.db", null, 2); } @Override public void onCreate(SQLiteDatabase db) { db.execSQL("create table person(_id integer primary key autoincrement, name char(10), phone char(20), money integer(10))"); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { db.execSQL("create table handsome(_id integer primary key autoincrement, name char(10), phone char(20))"); }}
public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } public void insert(View v){ //通过内容提供者把数据插入01数据库 //1.获取contentResolver ContentResolver resolver = getContentResolver(); //2.访问内容提供者,插入数据 ContentValues values = new ContentValues(); values.put("name", "傻逼"); values.put("phone", 138992); values.put("money", 14000); //arg0:指定内容提供者的主机名 resolver.insert(Uri.parse("content://com.cy.people/person"), values); values.clear(); values.put("name", "彩比"); values.put("phone", 15999); //arg0:指定内容提供者的主机名 resolver.insert(Uri.parse("content://com.cy.people/handsome"), values); } public void delete(View v){ ContentResolver resolver = getContentResolver(); int i = resolver.delete(Uri.parse("content://com.cy.people"), "name = ?", new String[]{"凤姐"}); System.out.println(i); } public void update(View v){ ContentResolver resolver = getContentResolver(); ContentValues values = new ContentValues(); values.put("money", 16001); int i = resolver.update(Uri.parse("content://com.cy.people"), values, "name = ?", new String[]{"抠逼"}); System.out.println(i); } public void query(View v){ ContentResolver resolver = getContentResolver(); Cursor cursor = resolver.query(Uri.parse("content://com.cy.people/person"), null, null, null, null); while(cursor.moveToNext()){ String name = cursor.getString(1); String phone = cursor.getString(2); String money = cursor.getString(3); System.out.println(name + ";" + phone + ";" + money); } } public void queryOne(View v){ ContentResolver resolver = getContentResolver(); Cursor cursor = resolver.query(Uri.parse("content://com.cy.people/person/4"), null, null, null, null); if(cursor.moveToNext()){ String name = cursor.getString(1); String phone = cursor.getString(2); String money = cursor.getString(3); System.out.println(name + ";" + phone + ";" + money); } }}
参考:Android ContentProvider
阅读全文
0 0
- Android ContentProvider之自定义内容提供者以及UriMatcher使用
- android内容提供者ContentProvider,UriMatcher和内容观察者ContentObserver常见使用
- Android自助餐之内容提供者ContentProvider使用
- ContentProvider(内容提供者)以及自定义ContentProvider
- contentprovider 自定义内容提供者
- 自定义ContentProvider内容提供者
- Android 之使用ContentProvider(内容提供者)共享数据
- Android知识梳理之ContentProvider内容提供者的使用
- Android ContentProvider(内容提供者)
- android内容提供者ContentProvider
- Android--- ContentProvider(内容提供者)
- Android 内容提供者ContentProvider
- Android内容提供者ContentProvider
- android内容提供者contentProvider
- android ContentProvider内容提供者
- Android-ContentProvider内容提供者
- Android ContentProvider 内容提供者
- android内容提供者ContentProvider
- chrome使用技巧
- ffplay源码剖析(3.2.4 + sdl2)(3):解码
- spring boot aop的使用
- 神经网络与深度学习 自制MNIST测试数据供神经网络测试
- LeetCode Graph:M399. Evaluate Division
- Android ContentProvider之自定义内容提供者以及UriMatcher使用
- Eclipse配置git,提交项目
- 福州大学2271x——弗洛伊德最短路
- Android动画解析(一)
- 模仿最新版爱奇艺卡片式轮播效果CardBannerDemo
- 带你玩转Netty(二)
- Android中ASE加密算法工具类
- Android Binder-框架简析
- 2017百度之星资格赛1003 度度熊与邪恶大魔王(完全背包)