Android小程序?qū)崿F(xiàn)訪問聯(lián)系人
本文實例為大家分享了Android實現(xiàn)訪問聯(lián)系人的具體代碼,供大家參考,具體內(nèi)容如下
要求:
編寫程序,使用ContentProvider實現(xiàn)訪問聯(lián)系人
ContentProvider類的作用:
ContentProvider(內(nèi)容提供器)是所有應(yīng)用程序之間數(shù)據(jù)存儲和檢索的一個橋梁,其作用是是各個應(yīng)用程序之間能共享數(shù)據(jù);主要功能是存儲、檢索數(shù)據(jù)并向應(yīng)用程序提供訪問數(shù)據(jù)的接口。
基本操作:
查詢:使用ContentResolver的query()方法查詢數(shù)據(jù)與 SQLite查詢一樣,返回一個指向結(jié)果集的游標Cursor。
插入:使用ContentResolver.insert()方法向ContentProvide中增加一個新的記錄時,需要先將新紀錄的數(shù)據(jù)封裝到ContentValues對象中,然后調(diào)用ContentResolver.insert()方法將返回一個URI,該URI內(nèi)容是由ContentProvider的URI加上該新紀錄的擴展ID得到的,可以通過該URI對該記錄做進一步的操作。
刪除:如果要刪除單個記錄,可以調(diào)用ContentResolver.delete()方法,通過給該方法傳遞一個特定行的URI參數(shù)來實現(xiàn)刪除操作。如果要對多行記錄執(zhí)行刪除操作,就需要給delete()方法傳遞需要被刪除的記錄類型的URI以及一個where子句來實現(xiàn)多行刪除。
更新:使用ContentResolver.update()方法實現(xiàn)記錄的更新操作。
實現(xiàn)方案:
(1)CPActivity.java程序代碼如下:
package com.example.contentprovider;
import android.app.Activity;
import android.content.ContentResolver;
import android.database.Cursor;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract.Contacts;
import android.widget.TextView;
public class CPActivity extends Activity {
Uri contact_uri = Contacts.CONTENT_URI;//聯(lián)系人的URI
//聲明TextView的對象
TextView textview;
//定義文本顏色
int textcolor = Color.BLACK;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//根據(jù)main.xml設(shè)置程序UI
setContentView(R.layout.activity_cp);
textview = (TextView)findViewById(R.id.textview);
//調(diào)用getContactInfo()方法獲取聯(lián)系人信息
String result = getContactInfo();
//設(shè)置文本框的顏色
textview.setTextColor(textcolor);
//定義字體大小
textview.setTextSize(20.0f);
//設(shè)置文本框的文本
textview.setText("記錄\t 名字\n"+result);
}
//getContactInfo()獲取聯(lián)系人列表的信息,返回String對象
public String getContactInfo() {
// TODO Auto-generated method stub
String result = "";
ContentResolver resolver = getContentResolver();
Cursor cursor = resolver.query(contact_uri, null, null, null, null);
//獲取_ID字段索引
int idIndex = cursor.getColumnIndex(Contacts._ID);
//獲取name字段的索引
int nameIndex = cursor.getColumnIndex(Contacts.DISPLAY_NAME);
//遍歷Cursor提取數(shù)據(jù)
cursor.moveToFirst();
for(;!cursor.isAfterLast();cursor.moveToNext()){
result = result+cursor.getString(idIndex)+"\t\t\t";
result = result+cursor.getString(nameIndex)+"\t\n";
}
//使用close方法關(guān)閉游標
cursor.close();
//返回結(jié)果
return result;
}
}
(2)Activity_cp.xml代碼如下:
<LinearLayout 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"
tools:context="${relativePackage}.${activityClass}" >
<TextView
android:id="@+id/textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
(3)其次必須在AndroidManifest.xml中添加如下權(quán)限:
<uses-permission android:name="android.permission.READ_CONTACTS" />
(4)實現(xiàn)效果:
在聯(lián)系人中添加幾個聯(lián)系人:
運行程序,手機里的所有聯(lián)系人的ID及名字就會記錄下來:

運行程序,手機里的所有聯(lián)系人的ID及名字就會記錄下來:

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android TextSwitcher實現(xiàn)文字上下翻牌效果(銅板街)
這篇文章主要介紹了Android TextSwitcher實現(xiàn)文字上下翻牌效果(銅板街),需要的朋友可以參考下2017-05-05
Android System fastboot 介紹和使用教程
Fastboot是Android快速升級的一種方法,Fastboot的協(xié)議fastboot_protocol.txt在源碼目錄./bootable/bootloader/legacy下可以找到,本文給大家介紹Android System fastboot 介紹和使用教程,感興趣的朋友一起看看吧2024-01-01
Android編程實現(xiàn)仿優(yōu)酷旋轉(zhuǎn)菜單效果(附demo源碼)
這篇文章主要介紹了Android編程實現(xiàn)仿優(yōu)酷旋轉(zhuǎn)菜單效果的方法,較為詳細的分析了Android實現(xiàn)旋轉(zhuǎn)菜單的布局與功能實現(xiàn)技巧,并附帶完整的demo源碼供讀者下載參考,需要的朋友可以參考下2015-12-12
flutter TextField換行自適應(yīng)的實現(xiàn)
這篇文章主要介紹了flutter TextField換行自適應(yīng)的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習或者工作具有一定的參考學(xué)習價值,需要的朋友們下面隨著小編來一起學(xué)習學(xué)習吧2020-02-02
c++ mk文件出錯Jni調(diào)用產(chǎn)生java.lang.UnsatisfiedLinkError錯誤解決方法
錯誤產(chǎn)生在我把方法從c語言轉(zhuǎn)為c++語言后產(chǎn)生的,后來檢查到這種錯誤是因為mk文件出錯,加載c文件和加載c++的文件所用的代碼不一樣,下面請看2013-11-11

