Android 2.0 API 获取通讯录中电话号码的例子

转自:http://www.itivy.com/android/archive/2011/8/13/how-android-get-phone-number-from-contact-table.html

最近开始学习Android,主要看的是《Android应用开发揭秘》,在第3章的Example_03_02是一个读取通讯录联系人姓名和电话 的实例,但由于API 2.0中,每个联系人可以有多个电话(例如手机、住宅、公司、传真等),书中原有的实例在API 2.0的环境中会报错。

书中的Example_03_02代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import android.app.Activity;
import android.content.ContentResolver;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.provider.ContactsContract.PhoneLookup;
import android.widget.TextView;
 
public class Activity01 extends Activity
{
     public void onCreate(Bundle savedInstanceState)
     {
         TextView tv = new TextView( this );
         String string = "" ;       
         super .onCreate(savedInstanceState);   
         //得到ContentResolver对象
         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);  //此行会报错,返回值=-1 
             String number = cursor.getString(numberFieldColumnIndex);
             
             string += (contact+ ":" +number+ "\n" );
         }
         cursor.close();
         //设置TextView显示的内容
         tv.setText(string);
         //显示到屏幕
         setContentView(tv);
     }
}

参考网上的资料,找到了正确读取联系人电话的方法:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import android.app.Activity;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.provider.ContactsContract.PhoneLookup;
import android.database.Cursor;
import android.widget.TextView;
import android.content.ContentResolver;
 
public class Activity01 extends Activity
{
     public void onCreate(Bundle savedInstanceState)
     {
         TextView tv = new TextView( this );
         String string = "" ;
         super .onCreate(savedInstanceState);
         
         //得到ContentResolver对象
         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 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())
             {
                 String PhoneNumber = phone.getString(phone.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                 string += (contact + ":" + PhoneNumber + "\n" );
             }
         }
         cursor.close();
         
         //设置TextView显示的内容
         tv.setText(string);
         //显示到屏幕
         setContentView(tv);
     }
}

最后记得在AndroidManifest.xml声明读取联系人的API:

1
< uses-permission android:name = "android.permission.READ_CONTACTS" />

转载于:https://www.cnblogs.com/SuperJunior/archive/2012/04/11/2442645.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值