*读取联系人
StringBuilder sb = new StringBuilder();// 1:得到中间人。ContentResolver resolver = getContentResolver();// 2:地址。// contacts;com.android.contacts:表示2个都可以String raw_contact_host = "content://com.android.contacts/raw_contacts";String data_host = "content://com.android.contacts/data";Uri uri = Uri.parse(raw_contact_host);Uri data_uri = Uri.parse(data_host);// 3:查询数据。Cursor cursor = resolver.query(uri, new String[] { "_id", "display_name" }, null, null, null);while (cursor.moveToNext()) { // contact_id int id = cursor.getInt(cursor.getColumnIndex("_id")); // ContentResolver resolver2 = getContentResolver(); Cursor data_cursor = resolver.query(data_uri, null, "raw_contact_id=?", new String[] { id + "" }, null) while (data_cursor.moveToNext()) { String mimytype = data_cursor.getString(data_cursor.getColumnIndex("mimetype")); String data = data_cursor.getString(data_cursor.getColumnIndex("data1")); if (mimytype.equals("vnd.android.cursor.item/phone_v2")) { // 电话号码 sb.append("电话号码:" + data); } else if (mimytype.equals("vnd.android.cursor.item/name")) { sb.append("名字:" + data); } else if (mimytype.equals("vnd.android.cursor.item/organization")) { sb.append("组织:" + data); } } // data_cursor.close(); String display_name = cursor.getString(cursor.getColumnIndex("display_name")); sb.append("姓名:" + display_name);}cursor.close();return sb.toString();
*读取短信
resolver = getContentResolver();add = "content://sms/";uri = Uri.parse(add);// 1:得到中间人。// 2:知道地址。// 查询所有的短信。// address:表示发短信的号码,date;发送的日期.body:发送的内容:这些都需要查看表才知道,不需要记忆。Cursor cursor = resolver.query(uri, new String[] { "address", "date", "body" }, null, null, null);while (cursor.moveToNext()) { String address = cursor.getString(cursor.getColumnIndex("address")); String body = cursor.getString(cursor.getColumnIndex("body")); int date = cursor.getInt(cursor.getColumnIndex("date")); System.out.println(address + "--" + body + "----" + date);}cursor.close();
*内容提供者
/* * authorities ;主机名。 * 1:创建一些地址。 * http://www.sina.com/index.html * 访问别人的数据。 * content://主机名/ * content://com.qf.day15_sqlitedemo1.person/person ------1 */public class PersonContentProvider extends ContentProvider { private static UriMatcher match; private static final String AUTHORITIES = "com.qf.day15_sqlitedemo1.person"; private PersonSqliteOpenHelper helper; private SQLiteDatabase db; static{ //创建匹配器。 match = new UriMatcher(UriMatcher.NO_MATCH);//当不匹配的时候就返回-1 //http://www.sina.com/ //content://com.qf.day15_sqlitedemo1.person/person match.addURI(AUTHORITIES, "person", 1);//表示查询person里面的所有数据。 match.addURI(AUTHORITIES, "person_id/#", 2);//表示查询person表中的id号为固定值的数据。 match.addURI(AUTHORITIES, "person_name/*", 3); } //内容提供者创建的时候调用。 @Override public boolean onCreate() { helper = new PersonSqliteOpenHelper(getContext()); db = helper.getWritableDatabase(); return true; } //查询 @Override public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) { Cursor cursor = null; //将别的应用的地址跟我们自己定义的地址进行匹配。 int code = match.match(uri); System.out.println(code); //对值进行判断。 if(code==1){ cursor = db.query("person", projection, selection, selectionArgs, null, null, null); }else if(code==2){ //获得id号 int id =Integer.parseInt( uri.getLastPathSegment()); cursor = db.query("person", projection, "_id=?", new String[]{id+""}, null, null, null); } return cursor; } //返回类型--一般不用 @Override public String getType(Uri uri) { return null; } //插入数据 @Override public Uri insert(Uri uri, ContentValues values) { return null; } //删除数据 @Override public int delete(Uri uri, String selection, String[] selectionArgs) { return 0; } //更新数据 @Override public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) { return 0; }}
注:记得配置读写短信和读写联系人的权限