获取通讯录app源码(获取通讯录app源码过报毒)
本文目录一览:
android开发怎样获取通讯录联系人信息
在Android开发中,我经常会遇到需要获取手机通讯录联系人信息,Android手机的通讯录联系人全部都存在系统的数据库中,如果须要获得通讯里联系人的信息就须要访问系统的数据库,才能将信息获取出来。
下面直接贴出代码供大家参考:
程序文件java代码:
import java.io.InputStream;
import java.util.ArrayList;
import android.app.ListActivity;
import android.content.ContentResolver;
import android.content.ContentUris;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.provider.ContactsContract.CommonDataKinds.Phone;
import android.provider.ContactsContract.CommonDataKinds.Photo;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;
public class ContactsActivity extends ListActivity {
Context mContext = null;
/**获取库Phon表字段**/
private static final String[] PHONES_PROJECTION = new String[] {
Phone.DISPLAY_NAME, Phone.NUMBER, Photo.PHOTO_ID,Phone.CONTACT_ID };
/**联系人显示名称**/
private static final int PHONES_DISPLAY_NAME_INDEX = 0;
/**电话号码**/
private static final int PHONES_NUMBER_INDEX = 1;
/**头像ID**/
private static final int PHONES_PHOTO_ID_INDEX = 2;
/**联系人的ID**/
private static final int PHONES_CONTACT_ID_INDEX = 3;
/**联系人名称**/
private ArrayListString mContactsName = new ArrayListString();
/**联系人头像**/
private ArrayListString mContactsNumber = new ArrayListString();
/**联系人头像**/
private ArrayListBitmap mContactsPhonto = new ArrayListBitmap();
ListView mListView = null;
MyListAdapter myAdapter = null;
@Override
public void onCreate(Bundle savedInstanceState) {
mContext = this;
mListView = this.getListView();
/**得到手机通讯录联系人信息**/
getPhoneContacts();
myAdapter = new MyListAdapter(this);
setListAdapter(myAdapter);
mListView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView? adapterView, View view,
int position, long id) {
//调用系统方法拨打电话
Intent dialIntent = new Intent(Intent.ACTION_CALL, Uri
.parse("tel:" + mContactsNumber.get(position)));
startActivity(dialIntent);
}
});
super.onCreate(savedInstanceState);
}
/**得到手机通讯录联系人信息**/
private void getPhoneContacts() {
ContentResolver resolver = mContext.getContentResolver();
// 获取手机联系人
Cursor phoneCursor = resolver.query(Phone.CONTENT_URI,PHONES_PROJECTION, null, null, null);
if (phoneCursor != null) {
while (phoneCursor.moveToNext()) {
//得到手机号码
String phoneNumber = phoneCursor.getString(PHONES_NUMBER_INDEX);
//当手机号码为空的或者为空字段 跳过当前循环
if (TextUtils.isEmpty(phoneNumber))
continue;
//得到联系人名称
String contactName = phoneCursor.getString(PHONES_DISPLAY_NAME_INDEX);
//得到联系人ID
Long contactid = phoneCursor.getLong(PHONES_CONTACT_ID_INDEX);
//得到联系人头像ID
Long photoid = phoneCursor.getLong(PHONES_PHOTO_ID_INDEX);
//得到联系人头像Bitamp
Bitmap contactPhoto = null;
//photoid 大于0 表示联系人有头像 如果没有给此人设置头像则给他一个默认的
if(photoid 0 ) {
Uri uri =ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI,contactid);
InputStream input = ContactsContract.Contacts.openContactPhotoInputStream(resolver, uri);
contactPhoto = BitmapFactory.decodeStream(input);
}else {
contactPhoto = BitmapFactory.decodeResource(getResources(), R.drawable.contact_photo);
}
mContactsName.add(contactName);
mContactsNumber.add(phoneNumber);
mContactsPhonto.add(contactPhoto);
}
phoneCursor.close();
}
}
/**得到手机SIM卡联系人人信息**/
private void getSIMContacts() {
ContentResolver resolver = mContext.getContentResolver();
// 获取Sims卡联系人
Uri uri = Uri.parse("content://icc/adn");
Cursor phoneCursor = resolver.query(uri, PHONES_PROJECTION, null, null,
null);
if (phoneCursor != null) {
while (phoneCursor.moveToNext()) {
// 得到手机号码
String phoneNumber = phoneCursor.getString(PHONES_NUMBER_INDEX);
// 当手机号码为空的或者为空字段 跳过当前循环
if (TextUtils.isEmpty(phoneNumber))
continue;
// 得到联系人名称
String contactName = phoneCursor
.getString(PHONES_DISPLAY_NAME_INDEX);
//Sim卡中没有联系人头像
mContactsName.add(contactName);
mContactsNumber.add(phoneNumber);
}
phoneCursor.close();
}
}
class MyListAdapter extends BaseAdapter {
public MyListAdapter(Context context) {
mContext = context;
}
public int getCount() {
//设置绘制数量
return mContactsName.size();
}
@Override
public boolean areAllItemsEnabled() {
return false;
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
ImageView iamge = null;
TextView title = null;
TextView text = null;
if (convertView == null) {
convertView = LayoutInflater.from(mContext).inflate(
R.layout.colorlist, null);
iamge = (ImageView) convertView.findViewById(R.id.color_image);
title = (TextView) convertView.findViewById(R.id.color_title);
text = (TextView) convertView.findViewById(R.id.color_text);
}
//绘制联系人名称
title.setText(mContactsName.get(position));
//绘制联系人号码
text.setText(mContactsNumber.get(position));
//绘制联系人头像
iamge.setImageBitmap(mContactsPhonto.get(position));
return convertView;
}
}
}
javaweb 做一个通讯录的小项目 求源代码 急求。
先设计数据库中通讯录表格(字段有:id,联系人姓名,手机号,备注),然后,hebinate实现数据库表到javabean的映射,同时也会生成对通讯录表格的增删改查的基本sql语句对应的接口。然后你再写一个Servlet,连接页面自己数据库操作接口即可
C++通讯录的源代码
#includeiostream
#includestring
#includeiomanip
#includelist
#includefstream
#includealgorithm
#define SPACE "\t\t\t"
using namespace std;
struct Record{
string name;
string tel;
public:
friend ostream operator(ostream os,const Record *rec){
return osrec-nameSPACErec-telendl;
};
friend istream operator(istream is,Record *rec){
isrec-name;
isrec-tel;
return is;
}
static bool comp(string patten,int type,Record *rec){
switch(type){
case 0: {return rec-name==patten;}break;
case 1: {return rec-tel==patten;}break;
}
return false;
}
static bool isvan(Record *rec){
return (rec-name==""rec-tel=="");
}
};
class RecordList:public listRecord*
{
public:
friend ostream operator(ostream os,const RecordList c_rl){
RecordList::const_iterator it=c_rl.begin();
while(it!=c_rl.end()){
os*it;
it++;
}
return os;
}
friend istream operator(istream is,RecordList rl){
RecordList::const_iterator it;
while(!is.eof()){
Record *prec=new Record;
isprec;
if(Record::isvan(prec)) continue;
rl.listRecord*::push_back(prec);
}
return is;
}
void clear();
iterator erase(iterator pos);
~RecordList(){
this-clear();
}
};
void RecordList::clear(){
RecordList::const_iterator it=this-begin();
while(it!=this-end()){
delete(*it);
it++;
}
this-listRecord*::clear();
}
RecordList::iterator RecordList::erase(iterator pos){
delete(*pos);
return listRecord*::erase(pos);
}
class AddressList{
protected:
RecordList recList;
public:
typedef RecordList::iterator Iterator;
//从通信录中寻找一个记录
virtual Iterator findRecord(string patten,int type,Iterator from);
virtual void addRecord(Record* rec);
virtual void removeRecord(Iterator it);
virtual void editRecord(Record* rec,Iterator it);
virtual void saveRecords(ostream os);
virtual void loadRecords(istream is);
virtual void addRecords(istream is);
virtual ~AddressList();
const int size() const{return (int)recList.size();}
const void clear(){recList.clear();}
Iterator begin(){return recList.begin();}
Iterator end() {return recList.end();}
};
AddressList::Iterator AddressList::findRecord(string patten,int type,Iterator from){
Iterator it;
it=from;
while(it!=recList.end()){
if(Record::comp(patten,type,*it)){
return it;
}
it++;
}
return it;
}
void AddressList::addRecord(Record* rec){
recList.push_back(rec);
}
void AddressList::removeRecord(Iterator it){
recList.erase(it);
}
void AddressList::editRecord(Record* rec,Iterator it){
delete(*it);
*it=rec;
}
void AddressList::saveRecords(ostream os){
osrecList;
}
void AddressList::loadRecords(istream is){
isrecList;
}
void AddressList::addRecords(istream is){
}
AddressList::~AddressList(){
recList.clear();
}
class AddressBook : public AddressList{
protected:
bool isModified;//通信录是否已被修改还未保存
public:
AddressBook();
virtual ~AddressBook();
//开始通信录的用户界面操作
virtual void start();
//菜单处理函数管理程序的用户界面操作
virtual void handleMenu();
//显示记录
virtual void displayRecords();
//查询记录
virtual void queryRecord();
//添加记录
virtual void addRecord();
//保存记录
virtual void saveRecords();
//读取记录
virtual void loadRecords();
//结束程序
virtual void quit();
//清空当前记录信息
virtual void clear();
protected:
//菜单选择函数
char menuSelect();
//显示表头
void dispTable();
};
AddressBook::AddressBook(){
}
AddressBook::~AddressBook(){;}
char AddressBook::menuSelect(){
char a=0;
cina;
if(a=='y'||a=='Y'){a='1';}
if(a=='n'||a=='N'){a='2';}
return a;
}
void AddressBook::start(){
cout "\n\t***** 欢迎使用sjb版通讯录 *****";
handleMenu();
}
void AddressBook::handleMenu(){
cout "\n\n\t\t\t-- 菜单管理 --\n\n";
cout "\tA -- 添加人员纪录 (Add Record)\n"
"\tF -- 查询纪录 (Find Record)\n"
"\tD -- 显示纪录 (Display Records)\n"
"\tL -- 从文本或标准输入流中加载纪录 (Load Records)\n"
"\tC -- 清空当前记录信息\n"
"\tS -- 保存记录 (Save Records)\n"
"\tQ -- 结束程序 (QUIT)\n";
switch(menuSelect()){
case 'A':
case 'a':addRecord();break;
case 'F':
case 'f':queryRecord();break;
case 'D':
case 'd':displayRecords();break;
case 'L':
case 'l':loadRecords();break;
case 'C':
case 'c':clear();break;
case 'S':
case 's':saveRecords();break;
case 'Q':
case 'q':quit();break;
}
handleMenu();
}
void AddressBook::displayRecords(){
cout"姓名"SPACE"电话"endl;
AddressList::saveRecords(cout);
cout"一共找到"size()"个对象。"endl;
cout"1"SPACE"返回"endl;
cout"2"SPACE"修改"endl;
cout"3"SPACE"删除"endl;
switch(menuSelect()){
case '1':break;
case '2':{
if(!size()){
cout"记录为0,无法修改。"endl;
break;
}
cout"要修改第几个?""1~"size()endl;
int i;
cini;
if(i1||isize()){
break;
}
Record *prec=new Record;
cout"姓名:"'\n'"电话:"'\n';
cinprec;
Iterator it=recList.begin();
while(i!=1){
it++;
i--;
}
editRecord(prec,it);
}break;
case'3':{
if(!size()){
cout"记录为0,无法修改。"endl;
break;
}
cout"要删除第几个?""1~"size()endl;
int i;
cini;
if(i1||isize()){
break;
}
Iterator it=recList.begin();
while(i!=1){
it++;
i--;
}
removeRecord(it);
}break;
}
}
void AddressBook::addRecord(){
string name,tel;
cout "\n输入名字: ";
cin name;
cout "输入电话: ";
cin tel;
Record* temp=new Record;
temp-name=name;
temp-tel=tel;
AddressList::addRecord(temp);
cout"是否继续(Y/N)"endl;
switch(menuSelect()){
case '1':addRecord();break;
case '2':break;
}
}
void AddressBook::queryRecord(){
cout"1"SPACE"按姓名查找"endl;
cout"2"SPACE"按电话查找"endl;
int type=-1;
string patten;
switch(menuSelect()){
case '1':{
type=0;
cout"请输入姓名"endl;
cinpatten;
}break;
case '2':{
type=1;
cout"请输入电话号码"endl;
cinpatten;
}break;
}
Iterator it=begin();
listIterator li;
cout"姓名"SPACE"电话"endl;
while((it=findRecord(patten,type,it))!=end()){
cout*it;
li.push_back(it);
it++;
}
cout"一共找到"li.size()"个对象。"endl;
cout"1"SPACE"返回"endl;
cout"2"SPACE"修改"endl;
cout"3"SPACE"删除"endl;
switch(menuSelect()){
case'1':break;
case'2':{
if(!li.size()){
cout"记录为0,无法修改。"endl;
break;
}
cout"要修改第几个?""1~"li.size()endl;
int i;
cini;
Record *prec=new Record;
cout"姓名:"'\n'"电话:"'\n';
cinprec;
listIterator::const_iterator it=li.begin();
while(i!=1){
it++;
i--;
}
editRecord(prec,*it);
}break;
case'3':{
if(!li.size()){
cout"记录为0,无法修改。"endl;
break;
}
cout"要删除第几个?""1~"li.size()endl;
int i;
cini;
listIterator::const_iterator it=li.begin();
while(i!=1){
it++;
i--;
}
removeRecord(*it);
}break;
}
}
void AddressBook::saveRecords(){
cout"1"SPACE"使用默认文件名。"endl;
cout"2"SPACE"使用自定义文件名。"endl;
cout"3"SPACE"返回"endl;
switch(menuSelect()){
case '1':{
ofstream file("default",ios::app);
AddressList::saveRecords(file);
file.close();
}break;
case '2':{
cout"请输入您定义的文件名:"endl;
string fname;
cinfname;
ofstream file(fname.c_str(),ios::app);
AddressList::saveRecords(file);
file.close();
}break;
case '3':break;
}
}
void AddressBook::loadRecords(){
cout"1"SPACE"使用默认文件。"endl;
cout"2"SPACE"使用自定义文件。"endl;
cout"3"SPACE"返回"endl;
switch(menuSelect()){
case '1':{
cout"1"SPACE"覆盖当前内容"endl;
cout"2"SPACE"追加到当前文件"endl;
switch(menuSelect()){
case '1':
AddressList::clear();break;
case '2':break;
}
ifstream file("default");
AddressList::loadRecords(file);
file.close();
}break;
case '2':{
cout"请输入文件名:"endl;
string fname;
cinfname;
ifstream file(fname.c_str());
AddressList::loadRecords(file);
file.close();
}break;
case '3':break;
}
}
void AddressBook::clear(){
cout"是否要清除内存中所有内容?(Y/N)"endl;
switch(menuSelect()){
case '1':AddressList::clear();break;
case '2':break;
}
}
void AddressBook::quit(){
cout"是否要保存文件?(Y/N)"endl;
switch(menuSelect()){
case'1':saveRecords();break;
case'2':break;
}
exit(0);
}
int main(int argc, char *argv[])
{
AddressBook a;
a.start();
system("PAUSE");
return EXIT_SUCCESS;
}