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é)果集的游標(biāo)Cursor。
插入:使用ContentResolver.insert()方法向ContentProvide中增加一個新的記錄時,需要先將新紀(jì)錄的數(shù)據(jù)封裝到ContentValues對象中,然后調(diào)用ContentResolver.insert()方法將返回一個URI,該URI內(nèi)容是由ContentProvider的URI加上該新紀(jì)錄的擴展ID得到的,可以通過該URI對該記錄做進(jìn)一步的操作。
刪除:如果要刪除單個記錄,可以調(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)閉游標(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)實現(xiàn)效果:
在聯(lián)系人中添加幾個聯(lián)系人:
運行程序,手機里的所有聯(lián)系人的ID及名字就會記錄下來:
運行程序,手機里的所有聯(lián)系人的ID及名字就會記錄下來:
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android動效Compose貝塞爾曲線動畫規(guī)格詳解
這篇文章主要為大家介紹了Android動效Compose貝塞爾曲線動畫規(guī)格詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-11-11Android開發(fā)實現(xiàn)切換主題及換膚功能示例
這篇文章主要介紹了Android開發(fā)實現(xiàn)切換主題及換膚功能,涉及Android界面布局與樣式動態(tài)操作相關(guān)實現(xiàn)技巧,需要的朋友可以參考下2019-03-03Android Studio使用Profiler來完成內(nèi)存泄漏的定位
這篇文章主要介紹了Android Studio使用Profiler來完成內(nèi)存泄漏的定位,幫助大家更好的理解和學(xué)習(xí)使用Android,感興趣的朋友可以了解下2021-03-03Android開發(fā)使用RecyclerView添加點擊事件實例詳解
這篇文章主要為大家介紹了Android開發(fā)使用RecyclerView添加點擊事件實例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-08-08Android使用廣播(BroadCast)實現(xiàn)強制下線的方法
這篇文章主要介紹了Android使用廣播(BroadCast)實現(xiàn)強制下線的方法,實例分析了Android廣播BroadCast控制activity關(guān)閉的具體步驟與實現(xiàn)技巧,需要的朋友可以參考下2016-01-01Android SQLite數(shù)據(jù)庫加密的操作方法
因為Android自帶的SQLite數(shù)據(jù)庫本身是沒有實現(xiàn)加密的,那我們?nèi)绾螌崿F(xiàn)對數(shù)據(jù)庫的加密呢?今天通過本文給大家介紹下Android SQLite數(shù)據(jù)庫加密的操作方法,一起看看吧2021-09-09