package wyf.wpf; //声明包语句
import android.app.Activity; //引入相关类
import android.content.ContentResolver; //引入相关类
import android.database.Cursor; //引入相关类
import android.net.Uri; //引入相关类
import android.os.Bundle; //引入相关类
import android.provider.Contacts.People; //引入相关类
import android.widget.TextView; //引入相关类
//继承自Activity的子类
public class Sample_4_4 extends Activity {
String [] columns = { //查询Content Provider时希望返回的列
People._ID,
People.NAME,
};
Uri contactUri = People.CONTENT_URI; //访问Content Provider需要的Uri
TextView tv; //TextView对象引用
@Override
public void onCreate(Bundle savedInstanceState) { //重写onCreate方法
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tv = (TextView)findViewById(R.id.tv); //获得TextView对象引用
String result = getQueryData(); //调用方法访问Content Provider
tv.setText("ID\t名字\n"+result); //将查询到的信息显示到TextView中
}
//方法:获取联系人列表信息,返回String对象
public String getQueryData(){
String result = "";
ContentResolver resolver = getContentResolver(); //获取ContentResolver对象
Cursor cursor = resolver.query(contactUri, columns, null, null, null); //调用方法查询Content Provider
int idIndex = cursor.getColumnIndex(People._ID); //获得_ID字段的列索引
int nameIndex = cursor.getColumnIndex(People.NAME); //获得NAME字段的列索引
for(cursor.moveToFirst();(!cursor.isAfterLast());cursor.moveToNext()){ //遍历Cursor,提取数据
result = result + cursor.getString(idIndex)+ "\t";
result = result + cursor.getString(nameIndex)+ "\t\n";
}
cursor.close(); //关闭Cursor对象
return result;
}
}
/*
package wyf.wpf;
import android.app.Activity;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.Contacts.People;
import android.widget.TextView;
public class Sample_4_4 extends Activity {
String [] columns = {
People._ID,
People.NAME,
People.NUMBER
};
Uri contactUri = People.CONTENT_URI;
TextView tv; //TextView对象引用
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tv = (TextView)findViewById(R.id.tv);
tv.setText(getQueryData());
}
//方法:获取联系人列表信息,返回String对象
public String getQueryData(){
String result = "";
Cursor cursor = this.managedQuery(contactUri, columns, null, null, null);
System.out.println("========= the size of cursor is:"+cursor.getCount());
int idIndex = cursor.getColumnIndex(People._ID);
int nameIndex = cursor.getColumnIndex(People.NAME);
int numberIndex = cursor.getColumnIndex(People.NUMBER);
for(cursor.moveToFirst();(!cursor.isAfterLast());cursor.moveToNext()){ //遍历Cursor,提取数据
result = result + cursor.getString(idIndex)+ "\t";
result = result + cursor.getString(nameIndex)+ "\t";
result = result + cursor.getString(numberIndex)+ "\t\n";
}
cursor.close();
return result;
}
}
*/
AndroidManifest.xml添加权限
<uses-permission android:name="android.permission.READ_CONTACTS" />
分享到:
相关推荐
理解Content Uri的概念和作用,掌握通过Content Provider访问联系人的方法,掌握通过Content Provider访问通话记录的方法,掌握通过Content Provider访问短信的方法。
content provider实现数据共享demo(内有两个项目)
博文《android基础知识05:四大组件之content provider》的示例程序,实现了content provider 的基本功能。
Content Provider获取联系人和图片,博客链接:http://write.blog.csdn.net/postedit/43950781
四大组件 Activity Service content provider broadcast receiver
1、什么是内容提供者 2、怎样创建内容提供者 3、通过内容提供者完成CRUD操作 4、访问内容提供者 5、监听内容提供者数据变化 6、两个案例:监听短信以及操作联系人 最最重要的是包含了源码
android content provider学习过程中的笔记,希望对大家有帮助。
Android Content Provider Demo
content provider和sqlite的详细介绍以及用法。。很好的资源,我自己整理的,相当有用,大家可以看看哈。。
Android Content Provider用法样例,解析如何使用“内容提供商”Content Provider共享不同应用的数据,本Android例子将使用Content Provider实现了获取联系人信息,里面的注释非常丰富,是学习android的好资料。
这是使用Content Provider案例源码,详细讲解请移步http://blog.csdn.net/wei_zhi/article/details/52960073
关于Content Providers Content Providers 是所有应用程序之间数据存储和检索的一个桥梁,作用是使得各个...把18.SQLite和22.22.Content Provider添加到项目中,先运行18.SQLite,然后在进行22.Content Provider测试
代码有两个项目,一个集合了知识sharepreference,content provider,另一个集合了listview的动态增删改查,有sqlite操作数据的增删改查和content provider操作sqlite的增删改查,经本人亲测项目导入便可运行,这是第...
android content provider两个程序共享数据实例demo。简单的两个应用app之间共享数据。 getContentResolver().insert() getContentResolver().query() getContentResolver().delete() getContentResolver().update()
Android中使用Content Provider组件访问通讯录中的联系人和添加联系人案例详解!
content_provider_demo, provide data to other app, use cursorLoader to update view
Content Provider详解
Android应用程序组件Content Provider的共享数据更新通知机制分析
NULL 博文链接:https://kevinlynx.iteye.com/blog/857721