1. 程式人生 > >Android Contacts(一)—— 讀取聯絡人

Android Contacts(一)—— 讀取聯絡人

Introduction To Android Contacts

Learn to work with the Android contacts database. Basic knowledge of accessing SQLite in Android along with using Cursors is expected. See the Android SQLite and Cursor Article for more information. Google changed the contacts database moving from 1.x to 2.0 versions of Android. This tutorial will be broken into 3 sections. First covering accessing contacts in Android 2.0. The second page will deal with accessing the contacts in Android 1.6 and before. Third we'll glue it all together with a class that abstracts specific classes for each version and a set of classes to manage the data from the contact records.

Contacts 讀取程式碼:

[java] view plaincopyprint?
  1. package com.homer.phone;  
  2. import java.util.ArrayList;  
  3. import java.util.HashMap;  
  4. import android.app.Activity;  
  5. import android.database.Cursor;  
  6. import android.os.Bundle;  
  7. import android.provider.ContactsContract;  
  8. import android.provider.ContactsContract.CommonDataKinds.Phone;  
  9. import android.widget.ListView;  
  10. import android.widget.SimpleAdapter;  
  11. publicclass phoneRead extends Activity {  
  12.     @Override
  13.     publicvoid onCreate(Bundle savedInstanceState){  
  14.         super.onCreate(savedInstanceState);  
  15.         showListView();  
  16.     }  
  17.     privatevoid showListView(){  
  18.         ListView listView = new
     ListView(this);  
  19.         ArrayList<HashMap<String, String>> list = getPeopleInPhone2();  
  20.         SimpleAdapter adapter = new SimpleAdapter(  
  21.                                     this,   
  22.                                     list,   
  23.                                     android.R.layout.simple_list_item_2,   
  24.                                     new String[] {"peopleName""phoneNum"},   
  25.                                     newint[]{android.R.id.text1, android.R.id.text2}  
  26.                                 );  
  27.         listView.setAdapter(adapter);  
  28.         setContentView(listView);  
  29.     }  
  30.     private ArrayList<HashMap<String, String>> getPeopleInPhone2(){  
  31.         ArrayList<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>();  
  32.         Cursor cursor = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, nullnullnullnull);     // 獲取手機聯絡人
  33.         while (cursor.moveToNext()) {  
  34.             HashMap<String, String> map = new HashMap<String, String>();  
  35.             int indexPeopleName = cursor.getColumnIndex(Phone.DISPLAY_NAME);    // people name
  36.             int indexPhoneNum = cursor.getColumnIndex(Phone.NUMBER);            // phone number
  37.             String strPeopleName = cursor.getString(indexPeopleName);  
  38.             String strPhoneNum = cursor.getString(indexPhoneNum);  
  39.             map.put("peopleName", strPeopleName);  
  40.             map.put("phoneNum", strPhoneNum);  
  41.             list.add(map);  
  42.         }  
  43.         if(!cursor.isClosed()){  
  44.             cursor.close();  
  45.             cursor = null;  
  46.         }  
  47.         return list;  
  48.     }  
  49. }  

AndroidManifest.xml 許可權

記得在AndroidManifest.xml中加入android.permission.READ_CONTACTS這個permission

<uses-permission android:name="android.permission.READ_CONTACTS" />

執行結果:


程式碼示例

參考推薦:

相關推薦

Android Contacts—— 讀取聯絡人

Introduction To Android Contacts Learn to work with the Android contacts database. Basic knowledge of accessing SQLite in Android along

Android SMS —— 讀取簡訊

Android SMS Read package com.homer.sms; import java.sql.Date; import java.text.SimpleDateFormat; import android.app.Activity; import

Android SMS —— 讀取短信

andro @override 智能 tac db文件 沒有 end match art 分享一下我老師大神的人工智能教程吧。零基礎!通俗易懂!風趣幽默!還帶黃段子!希望你也加入到我們人工智能的隊伍中來!https://blog.csdn.net/jiangjunshow

Android 學習筆記 Contacts ContentResolver query 引數詳解

