关闭

Android 之 获取联系人信息

470人阅读 评论(0) 收藏 举报
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.ethan.activity"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="7" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:name=".ContactActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
       
    </application>
	<uses-permission android:name="android.permission.READ_CONTACTS"/>
</manifest>


package com.ethan.activity;

import java.util.ArrayList;
import java.util.List;

import android.app.ListActivity;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class ContactActivity extends ListActivity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        Uri contactsUri = ContactsContract.Contacts.CONTENT_URI;
        
        String[] projection = new String[] {ContactsContract.Contacts.DISPLAY_NAME,
        		ContactsContract.Contacts.HAS_PHONE_NUMBER,
        		ContactsContract.Contacts.LOOKUP_KEY};
        
        Cursor cursor = getContentResolver().query(contactsUri, projection, null, null, null);
        
        List<String> contactsList = new ArrayList<String>();
        
        String allPhoneNo = "";
        if(cursor.getCount()>0) {
        	while(cursor.moveToNext()) {
        		if(cursor.getInt(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))>0) {
        			allPhoneNo=getAllPhoneNumbers(cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.LOOKUP_KEY)));
        		}
        		
        		contactsList.add(cursor.getString(0)+" , "+allPhoneNo);
        		allPhoneNo = "";
        	}
        }
        
        // binding the data to ListView 
        setListAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, contactsList));
      /*  ListView lv = getListView();
        lv.setTextFilterEnabled(true);*/
    }

	private String getAllPhoneNumbers(String lookUp_Key) {
		String allPhoneNo = "";
		
		Uri phoneUri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
		String[] projection2 = {ContactsContract.CommonDataKinds.Phone.NUMBER};
		 // using lookUp key to search the phone numbers
		String selection = ContactsContract.Data.LOOKUP_KEY+"=?";
		String[] selectionArgs = {lookUp_Key};
		
		Cursor cur = getContentResolver().query(phoneUri, projection2, selection, selectionArgs, null);
		
		while(cur.moveToNext()) {
			allPhoneNo += cur.getString(0)+" ";
		}
		return allPhoneNo;
	}
}


0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:628055次
    • 积分:7207
    • 等级:
    • 排名:第3211名
    • 原创:183篇
    • 转载:80篇
    • 译文:0篇
    • 评论:123条
    文章分类
    最新评论