Android 獲取手機(jī)聯(lián)系人實(shí)例代碼詳解
我的風(fēng)格,廢話不多說了,直接給大家貼代碼了。
具體代碼如下所示:
package com.org.demo.demo; import com.org.wangfeng.R; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.EditText; public class GetPhoneActivity extends Activity { private EditText editText; private Button button; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.getphoneactivity); editText = (EditText) findViewById(R.id.et_getphone); button = (Button) findViewById(R.id.bb_getphone); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub Intent intent = new Intent(GetPhoneActivity.this, ContactActivity.class); startActivityForResult(intent, 1); } }); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { // System.out.println("resultCode:" + resultCode); // System.out.println("requestCode:" + requestCode); Log.d("jiejie", "被調(diào)用了"); if (resultCode == Activity.RESULT_OK) { String phone = data.getStringExtra("phone"); Log.d("jiejie", "_______________"+phone); phone = phone.replaceAll("-", "").replaceAll(" ", "");// 替換-和空格 editText.setText(phone);// 把電話號(hào)碼設(shè)置給輸入框 } super.onActivityResult(requestCode, resultCode, data); } } package com.org.demo.demo; import java.util.ArrayList; import java.util.HashMap; import com.org.wangfeng.R; import android.app.Activity; import android.content.Intent; import android.database.Cursor; import android.net.Uri; import android.os.Bundle; import android.view.View; import android.widget.AdapterView; import android.widget.ListView; import android.widget.SimpleAdapter; import android.widget.AdapterView.OnItemClickListener; import android.widget.TextView; public class ContactActivity extends Activity { private ListView lvList; private ArrayList<HashMap<String, String>> readContact; private TextView back; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_contact); back =(TextView)findViewById(R.id.back); back.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub finish(); } }); lvList = (ListView) findViewById(R.id.lv_list); readContact = readContact(); // System.out.println(readContact); lvList.setAdapter(new SimpleAdapter(this, readContact, R.layout.contact_list_item, new String[] { "name", "phone" }, new int[] { R.id.tv_name, R.id.tv_phone })); lvList.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { String phone = readContact.get(position).get("phone");// 讀取當(dāng)前item的電話號(hào)碼 Intent intent = new Intent(); intent.putExtra("phone", phone); setResult(Activity.RESULT_OK, intent);// 將數(shù)據(jù)放在intent中返回給上一個(gè)頁面 finish(); } }); } private ArrayList<HashMap<String, String>> readContact() { // 首先,從raw_contacts中讀取聯(lián)系人的id("contact_id") // 其次, 根據(jù)contact_id從data表中查詢出相應(yīng)的電話號(hào)碼和聯(lián)系人名稱 // 然后,根據(jù)mimetype來區(qū)分哪個(gè)是聯(lián)系人,哪個(gè)是電話號(hào)碼 Uri rawContactsUri = Uri .parse("content://com.android.contacts/raw_contacts"); Uri dataUri = Uri.parse("content://com.android.contacts/data"); ArrayList<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>(); // 從raw_contacts中讀取聯(lián)系人的id("contact_id") Cursor rawContactsCursor = getContentResolver().query(rawContactsUri, new String[] { "contact_id" }, null, null, null); if (rawContactsCursor != null) { while (rawContactsCursor.moveToNext()) { String contactId = rawContactsCursor.getString(0); // System.out.println(contactId); // 根據(jù)contact_id從data表中查詢出相應(yīng)的電話號(hào)碼和聯(lián)系人名稱, 實(shí)際上查詢的是視圖view_data Cursor dataCursor = getContentResolver().query(dataUri, new String[] { "data1", "mimetype" }, "contact_id=?", new String[] { contactId }, null); if (dataCursor != null) { HashMap<String, String> map = new HashMap<String, String>(); while (dataCursor.moveToNext()) { String data1 = dataCursor.getString(0); String mimetype = dataCursor.getString(1); // System.out.println(contactId + ";" + data1 + ";" // + mimetype); if ("vnd.android.cursor.item/phone_v2".equals(mimetype)) { map.put("phone", data1); } else if ("vnd.android.cursor.item/name" .equals(mimetype)) { map.put("name", data1); } } list.add(map); dataCursor.close(); } } rawContactsCursor.close(); } return list; } }
本段代碼到此結(jié)束,代碼比較簡單,并附有注釋,希望對大家學(xué)習(xí)android獲取手機(jī)聯(lián)系人相關(guān)知識(shí)有所幫助。
- Android ContentProvider實(shí)現(xiàn)手機(jī)聯(lián)系人讀取和插入
- Android讀取手機(jī)通訊錄聯(lián)系人到自己項(xiàng)目
- android仿微信聯(lián)系人索引列表功能
- Android保存聯(lián)系人到通訊錄的方法
- android如何獲取聯(lián)系人所有信息
- Android使用AsyncQueryHandler實(shí)現(xiàn)獲取手機(jī)聯(lián)系人功能
- Android ContentProvider實(shí)現(xiàn)獲取手機(jī)聯(lián)系人功能
- android實(shí)現(xiàn)讀取、搜索聯(lián)系人的代碼
- Android ContentProvider獲取手機(jī)聯(lián)系人實(shí)例
- Android小程序?qū)崿F(xiàn)訪問聯(lián)系人
相關(guān)文章
Android開發(fā)實(shí)現(xiàn)根據(jù)包名判斷App運(yùn)行狀態(tài)的方法
這篇文章主要介紹了Android開發(fā)實(shí)現(xiàn)根據(jù)包名判斷App運(yùn)行狀態(tài)的方法,結(jié)合實(shí)例形式分析了Android結(jié)合包名判斷app運(yùn)行狀態(tài)的方法,需要的朋友可以參考下2017-11-11Android開發(fā)之組件GridView簡單使用方法示例
這篇文章主要介紹了Android開發(fā)之組件GridView簡單使用方法,涉及Android GridView組件圖片瀏覽及保存圖片等相關(guān)操作技巧,需要的朋友可以參考下2019-03-03利用Android實(shí)現(xiàn)一種點(diǎn)贊動(dòng)畫效果的全過程
最近做項(xiàng)目需要實(shí)現(xiàn)點(diǎn)贊動(dòng)畫,下面這篇文章主要給大家介紹了關(guān)于利用Android實(shí)現(xiàn)一種點(diǎn)贊動(dòng)畫效果的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-12-12Kotlin開發(fā)中open關(guān)鍵字與類名函數(shù)名和變量名的使用方法淺析
這篇文檔中,我們將解釋如何以及為什么將 open 關(guān)鍵字與類名、函數(shù)名和變量名一起使用,了解內(nèi)部原理是為了幫助我們做擴(kuò)展,同時(shí)也是驗(yàn)證了一個(gè)人的學(xué)習(xí)能力,如果你想讓自己的職業(yè)道路更上一層樓,這些底層的東西你是必須要會(huì)的2023-02-02Android 數(shù)據(jù)庫文件存取至儲(chǔ)存卡的方法
這篇文章主要介紹了Android 數(shù)據(jù)庫文件存取至儲(chǔ)存卡的方法的相關(guān)資料,需要的朋友可以參考下2016-03-03Android入門之ActivityGroup+GridView實(shí)現(xiàn)Tab分頁標(biāo)簽的方法
這篇文章主要介紹了Android入門之ActivityGroup+GridView實(shí)現(xiàn)Tab分頁標(biāo)簽的方法,非常實(shí)用的功能,需要的朋友可以參考下2014-08-08android開發(fā)中ListView與Adapter使用要點(diǎn)介紹
項(xiàng)目用到ListView,由于要用到 ImageView ,圖片源不是在資源里面的,沒法使用資源 ID,因此無法直接使用SimpleAdapter,要自己寫一個(gè)Adapter。 在使用ListView和Adapter需要注意以下幾點(diǎn)2013-06-06創(chuàng)建Android庫的方法及Android .aar文件用法小結(jié)
本文給大家介紹了創(chuàng)建Android庫的方法及Android中 .aar文件生成方法與用法詳解,涉及到創(chuàng)建庫模塊操作步驟及開發(fā)注意事項(xiàng),需要的朋友參考下吧2017-12-12Android Studio和Gradle使用不同位置JDK的問題解決
這篇文章主要介紹了Android Studio和Gradle使用不同位置JDK的問題解決,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-03-03Android開發(fā)之a(chǎn)ndroid_gps定位服務(wù)簡單實(shí)現(xiàn)
這篇文章主要介紹了Android開發(fā)之a(chǎn)ndroid_gps定位服務(wù)簡單實(shí)現(xiàn) ,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-04-04