Android小程序?qū)崿F(xiàn)訪問聯(lián)系人
本文實(shí)例為大家分享了Android實(shí)現(xiàn)訪問聯(lián)系人的具體代碼,供大家參考,具體內(nèi)容如下
要求:
編寫程序,使用ContentProvider實(shí)現(xiàn)訪問聯(lián)系人
ContentProvider類的作用:
ContentProvider(內(nèi)容提供器)是所有應(yīng)用程序之間數(shù)據(jù)存儲(chǔ)和檢索的一個(gè)橋梁,其作用是是各個(gè)應(yīng)用程序之間能共享數(shù)據(jù);主要功能是存儲(chǔ)、檢索數(shù)據(jù)并向應(yīng)用程序提供訪問數(shù)據(jù)的接口。
基本操作:
查詢:使用ContentResolver的query()方法查詢數(shù)據(jù)與 SQLite查詢一樣,返回一個(gè)指向結(jié)果集的游標(biāo)Cursor。
插入:使用ContentResolver.insert()方法向ContentProvide中增加一個(gè)新的記錄時(shí),需要先將新紀(jì)錄的數(shù)據(jù)封裝到ContentValues對(duì)象中,然后調(diào)用ContentResolver.insert()方法將返回一個(gè)URI,該URI內(nèi)容是由ContentProvider的URI加上該新紀(jì)錄的擴(kuò)展ID得到的,可以通過該URI對(duì)該記錄做進(jìn)一步的操作。
刪除:如果要?jiǎng)h除單個(gè)記錄,可以調(diào)用ContentResolver.delete()方法,通過給該方法傳遞一個(gè)特定行的URI參數(shù)來實(shí)現(xiàn)刪除操作。如果要對(duì)多行記錄執(zhí)行刪除操作,就需要給delete()方法傳遞需要被刪除的記錄類型的URI以及一個(gè)where子句來實(shí)現(xiàn)多行刪除。
更新:使用ContentResolver.update()方法實(shí)現(xiàn)記錄的更新操作。
實(shí)現(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的對(duì)象 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對(duì)象 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)閉游標(biāo) 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)實(shí)現(xiàn)效果:
在聯(lián)系人中添加幾個(gè)聯(lián)系人:
運(yùn)行程序,手機(jī)里的所有聯(lián)系人的ID及名字就會(huì)記錄下來:
運(yùn)行程序,手機(jī)里的所有聯(lián)系人的ID及名字就會(huì)記錄下來:
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android 中兩個(gè)Activity 之間的傳值問題
這篇文章主要介紹了Android 中兩個(gè)Activity 之間的傳值問題的相關(guān)資料,需要的朋友可以參考下2017-08-08Android動(dòng)效Compose貝塞爾曲線動(dòng)畫規(guī)格詳解
這篇文章主要為大家介紹了Android動(dòng)效Compose貝塞爾曲線動(dòng)畫規(guī)格詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-11-11Android開發(fā)實(shí)現(xiàn)切換主題及換膚功能示例
這篇文章主要介紹了Android開發(fā)實(shí)現(xiàn)切換主題及換膚功能,涉及Android界面布局與樣式動(dòng)態(tài)操作相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2019-03-03Android控件動(dòng)態(tài)用法實(shí)例分析
這篇文章主要介紹了Android控件動(dòng)態(tài)用法,以實(shí)例形式較為詳細(xì)的分析了Android控件動(dòng)態(tài)的具體編程實(shí)現(xiàn)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-10-10Android Studio使用Profiler來完成內(nèi)存泄漏的定位
這篇文章主要介紹了Android Studio使用Profiler來完成內(nèi)存泄漏的定位,幫助大家更好的理解和學(xué)習(xí)使用Android,感興趣的朋友可以了解下2021-03-03Android開發(fā)使用RecyclerView添加點(diǎn)擊事件實(shí)例詳解
這篇文章主要為大家介紹了Android開發(fā)使用RecyclerView添加點(diǎn)擊事件實(shí)例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-08-08Android使用廣播(BroadCast)實(shí)現(xiàn)強(qiáng)制下線的方法
這篇文章主要介紹了Android使用廣播(BroadCast)實(shí)現(xiàn)強(qiáng)制下線的方法,實(shí)例分析了Android廣播BroadCast控制activity關(guān)閉的具體步驟與實(shí)現(xiàn)技巧,需要的朋友可以參考下2016-01-01Android SQLite數(shù)據(jù)庫加密的操作方法
因?yàn)锳ndroid自帶的SQLite數(shù)據(jù)庫本身是沒有實(shí)現(xiàn)加密的,那我們?nèi)绾螌?shí)現(xiàn)對(duì)數(shù)據(jù)庫的加密呢?今天通過本文給大家介紹下Android SQLite數(shù)據(jù)庫加密的操作方法,一起看看吧2021-09-09