Welcome 微信登录

首页 / 操作系统 / Linux / Android SDK 2.0以上读取手机联系人

Android SDK 2.0以上的版本对联系人相关的API做了很大的调整,因此在使用时与1.6版本的差异也较大。具体读取联系人及号码,以ListView方式显示,代码如下。
public class ReadContacts extends Activity {    ListView listView;    ListAdapter adapter;    @Override    public void onCreate(Bundle savedInstanceState) {       super.onCreate(savedInstanceState);       LinearLayout linearLayout = new LinearLayout(this);       linearLayout.setOrientation(LinearLayout.VERTICAL);       linearLayout.setBackgroundColor(Color.BLACK);       LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(              LinearLayout.LayoutParams.FILL_PARENT             LinearLayout.LayoutParams.WRAP_CONTENT);        listView = new ListView(this);       listView.setBackgroundColor(Color.BLACK);       linearLayout.addView(listView, param);
       this.setContentView(linearLayout);        // 生成动态数组,加入数据       ArrayList<HashMap<String, Object>> listItem = new ArrayList<HashMap<String, Object>>();       ArrayList<HashMap<String, Object>> listItemRead= newArrayList<HashMap<String, Object>>();       Cursor cursor = getContentResolver().query(              ContactsContract.Contacts.CONTENT_URI, null, null, null, null);       while(cursor.moveToNext()) {           HashMap<String, Object> map = new HashMap<String, Object>();           String phoneName = cursor.getString(cursor                  .getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));           map.put("ItemTitle", phoneName);// 电话姓名           String contactId = cursor.getString(cursor                  .getColumnIndex(ContactsContract.Contacts._ID));           String hasPhone = cursor                  .getString(cursor                         .getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));            if(hasPhone.compareTo("1") == 0) {              Cursor phones = getContentResolver().query(                     ContactsContract.CommonDataKinds.Phone.CONTENT_URI                    null                    ContactsContract.CommonDataKinds.Phone.CONTACT_ID                            + " = " + contactId, null, null);              while(phones.moveToNext()) {                  String phoneNumber = phones                         .getString(phones                                .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));                  String phoneTpye = phones                         .getString(phones                                .getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE));                  map.put("ItemText", phoneNumber); // 多个号码如何处理                          phones.close();                    listItem.add(map);             // 生成适配器的Item和动态数组对应的元素       SimpleAdapter listItemAdapter = new SimpleAdapter(this, listItem,// 数据源              android.R.layout.simple_list_item_2,// ListItem的XML实现              // 动态数组与ImageItem对应的子项              new String[] { "ItemTitle", "ItemText" },              new int[] { android.R.id.text1, android.R.id.text2 });       listView.setAdapter(listItemAdapter);       cursor.close();      }}