*读取联系人

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;
} }

注:记得配置读写短信和读写联系人的权限

<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.READ_SMS" />
<uses-permission android:name="android.permission.WRITE_CONTACTS" />
<uses-permission android:name="android.permission.WRITE_SMS" />

内容提供者Content Provider的更多相关文章

  1. 内容提供者(Content Provider)——跨程序共享数据

    内容提供者 Content Provider 应用的数据库是不允许其他应用访问的 内容提供者的作用就是让别的应用访问到你的数据库 自定义内容提供者,继承ContentProvider类,重写增删改查方 ...

  2. Android开发-API指南-Content Provider基础

    Content Provider Basics 英文原文:http://developer.android.com/guide/topics/providers/content-provider-ba ...

  3. Android内容提供者(Content provider)

    使用ContentProvider共享数据 当应用继承ContentProvider类,并重写该类用于提供数据和存储数据的方法,就可以向其他应用共享其数据.虽然使用其他方法也可以对外共享数据,但数据访 ...

  4. 内容提供器(Content Provider)

    一个跟数据库很相似的用于与其他程序传递信息的组件,用的也是数据库的CRUD操作 相关权限 注册内容提供者以及权限 <provider android:name=".ContentRes ...

  5. Android 内容提供器(Content Provider)介绍

    内容提供器(Content Provider)主要用于在不同的应用程序之间实现数据共享的功能,它提供了一套完整的机制,允许一个程序访问另一个程序中的数据,同时还能保证被访问数据的安全性.目前,使用内容 ...

  6. 【Android Developers Training】 94. 创建一个空内容提供器(Content Provider)

    注:本文翻译自Google官方的Android Developers Training文档,译者技术一般,由于喜爱安卓而产生了翻译的念头,纯属个人兴趣爱好. 原文链接:http://developer ...

  7. android学习十二(android的Content Provider(内容提供器)的使用)

    文件存储和SharePreference存储以及数据存储一般为了安全,最好用于当前应用程序中訪问和存储数据.内容提供器(Content Provider)主要用于在不同的应用程序之间实现数据共享的功能 ...

  8. Android学习之基础知识十—内容提供器(Content Provider)

    一.跨程序共享数据——内容提供器简介 内容提供器(Content Provider)主要用于在不同的应用程序之间实现数据共享的功能,它提供了一套完整的机制,允许一个程序访问另一个程序中的数据,同时还能 ...

  9. android笔记 : Content provider内容提供器

    内容提供器(Content Provider)主要用于在不同的应用程序之间实现数据共享的功能. 内容提供器的用法一般有两种,一种是使用现有的内容提供器来读取和操作相应程序中的数据,另一种是创建自己的内 ...

随机推荐

  1. derby数据库ql语法

    [数据库知识] 主键.唯一键包含索引 主键包含唯一键.索引.非空 唯一键包含索引,可空或非空 数据库需要与执行服务的在同个目录下 唯一键 create table app.tyu ( primaryk ...

  2. 转:Java NIO系列教程(三) Buffer

    Java NIO中的Buffer用于和NIO通道进行交互.如你所知,数据是从通道读入缓冲区,从缓冲区写入到通道中的. 缓冲区本质上是一块可以写入数据,然后可以从中读取数据的内存.这块内存被包装成NIO ...

  3. ECMAScript6-下一代Javascript标准

    介绍 ECMAScript6是下一代Javascript标准,这个标准将在2015年6月得到批准.ES6是Javascript的一个重大的更新,并且是自2009年发布ES5以来的第一次更新. 它将会在 ...

  4. UItableView 编辑

    - (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:( ...

  5. Leetcode 之Construct Binary Tree(52)

    根据先序和中序构造二叉树.根据中序和后序构造二叉树,基础题,采用递归的方式解决,两题的方法类似.需要注意的是迭代器的用法. //先序和中序 TreeNode *buildTree(vector< ...

  6. puppet组织架构

    树结构如下: |-- puppet.conf #主配置配置文件 |-- fileserver.conf #文件服务器配置文件 |-- auth.conf #认证配置文件 |-- autosign.co ...

  7. BZOJ2904

    找了一个晚上的资料,拼凑出来这么一个东西: 1) 如果是完全平方数返回12) 如果可以表示成形如$x^2+y^2$的形式输出2.这要求该数质因数分解后形如$4k+3$的质因数次数都是偶数.3) 如果该 ...

  8. 腾讯新浪通过IP地址获取当前地理位置(省份)的接口

    腾讯新浪通过IP地址获取当前地理位置(省份)的接口  腾讯的接口是 ,返回数组 http://fw.qq.com/ipaddress 返回值 var IPData = new Array(" ...

  9. exFAT是支持Mac和Win的

    exFAT是支持Mac和Win的 转自: http://bbs.feng.com/read-htm-tid-8214017.html

  10. 推荐一篇java抽象类和接口区别的文章

    写的不错,http://dev.yesky.com/436/7581936.shtml