2013/11/03

Android 取得電話簿姓名及號碼


AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.cy.listview"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />
    <uses-permission android:name="android.permission.READ_CONTACTS"/>

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.cy.listview.MainActivity"
            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>

</manifest>

Layout:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <ListView
        android:id="@+id/listView1"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    </ListView>

</RelativeLayout>



Code:
package com.cy.listview;

import android.app.Activity;
import android.content.ContentResolver;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class MainActivity extends Activity {

 private ListView listView;

 // 橋接
 private ArrayAdapter<String> adapter;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  listView = (ListView) findViewById(R.id.listView1);

  // 橋接器的視覺
  adapter = new ArrayAdapter<String>(this,
    android.R.layout.simple_list_item_1);

  // 透過走訪將值帶入橋接器
  for (String s : getPhoneBook()) {
   adapter.add(s);
  }

  // 設定橋接給ListView
  listView.setAdapter(adapter);
 }

 private String[] getPhoneBook() {
  // 電話簿
  String[] phoneBook;

  // 取得裝置上內容
  ContentResolver contentResolver = getContentResolver();

  // 指到電話簿
  Cursor cursor = contentResolver.query(
    ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null,
    null, null);

  // 取得電話簿數量
  phoneBook = new String[cursor.getColumnCount()];

  for (int index = 0; index < phoneBook.length; index++) {

   // 持續走訪
   cursor.moveToNext();

   // 取得姓名
   String name = cursor
     .getString(cursor
       .getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));

   // 取得電話
   String number = cursor
     .getString(cursor
       .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));

   // 將兩者拼湊起來
   phoneBook[index] = name + "\t" + number;
  }

  // 回傳值
  return phoneBook;

 }
}



參考資料:
http://developer.android.com/reference/android/provider/ContactsContract.html
http://developer.android.com/reference/android/provider/ContactsContract.Contacts.html
http://developer.android.com/reference/android/provider/ContactsContract.CommonDataKinds.Phone.html
http://developer.android.com/guide/topics/ui/layout/listview.html
http://developer.android.com/reference/android/widget/ListView.html