Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> Android開發 >> 關於android開發 >> andriod 讀取通訊錄,andriod通訊錄

andriod 讀取通訊錄,andriod通訊錄

編輯:關於android開發

andriod 讀取通訊錄,andriod通訊錄


package com.example.yanlei.wifi;

import android.content.ContentResolver;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.provider.ContactsContract.PhoneLookup;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

private ListView lvContacts=null;
private Cursor cursor=null;
private Button btnRead=null;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView tv = new TextView(this);
String str = "";
// 得到contentresolver對象
ContentResolver cr = getContentResolver();
// 取得電話本中開始一項的光標,必須先moveToNext()
Cursor cursor = cr.query(ContactsContract.Contacts.CONTENT_URI, null,
null, null, null);
while (cursor.moveToNext())
{
//取得聯系人的名字索引
int nameIndex = cursor.getColumnIndex(PhoneLookup.DISPLAY_NAME);
String contact = cursor.getString(nameIndex);
str += (contact + ":" + "\n");
//取得聯系人的ID索引值
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 strPhoneNumber = phone
.getString(phone
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
str += (strPhoneNumber + "\n");
Log.i("TAG","phoneNumber:"+strPhoneNumber+"contact"+contact);
}
phone.close();
}
cursor.close();
tv.setText(str);
setContentView(tv);

}



}

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved