标签:
1.ContentProvider 一般服务端设置共享数据,
当应用继承ContentProvider类,并重写该类用于提供数据和存储数据的方法,就可以向其他应用共享其数据。虽然使用其他方法也可以对外共享数据,但数据访问方式会因数据存储的方式而不同,如:采用文件方式对外共享数据,需要进行文件操作读写数据;采用sharedpreferences共享数据,需要使用sharedpreferences API读写数据。而使用ContentProvider共享数据的好处是统一了数据访问方式。
当应用需要通过ContentProvider对外共享数据时,需要以下几步:
第一步,需要创建一个继承ContentProvider类的子类StudentProvider。
第二步需要在AndroidManifest.xml使用<provider>对该ContentProvider进行配置,为了能让其他应用找到该ContentProvider ,ContentProvider 采用了authorities(主机名/域名)对它进行唯一标识,你可以把 ContentProvider看作是一个网站(想想,网站也是提供数据者),authorities 就是他的域名:
最好authorities唯一标示写成如下形式: android:authorities="包名.类如 android:authorities="com.example.android_contentprovider2.StudentProvider"
<manifest .... >
<application android:icon="@drawable/icon" android:label="@string/app_name">
<provider android:name=".StudentProvider" android:authorities=""com.example.contentprovider.StudentProvider"/>
</application>
</manifest>
第三步,需要设置访问内容提供者共享数据的Uri,因为客户端(ContentResolver)只能通过Uri访问数据,即当ContentResolver解析的Uri与内容提供者设置的Uri相配时,才可以操纵内容提供者的共享的数据。设置访问内容提供者共享数据的Uri通过UriMatcher类的方法addURI设置。
如,
static private UriMatcher URIMATCHER = new UriMatcher(UriMatcher.NO_MATCH);
static{
URIMATCHER.addURI("com.example.contentprovider.StudentProvider", "stu/#", STUDENT);
URIMATCHER.addURI("com.example.contentprovider.StudentProvider", "stu", STUDENTS);
}
第四步,学要重写在新建的子类StudentProvider类中重写父类中ContentProvider以下方法:
public boolean onCreate()
public Uri insert(Uri uri, ContentValues values)
public int delete(Uri uri, String selection, String[] selectionArgs)
public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs)
public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder)
public String getType(Uri uri)
在重写那些方法时,注意getType(Uri)方法,以下是getType方法的介绍:
public String getType(Uri) 用于返回指定的Uri中的数据的MIME类型
如果操作的数据属于集合类型,那么MIME类型字符串应该以vnd.android.cursor.dir/开头。
例如:要得到所有student记录的Uri为content://com.example.contentprovider.StudentProvider/student,那么返回的MIME类型字符串 为"vnd.android.cursor.dir/person"。
如果要操作的数据属于非集合类型数据,那么MIME类型字符串应该以vnd.android.cursor.item/开头。
例如:要得到id为10的student记录的Uri为content://com.example.contentprovider.StudentProvider/student/10,那么返回的MIME类 型字符串应为"vnd.android.cursor.item/student"。
好了,内容提供者设置好了,我们就可以在另外的应用里共享数据内容了。
注意:一旦应用继承了ContentProvider类,后面我们就会把这个应用称为ContentProvider(内容提供者)。
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。
以下是一个关于实例得源代码:
在StudentProvider.java文件中:
1 public class StudentProvider extends ContentProvider { 2 3 private final String TAG = "StudentProvider"; 4 5 private DbHelper helper = null; 6 7 private static final UriMatcher URI_MATCHER = new UriMatcher( 8 UriMatcher.NO_MATCH); 9 private static final int STUDENT = 1;// 操作单条记录 10 private static final int STUDENTS = 2;// 操作多条记录 11 static { 12 URI_MATCHER.addURI( 13 "com.example.contentprovider.StudentProvider", 14 "student", STUDENTS); 15 16 URI_MATCHER.addURI( 17 "com.example.contentprovider.StudentProvider", 18 "student/#", STUDENT); 19 } 20 21 public StudentProvider() { 22 // TODO Auto-generated constructor stub 23 } 24 25 @Override 26 public int delete(Uri uri, String selection, String[] selectionArgs) { 27 // TODO Auto-generated method stub 28 int count = -1;// 影响数据库的行数 29 try { 30 int flag = URI_MATCHER.match(uri); 31 SQLiteDatabase database = helper.getWritableDatabase(); 32 switch (flag) { 33 case STUDENT: 34 // content://com.example.android_contentprovider2.StudentProvider/student/1 35 // delete from student where id = ? //id 通过客户端传递过来的 36 long id = ContentUris.parseId(uri); 37 String where_value = " id = " + id; 38 if (selection != null && !selection.equals("")) { 39 where_value += " and " + selection; 40 } 41 count = database.delete("student", where_value, selectionArgs); 42 break; 43 case STUDENTS: 44 count = database.delete("student", selection, selectionArgs); 45 break; 46 } 47 } catch (Exception e) { 48 // TODO: handle exception 49 } 50 return count; 51 } 52 53 @Override 54 public String getType(Uri uri) { 55 // TODO Auto-generated method stub 56 int flag = URI_MATCHER.match(uri); 57 switch (flag) { 58 case STUDENT: 59 return "vnd.android.cursor.item/student"; 60 case STUDENTS: 61 return "vnd.android.cursor.dir/student"; 62 } 63 return null; 64 } 65 66 @Override 67 public Uri insert(Uri uri, ContentValues values) { 68 // TODO Auto-generated method stub 69 // insert into student () (?,?); 70 Uri resultUri = null; 71 int flag = URI_MATCHER.match(uri); 72 switch (flag) { 73 case STUDENTS: 74 SQLiteDatabase database = helper.getWritableDatabase(); 75 long id = database.insert("student", null, values);// 插入当前行的行号 76 resultUri = ContentUris.withAppendedId(uri, id); 77 break; 78 } 79 Log.i(TAG, "---->>" + resultUri.toString()); 80 return resultUri; 81 } 82 83 @Override 84 public boolean onCreate() { 85 // TODO Auto-generated method stub 86 helper = new DbHelper(getContext()); 87 return true; 88 } 89 90 @Override 91 public Cursor query(Uri uri, String[] projection, String selection, 92 String[] selectionArgs, String sortOrder) { 93 // TODO Auto-generated method stub 94 Cursor cursor = null; 95 try { 96 SQLiteDatabase database = helper.getReadableDatabase(); 97 int flag = URI_MATCHER.match(uri); 98 switch (flag) { 99 case STUDENT: 100 long id = ContentUris.parseId(uri); 101 String where_value = " id = " + id; 102 if (selection != null && !selection.equals("")) { 103 where_value += " and " + selection; 104 } 105 cursor = database.query("student", null, where_value, 106 selectionArgs, null, null, null, null); 107 break; 108 case STUDENTS: 109 cursor = database.query("student", null, selection, 110 selectionArgs, null, null, null); 111 break; 112 } 113 } catch (Exception e) { 114 // TODO: handle exception 115 } 116 return cursor; 117 } 118 119 @Override 120 public int update(Uri uri, ContentValues values, String selection, 121 String[] selectionArgs) { 122 // TODO Auto-generated method stub 123 int count = -1; 124 try { 125 // update table set name = ? ,address = ? where id = ? 126 SQLiteDatabase database = helper.getWritableDatabase(); 127 long id = ContentUris.parseId(uri); 128 int flag = URI_MATCHER.match(uri); 129 switch (flag) { 130 case STUDENT: 131 String where_value = " id = " + id; 132 if (selection != null && !selection.equals("")) { 133 where_value += " and " + selection; 134 } 135 count = database.update("student", values, where_value, 136 selectionArgs); 137 break; 138 139 } 140 } catch (Exception e) { 141 // TODO: handle exception 142 } 143 return count; 144 } 145 146 }
在MyTest.java文件中:
1 public class MyTest extends AndroidTestCase { 2 3 public MyTest() { 4 // TODO Auto-generated constructor stub 5 } 6 7 public void insert() { 8 // 访问内容提供者的步骤: 9 // 1、需要一个内容解析者 10 ContentResolver contentResolver = getContext().getContentResolver(); 11 // 使用content://+授权路径 12 Uri url = Uri 13 .parse("content://com.example.contentprovider.StudentProvider/student"); 14 ContentValues values = new ContentValues(); 15 values.put("name", "张三"); 16 values.put("address", "北京"); 17 contentResolver.insert(url, values); 18 } 19 20 public void delete() { 21 ContentResolver contentResolver = getContext().getContentResolver(); 22 // 删除单行记录,如果要删除多行记录:content://com.example.contentprovider.StudentProvider/student 23 Uri uri = Uri 24 .parse("content://com.example.contentprovider.StudentProvider/student/1"); 25 contentResolver.delete(uri, null, null); 26 } 27 28 public void update() { 29 ContentResolver contentResolver = getContext().getContentResolver(); 30 Uri uri = Uri 31 .parse("content://com.example.contentprovider.StudentProvider/student/2"); 32 ContentValues values = new ContentValues(); 33 values.put("name", "李斯"); 34 values.put("address", "上海"); 35 contentResolver.update(uri, values, null, null); 36 } 37 38 public void query(){ 39 ContentResolver contentResolver = getContext().getContentResolver(); 40 //查询单条记录:content://com.example.contentprovider.StudentProvider/student/2 41 //查询多条记录:content://com.example.contentprovider.StudentProvider/student 42 Uri uri = Uri 43 .parse("content://com.example.contentprovider.StudentProvider/student"); 44 //select * from student where id = 2; 45 Cursor cursor = contentResolver.query(uri, null, null, null, null); 46 while(cursor.moveToNext()){ 47 System.out.println("---->>"+cursor.getString(cursor.getColumnIndex("name"))); 48 } 49 } 50 }
标签:
原文地址:http://www.cnblogs.com/SoulCode/p/5414480.html