1.獲取聯絡人姓名 一個簡單的例子,這個函式獲取裝置上所有的聯絡人ID和聯絡人NAME。 public void fetchAllContacts() { ContentResolver contentResolver = this.getContentResolve

Python+OpenCV圖像處理——讀取顯示張圖片

沒有 class 釋放資源 圖像 Coding 路徑 troy 如果 nco   配置好所有環境後,開始利用python+opencv進行圖像處理第一步。   讀取和顯示一張圖片: import cv2 as cv src=cv.imread(‘E:\imageload\

Android MVC MVC簡介

mvc tar .com 程序 tro view 用戶界面 lis 代碼 今天有朋友和我提到Android中的MVC模式,自己就在這裏總結下,如有不妥,大家盡情批評指教 MVC框架簡介   MVC全名是Model View Controller,是模型(model)-視圖(

Android學習--安卓四大元件

 Android有四大元件,分別是Activity,Service,Content Provider和Broadcast Receiver。   首先說明Activity元件,開發一個應用程式不用到Activity是很難的,一個Activity通常就是一個獨立的視窗或

Android學習—— Android studio的安裝與配置

          最近事比較少,想著做一系列Android的學習,把自己當時遇到的一些整理一下,方便新學android的人學習 首先先來安裝編寫Android的軟體,我這裡用的是android studio Android studio安裝與配

Android動畫-檢視動畫

概述 Android開發中一直會遇到各種動畫效果,特別是如果老闆和UI妹子很扣這塊的話。這也是每一個Android程式設計師無法繞過的一塊內容,目前專案不忙,終於有時間來系統的整理下Android中的動畫。 Android中動畫分為: 幀動畫(Frame Anim),像幻

Android學習

分析 構建 新的 studio 字符串解析 ble 學習 nullable android學習 Android 系統架構 linux內核層 系統運行庫層 應用框架層 應用層 Android 的四大組件 活動(Activity) 服務(S

android開發

開始android歷程: 1、模擬器無法連線本地PC:       模擬器中可以連外網,但無法訪問本地PC上的IIS的API介面:報connect refused錯誤。      後試了很多方法,修改模擬器網址等,沒用。 &

Android GreenDAO

GreenDao是一個高效的資料庫訪問ORM框架,節省了自己編寫SQL的時間,快速的增刪查改等操作。 配置GreenDao 新增在工程的build.gradle // In your root build.gradle file: buildscript {

android-hybird

hybird混合開發是現在應用開發中非常常見的 1.成本低,展示豐富 有些公司的產品剛剛開始可能甚至只投入後h5的應用,也就是你開啟應用,都是h5頁面。為什麼呢,成本低啊 2.h5展示,更新方便 h5真是的web頁面,是後臺推送來的資料,由伺服器隨時更新,而應用端不用

TensorFlow詳解貓狗識別--讀取自己的資料集

資料集下載 連結: https://pan.baidu.com/s/1SlNAPf3NbgPyf93XluM7Fg 密碼: hpn4 資料集分別有12500張cat,12500張dog 讀取資料集 資料集的讀取,查閱了那麼多文件,大致瞭解到,資料集的讀取方法大概會分為兩種

Android備忘錄

1.獲取和設定螢幕的點選事件 input text 1234  向介面注入1234文字,有輸入框,可以明顯看到效果 input keyevent 4  向螢幕傳送鍵盤事件,4是返回 input tap 100 300 單機觸屏事件,模擬點選x=100,y=300位置 inp

react-native-android-unityreact-native加入android原生

 本人是做後端php的,對各類語言和方向都有興趣,多而不精。最近公司專案有一個移動端app,決定採用react-native開發,專案中有這麼個要求,要求react-native中嵌入原生頁面,然後原生頁面嵌入unity,並實現原生和unity之前相互通訊,網路查詢資料後實現

python3+opencv3 影象處理讀取顯示張圖片

  先在此處先宣告,後面學習python+opencv影象處理時均參考這位博主的博文https://blog.csdn.net/u011321546/article/category/7495016/2?,我只是復現和稍微修改一下程式碼,加深自己印象的同時也督促自己好好學習影

ReactNative 呼叫Android 原生——原生模組()

前言 React native呼叫Android原生主要2種方式: 1、呼叫原生模組 2、呼叫原生元件 這裡之所以強調有2種方式主要是自己剛開始弄rn調原生的時候感覺很懵,感覺沒有學習的方向,所以提醒一

ReactNative 呼叫Android 原生——原生模組(二)

ReactNative 呼叫Android 原生(一)——原生模組(一):https://blog.csdn.net/danfengw/article/details/83862623 在上次文章中我們只是簡單的嘗試了rn給Android原生傳遞引數呼叫Andr

Android例項—— 3D畫廊

3D畫廊 之前我都是寫的學習的內容,我在寫這些教程時遇到有趣的炫酷的小例子也會專門拿出來寫一篇文章,今天就寫一個酷炫的小例子,叫3D畫廊,它是屬於ViewPage的進階版。 下面的指示器是使用的一大神的第三方庫,會在文章下方簡單講述一下。 效果圖 3D畫廊的實現 首先是佈局檔案 1 <Frame