欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android小程序?qū)崿F(xiàn)訪問聯(lián)系人

 更新時(shí)間:2020年05月22日 16:49:28   作者:adorable_  
這篇文章主要為大家詳細(xì)介紹了Android小程序?qū)崿F(xiàn)訪問聯(lián)系人,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一

本文實(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)文章

最新評(píng)論