1 // 查询用户定义字典并返回结果
2 mCursor = getContentResolver().query(
3 UserDictionary.Words.CONTENT_URI, 单词表的内容URI
4 mProjection,1)"> 查询的数据列名数组
5 mSelectionClause 查询条件,可以为null
6 mSelectionArgs,1)"> 查询参数,可以为null
7 mSortOrder); 返回数据对象的排序条件
1 content:user_dictionary/words
1 Uri singleUri = ContentUris.withAppendedId(UserDictionary.Words.CONTENT_URI,4);
-
请求读取Provider的访问权限。
-
定义查询Provider的代码。
1 "projection" 定义每行返回的列名数组
2 String[] mProjection =
3 {
4 UserDictionary.Words._ID,1)"> _ID column name
5 UserDictionary.Words.WORD,1)"> word column name
6 UserDictionary.Words.LOCALE locale column name
7 };
8
9 定义查询条件
10 String mSelectionClause = null;
11
12 定义查询条件参数
13 String[] mSelectionArgs = {""};
/*
2 * 定义查询参数
3 */
4 String[] mSelectionArgs = {"" 5
6 获取界面输入的查询条件
7 mSearchString = mSearchWord.getText().toString();
此处插入代码校验数据是否有效
10 如果条件为空,则查询所有
11 if (TextUtils.isEmpty(mSearchString)) {
12 如果查询条件为空,则返回所有内容
13 mSelectionClause = 14 mSelectionArgs[0] = ""15 } else {
16 构造查询条件,匹配用户输入的数据.
17 mSelectionClause = UserDictionary.Words.WORD + " = ?"18 查询参数.
19 mSelectionArgs[0] = mSearchString;
20 }
21
22 对表进行查询并返回Cursor对象
23 mCursor =24 UserDictionary.Words.CONTENT_URI,1)"> URI
25 mProjection,1)"> 查询数据列
26 mSelectionClause 查询条件
27 mSelectionArgs,1)"> 查询参数
28 mSortOrder); 返回结果排序行
29
30 如果出现查询异常,则返回空
31 if (null == mCursor) {
32 33 * 插入代码捕获异常
34 35 如果返回为空,则没有匹配的内容
36 } else if (mCursor.getCount() < 1) {
37 38 * 通知用户查询不成功. 但这不是必须的39 } 40 插入代码处理结果
41 }
SELECT _ID,word,locale FROM words WHERE word = <userinput> ORDER BY word ASC;
通过拼接用户输入和列名的方式构造查询条件
2 String mSelectionClause = "var = " + mUserInput;
用一个可替换参数来包含用户输入
2 String mSelectionClause = "var = ?";
定义一个查询条件的数组
2 String[] selectionArgs = {""};
将用户数据作为参数数据
2 selectionArgs[0] = mUserInput;
定义从Cursor中检索并加载到输出行的列名
2 String[] mWordListColumns =
4 UserDictionary.Words.WORD,1)"> word column name
5 UserDictionary.Words.LOCALE 7
8 //定义一个视图ID列表,该列表将接收每行的Cursor列
int[] mWordListItems = { R.id.dictWord,R.id.locale};
10
创建一个SimpleCursorAdapter对象
12 mCursorAdapter = new SimpleCursorAdapter(
13 getApplicationContext(),1)"> 应用程序上下文对象
14 R.layout.wordlistrow,1)"> ListView单行配置文件
15 mCursor,1)"> query函数返回的结果
16 mWordListColumns,1)"> Cursor中的列名数组
17 mWordListItems,1)"> ListView中Item项的布局文件
18 0); Flags (usually none are needed)
19
设置 adapter到ListView
21 mWordList.setAdapter(mCursorAdapter);
定义"word"列的索引
int index = mCursor.getColumnIndex(UserDictionary.Words.WORD);
3
4 5 * 当cursor有效的时候才执行. User Dictionary Provider如果发生内部错误,将返回null,其他provider可能会抛出异常
6 8 if (mCursor != 9 * 移动到cursor的下一行.在第一行移动之前,行指向是-1,如果试图去查询此位置上的内容,将会抛出一个异常
11 while (mCursor.moveToNext()) {
13 获取对应的列的值.
14 newWord = mCursor.getString(index);
15 插入代码处理获取的值.
16 ...
17 while 循环结束
18 }
19 } 20 展示错误和异常信息
21 }
<uses-permission android:name="android.permission.READ_USER_DICTIONARY">
定义一个新的 Uri对象,接收插入新行放回的内容
Uri mNewUri;
要插入的新值
5 ContentValues mNewValues = ContentValues();
6
* 设置每列对应的值
9 10 mNewValues.put(UserDictionary.Words.APP_ID,"example.user");
11 mNewValues.put(UserDictionary.Words.LOCALE,"en_US"12 mNewValues.put(UserDictionary.Words.WORD,"insert"13 mNewValues.put(UserDictionary.Words.FREQUENCY,"100"14
15 mNewUri = getContentResolver().insert(
16 UserDictionary.Word.CONTENT_URI,1)"> 内容 URI
17 mNewValues 插入的值
18 );
user_dictionary/words/<id_value>
包含更新的内容的对象
2 ContentValues mUpdateValues = 定义需要更新的查询条件
5 String mSelectionClause = UserDictionary.Words.LOCALE + "LIKE ?" 6 String[] mSelectionArgs = {"en_%" 定义更新行得到的行数
int mRowsUpdated = 0 * 设置更新的内容.
13 14 mUpdateValues.putNull(UserDictionary.Words.LOCALE);
15
16 mRowsUpdated = getContentResolver().update(
17 UserDictionary.Words.CONTENT_URI,1)">18 mUpdateValues 更新的内容
19 mSelectionClause 查询条件
20 mSelectionArgs 查询内容参数
21 );
定义需要删除的条件
2 String mSelectionClause = UserDictionary.Words.APP_ID + " LIKE ?" 3 String[] mSelectionArgs = {"user" 4
定义删除掉行数
int mRowsDeleted = 0 删除匹配条件的内容
9 mRowsDeleted = getContentResolver().delete(
10 UserDictionary.Words.CONTENT_URI,1)"> URI
11 mSelectionClause 删除条件
12 mSelectionArgs 删除参数
13 );
- integer
- long integer (long)
- floating point
- long floating point (double)
-
批量访问:可以在ContentProviderOperation中使用方法创建批量处理访问调用,然后用ContentResolver.applyBatch()应用它们。
-
异步查询:应该在单独的线程中进行查询,其中一种方法是使用CursorLoader对象。
-
通过intents访问数据:虽然不能直接向提供者发送intent,但可以向provider's application发送intent,而provider's application序通常是最适合修改provider数据的应用程序。
1 String[] mProjection =
2 3 UserDictionary.Words._ID,4 UserDictionary.Words.WORD,1)">5 UserDictionary.Words.LOCALE
6 };
通讯记录URI
2 private String call_uri = "content://call_log/calls" 4 内容解析器
5 private ContentResolver mResolver;
7 列表
8 ListView lvCall;
9
10 获取的通讯记录的列名
11 private String[] columns = String[]{
CallLog.Calls._ID,CallLog.Calls.CACHED_NAME,CallLog.Calls.NUMBER,CallLog.Calls.TYPE,CallLog.Calls.DATE,CallLog.Calls.DURATION
13 };
15 @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
19 初始化内容解析器
20 mResolver = getContentResolver();
21 lvCall = (ListView) this.findViewById(R.id.lv_call);
23
24 /**
25 * 获取通讯记录事件
26 * @param v
27 28 public bn_call(View v) {
29 List<Map<String,String>> list = new ArrayList<>();
30 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"31 Cursor cursor = mResolver.query(Uri.parse(call_uri),columns,1)">null,1)">,CallLog.Calls.DEFAULT_SORT_ORDER);
32 以下是为了转换数据格式
33 if(cursor!=){
34 (cursor.moveToNext()){
35 long dt=cursor.getLong(cursor.getColumnIndex("date"));
36 Date callDate = Date(dt);
37 String callDateStr = sdf.format(callDate);
38 String name=cursor.getString(cursor.getColumnIndex("name"39 String number=cursor.getString(cursor.getColumnIndex("number"40 String duration =cursor.getString(cursor.getColumnIndex("duration"))+"s"41 Map<String,String> map=new HashMap<String,String>() ;
42 map.put("name"43 map.put("number"44 map.put("date"45 map.put("duration"46 list.add(map);
47 }
48 }
49 将数据填充到Adapter
50 SimpleAdapter adapter=new SimpleAdapter(51 new String[]{"name","number","date","duration"},1)">52 new int[]{R.id.tv_name,R.id.tv_number,R.id.tv_time,R.id.tv_duration});
53
54 /*SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,cursor,1)">55 new String[]{"name","date"},1)">56 new int[]{R.id.tv_name,R.id.tv_time},1)">57 CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);58 绑定Adapter到ListView
59 lvCall.setAdapter(adapter);
60 }
private String sms_uri="content://sms" 2
3 private String[] columns= Telephony.Sms._ID,Telephony.Sms.ADDRESS,Telephony.Sms.CREATOR,Telephony.Sms.BODY,Telephony.Sms.DATE,Telephony.Sms.PERSON,Telephony.Sms.STATUS,Telephony.Sms.DATE_SENT
ListView lvMsg;
setContentView(R.layout.activity_main2);
15 mResolver=getContentResolver();
16 lvMsg= (ListView) .findViewById(R.id.lv_sms);
17 18
19 bn_sms(View view) {
20 List<Map<String,1)">21 SimpleDateFormat sdf = 22 Cursor cursor = mResolver.query(Uri.parse(sms_uri),Telephony.Sms.DEFAULT_SORT_ORDER);
23 if (cursor != 24 (cursor.moveToNext()) {
25 Log.e("TAG","bn_sms: "+cursor.getColumnIndex("person")+"---"+ cursor.getColumnIndex("date")+"---"+cursor.getColumnIndex("creator")+"---"+cursor.getColumnIndex("address"26 long dt = cursor.getLong(cursor.getColumnIndex("date"27 Date callDate = 28 String callDateStr =29 String person = cursor.getString(cursor.getColumnIndex("address"30 String creator = cursor.getString(cursor.getColumnIndex("creator"31 String duration =cursor.getString(cursor.getColumnIndex("duration"))+"s";
32 String body = cursor.getString(cursor.getColumnIndex("body"33 Map<String,String> map = 34 map.put("person"35 map.put("creator"36 map.put("date"37 map.put("duration",duration);
38 map.put("body"39 40 41 42 43 SimpleAdapter adapter = 44 new String[]{"person","creator","body"45 46
47 lvMsg.setAdapter(adapter);
49 }