在2.0版本之前,获取通讯录中的联系人及其电话号码的方式如下:
String string = "";
super.onCreate(savedInstanceState);
ContentResolver cr = getContentResolver();
Cursor cursor = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
while (cursor.moveToNext())
{
int nameFieldColumnIndex = cursor.getColumnIndex(PhoneLookup.DISPLAY_NAME);
String contact = cursor.getString(nameFieldColumnIndex);
int numberFieldColumnIndex = cursor.getColumnIndex(PhoneLookup.NUMBER);
String number = cursor.getString(numberFieldColumnIndex);
string += (contact+":"+number+"\n");
}
但是在2.0及之后的版本中,获取联系人的方式不变,但是电话号码的方式发生改变, 具体代码如下:
String string = "";
super.onCreate(savedInstanceState);
ContentResolver cr = getContentResolver();
Cursor cursor = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
while (cursor.moveToNext())
{
int nameFieldColumnIndex = cursor.getColumnIndex(PhoneLookup.DISPLAY_NAME);
String contact = cursor.getString(nameFieldColumnIndex);
string += contact + ":";
String contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
Cursor phone = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID+"="+contactId, null, null);
while (phone.moveToNext())
{
int numberFieldColumnIndex = phone.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
String strPhoneNumber = phone.getString(numberFieldColumnIndex);
string += " " + strPhoneNumber;
}
string += "\n";
phone.close();
}
cursor.close();
可见,在2.0之后的版本中要获取手机号码就只能通过先取得联系人ID然后再取得联系人电话号码, 而且号码可能不止一个.
分享到:
相关推荐
NULL 博文链接:https://zymic.iteye.com/blog/737643
android的SDK做了调整,相应的读取联系人资料有了变化。 这里提供的是如何在2.0包括2.1中读取联系人资料的代码
NULL 博文链接:https://zymic.iteye.com/blog/737681
本篇文档主要记录一下获取联系人的电话号码的一种方式。 1、选择联系人 ............ //构造一个隐式的Intent,拉起联系人界面 final Intent pickIntent = new Intent(Intent.ACTION_PICK, ContactsContract....
Android-ContactsContract联系人增删改查
Android如何获取手机联系人信息,本文为大家揭晓。 获取手机联系人信息步骤: 1、获取 ContentResolver ContentResolver resolver = getContentResolver(); 2、resolver.query(*)查询信息 查询手机联系人的URI...
//获取所有电话号码 String phoneTpye = phones.getString(phones.getColumnIndex( ContactsContract.CommonDataKinds.Phone.TYPE));//获取电话号码类型 if(phoneTpye.equals("2"))//向所有用户的手机发送...
在上面的代码中,我们使用 ContentResolver 的 query 方法来查询 ContactsContract.CommonDataKinds.Phone.CONTENT_URI,从而获取联系人的电话信息。 Android 系统提供了便捷的方式来访问和管理手机联系人信息。...
这个是android系统原生代码中的联系人代码,在源代码的packages文件夹下,需要做联系人相关工作的同仁们,可以解压后看一看,同时,附赠上一个博客链接http://blog.csdn.net/Kafka_88/article/details/50670406 ...
本文实例讲述了Android获取联系人头像的方法。分享给大家供大家参考,具体如下: public byte[] getPhoto(String people_id) { String photo_id = null; String selection1 = ContactsContract.Contacts._ID + = + ...
Android 获取系统联系人信息... ContactsContract.Contacts.CONTENT_URI); try { startActivityForResult(intent, Contacts1RequestCode); } catch (Exception e) { LogManager.e(打开联系人信息失败); } 添加权
最近想写段android程序玩玩。 开发环境 eclipse ,android2.2 ...1.把通讯录中的联系人,电话号码保存到txt文件中完成备份。 2.读取txt文件,导入到通讯录完成还原。 具体代码 1.添加 通讯录读写权限,存储卡写权限 ...
//取得电话号码 String ContactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID)); Cursor phone = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ...
import java.util.ArrayList;import java.util.HashMap; import android.app.Activity;import android.database....import android.provider.ContactsContract.CommonDataKinds.Phone;import android.widget.ListVie
intent.putExtra(android.provider.ContactsContract.Intents.Insert.COMPANY, company); intent.putExtra(android.provider.ContactsContract.Intents.Insert.PHONE, tel); startActivity(intent); ``` 在上面的...
代码很简单,就不多废话了 代码如下: //读取联系人 public static Uri CONTACTSURI = ... String[] projections = new String[]{ContactsContract.Contacts._ID,ContactsContract.Contacts.DISPLAY_NAME}; Curso
pickContactIntent.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_TYPE); startActivityForResult(pickContactIntent, PICK_CONTACT_REQUEST); } @Override protected void onActivityResult(int ...
An application that demonstrates how to query the system contacts provider using the ContactsContract API, as well as insert contacts into a specific account. Home A home screen replacement ...
import java.util.ArrayList; import java.util.List; import android.app.Activity; import android.database.Cursor; import android.database.DataSetObserver;... import android.provider.ContactsContract
The ContactsContract and CallLog Providers The CalendarContract Provider The MediaStore Provider Consuming Documents Providing Documents Encrypted Storage Packaging and Distributing Data Advanced ...