android:四大组件之一content provider
2015-09-03 00:06
585 查看
ContentProvider(内容提供者)是Android中的四大组件之一。主要用于对外共享数据,也就是通过ContentProvider把应用中的数据共享给其他应用访问,其他应用可以通过ContentProvider对指定应用中的数据进行操作,统一了数据访问的方式。可以类似的比作访问网页网页获取数据,或者SOA.
1、ContentProvider
主要方法:
public boolean onCreate() 在创建ContentProvider时调用
public Cursor query(Uri, String[], String, String[], String) 用于查询指定Uri的ContentProvider,返回一个Cursor
public Uri insert(Uri, ContentValues) 用于添加数据到指定Uri的ContentProvider中
public int update(Uri, ContentValues, String, String[]) 用于更新指定Uri的ContentProvider中的数据
public int delete(Uri, String, String[]) 用于从指定Uri的ContentProvider中删除数据
public String getType(Uri) 用于返回指定的Uri中的数据的MIME类型
2、ContentResolver
当外部应用需要对ContentProvider中的数据进行添加、删除、修改和查询操作时,可以使用ContentResolver类来完成,要获取ContentResolver对象,可以使用Context提供的getContentResolver()方法。
ContentResolver提供的方法和ContentProvider提供的方法对应的有以下几个方法。
public Uri insert(Uri uri, ContentValues values) 用于添加数据到指定Uri的ContentProvider中。
public int delete(Uri uri, String selection, String[] selectionArgs) 用于从指定Uri的ContentProvider中删除数据。
public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) 用于更新指定Uri的ContentProvider中的数据。
public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) 用于查询指定Uri的ContentProvider。
下面是实现的代码:
UserInfoProvider .java
UserInfo .java
AndroidManifest.xml
提示:报错java.lang.SecurityException:Permission Denial
需要在provider中添加:android:exported=”true”
新建一个TestAPP工程测试这个方法:
1、ContentProvider
主要方法:
public boolean onCreate() 在创建ContentProvider时调用
public Cursor query(Uri, String[], String, String[], String) 用于查询指定Uri的ContentProvider,返回一个Cursor
public Uri insert(Uri, ContentValues) 用于添加数据到指定Uri的ContentProvider中
public int update(Uri, ContentValues, String, String[]) 用于更新指定Uri的ContentProvider中的数据
public int delete(Uri, String, String[]) 用于从指定Uri的ContentProvider中删除数据
public String getType(Uri) 用于返回指定的Uri中的数据的MIME类型
getType 集合类型:vnd.android.cursor.dir/ 非集合类型:vnd.android.cursor.item/
2、ContentResolver
当外部应用需要对ContentProvider中的数据进行添加、删除、修改和查询操作时,可以使用ContentResolver类来完成,要获取ContentResolver对象,可以使用Context提供的getContentResolver()方法。
ContentResolver提供的方法和ContentProvider提供的方法对应的有以下几个方法。
public Uri insert(Uri uri, ContentValues values) 用于添加数据到指定Uri的ContentProvider中。
public int delete(Uri uri, String selection, String[] selectionArgs) 用于从指定Uri的ContentProvider中删除数据。
public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) 用于更新指定Uri的ContentProvider中的数据。
public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) 用于查询指定Uri的ContentProvider。
下面是实现的代码:
UserInfoProvider .java
public class UserInfoProvider extends ContentProvider { private DBOpenHelp dbOpenHelper; private static final UriMatcher MATCHER = new UriMatcher(UriMatcher.NO_MATCH); private static final int USERS = 1; private static final int USER = 2; static{ MATCHER.addURI("com.example.sqlitetest.userprovider", "user_info", USERS); MATCHER.addURI("com.example.sqlitetest.userprovider", "user_info/#", USER); } @Override public boolean onCreate() { dbOpenHelper = new DBOpenHelp(this.getContext()); return true; } @Override public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) { SQLiteDatabase db = dbOpenHelper.getReadableDatabase(); switch (MATCHER.match(uri)) { case USERS: return db.query("user_info", projection, selection, selectionArgs, null, null, sortOrder); case USER: long rowid = ContentUris.parseId(uri); String where = "user_id="+ rowid; if(selection!=null && !"".equals(selection.trim())){ where += " and "+ selection; } return db.query("user_info", projection, where, selectionArgs, null, null, sortOrder); default: throw new IllegalArgumentException("this is Unknown Uri:"+ uri); } } @Override public String getType(Uri uri) { // TODO Auto-generated method stub return null; } @Override public Uri insert(Uri uri, ContentValues values) { SQLiteDatabase db= dbOpenHelper.getWritableDatabase(); switch (MATCHER.match(uri)) { case USERS: /** * long android.database.sqlite.SQLiteDatabase.insert * (String table, String nullColumnHack, ContentValues values) * */ long rowid = db.insert("user_info" , "user_name" , values); Uri insertUri = ContentUris.withAppendedId(uri, rowid); this.getContext().getContentResolver().notifyChange(uri, null); return insertUri; default: throw new IllegalArgumentException("this is Unknown Uri:"+ uri); } } @Override public int delete(Uri uri, String selection, String[] selectionArgs) { SQLiteDatabase db = dbOpenHelper.getWritableDatabase(); int num = 0; switch (MATCHER.match(uri)) { case USERS: num = db.delete("user_info", selection, selectionArgs); break; case USER: long rowid = ContentUris.parseId(uri); String where = "user_id="+ rowid; if(selection!=null && !"".equals(selection.trim())){ where += " and "+ selection; } num = db.delete("user_info", where, selectionArgs); break; default: throw new IllegalArgumentException("this is Unknown Uri:"+ uri); } return num; } @Override public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) { SQLiteDatabase db = dbOpenHelper.getWritableDatabase(); int num = 0; switch (MATCHER.match(uri)) { case USERS: num = db.update("user_info", values, selection, selectionArgs); break; case USER: long rowid = ContentUris.parseId(uri); String where = "user_id="+ rowid; if(selection!=null && !"".equals(selection.trim())){ where += " and "+ selection; } num = db.update("user_info", values, where, selectionArgs); break; default: throw new IllegalArgumentException("this is Unknown Uri:"+ uri); } return num; } }
UserInfo .java
public class UserInfo { int user_id; String user_name; int user_age; public UserInfo(int user_id, String user_name, int user_age) { super(); this.user_id = user_id; this.user_name = user_name; this.user_age = user_age; } public int getUser_id() { return user_id; } public void setUser_id(int user_id) { this.user_id = user_id; } public String getUser_name() { return user_name; } public void setUser_name(String user_name) { this.user_name = user_name; } public int getUser_age() { return user_age; } public void setUser_age(int user_age) { this.user_age = user_age; } @Override public String toString() { return "UserInfo [user_id=" + user_id + ", user_name=" + user_name + ", user_age=" + user_age + "]"; } }
AndroidManifest.xml
提示:报错java.lang.SecurityException:Permission Denial
需要在provider中添加:android:exported=”true”
<provider android:name=".UserInfoProvider" android:authorities="com.example.sqlitetest.userprovider" android:exported="true" />
新建一个TestAPP工程测试这个方法:
public class test extends AndroidTestCase{ public void testInsert() throws Exception{ Uri uri = Uri.parse("content://com.example.sqlitetest.userprovider/user_info"); ContentResolver resolver = this.getContext().getContentResolver(); ContentValues values = new ContentValues(); values.put("user_age", 18); values.put("user_name", "bank_atm"); resolver.insert(uri, values); } public void testDelete() throws Exception{ Uri uri = Uri.parse("content://com.example.sqlitetest.userprovider/user_info"); ContentResolver resolver = this.getContext().getContentResolver(); resolver.delete(uri, null, null); } public void testUpdate() throws Exception{ Uri uri = Uri.parse("content://com.example.sqlitetest.userprovider/user_info/41"); ContentResolver resolver = this.getContext().getContentResolver(); ContentValues values = new ContentValues(); values.put("user_name", "xiaoniu"); resolver.update(uri, values, null, null); } public void testQuery() throws Exception{ Uri uri = Uri.parse("content://com.example.sqlitetest.userprovider/user_info"); ContentResolver resolver = this.getContext().getContentResolver(); Cursor cursor = resolver.query(uri, null, null, null, "user_id asc"); if(cursor.getCount()>0){ while(cursor.moveToNext()){ String name = cursor.getString(cursor.getColumnIndex("user_name")); Log.i("TEST", name); }} cursor.close(); } }
相关文章推荐
- Google《Android性能优化》学习笔记
- android:数据的存储与访问----Sharedpreferences
- Android-图片选择,压缩,上传,加载 的实例(图片上传(多图也可),服务器端实现,图片加载(图片轮播实现))
- android:数据的存储与访问----文件方法
- Android ListView性能优化,异步加载图片
- Android Api Demos登顶之路(五十五)Service Messenger Service
- Android + sqlite simplecursorAdapter Demo
- Android-图片选择,压缩,上传,加载的实例 (图片选择,压缩实现 )
- Android 开发日志——优化ListView
- 用Android Studio进行NDK开发(简化流程)
- Android学习笔记(十六)
- android应用开发之判断wifi下局域网IP
- android:如何取消标题栏和实现全屏
- Android基础学习——Activity生命周期
- Android-图片选择,压缩,上传,加载 的 实例
- Android中退出程序的方法
- 详解Android中AsyncTask的使用
- Android中的隐藏API和Internal包的使用
- android___文件访问权限
- Android问题集(七)——TableLayout 中让TableRow中的控件填充满整列