Use ContentProvider Get Contacts

简介:   UseContentProviderGetContactsActivity .java   package gogler.myandroid; import android.app.

 

UseContentProviderGetContactsActivity .java

 

package gogler.myandroid;

import android.app.Activity;
import android.content.ContentResolver;
import android.database.Cursor;

import android.os.Bundle;

import android.provider.ContactsContract;
import android.provider.ContactsContract.PhoneLookup;
import android.widget.TextView;

public class UseContentProviderGetContactsActivity extends Activity {
 /** Called when the activity is first created. */
 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  TextView tv = new TextView(this);
  String string = "contacts:\n";

  String number = "";
  /* 1、获取contentResolver */
  ContentResolver cr = this.getContentResolver();
  /* 2、获取一个游标查询对象 */
  Cursor cursor = cr.query(ContactsContract.Contacts.CONTENT_URI, null,
    null, null, null);
  /* 3、遍历contentResolver */
  while (cursor.moveToNext()) {
   /*
    * 获取联系人名字索引
    */
   int nameFieldColumnIndex = cursor
     .getColumnIndex(PhoneLookup.DISPLAY_NAME);
   /* 获取联系人名 */
   String contact = cursor.getString(nameFieldColumnIndex);
   /* 4、新建游标 */
   Cursor target = this.getContentResolver().query(
     ContactsContract.Contacts.CONTENT_URI, null, null, null,
     null);
   /* 5、定位游标到第一行 */
   target.moveToFirst();
   /* 6、得到联系人的ID */
   String contentId = cursor.getString(target
     .getColumnIndex(ContactsContract.Contacts._ID));
   /* 7、得到当前联系人有没有手机号码的值 */
   String isPnone = cursor
     .getString(cursor
       .getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));
   /* 8、如果当前联系人有电话号码 */
   if (Integer.parseInt(isPnone) > 0) {
    /* 9、新建游标-联系人ID */
    Cursor phone = this.getContentResolver().query(
      ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
      null,
      ContactsContract.CommonDataKinds.Phone.CONTACT_ID + "="
        + contentId, null, null);
    /* 10、遍历得到当前联系人的多个手机号码 */
    while (phone.moveToNext()) {
     // 得到联系人电话
     number = phone
       .getString(phone
         .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
     string += (contact + ":" + number + "\n");
    }
   }
  }
  tv.setText(string);
  setContentView(tv);

 }
}

 

manifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="gogler.myandroid"
    android:versionCode="1"
    android:versionName="1.0" >

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

    <application
        android:icon="@drawable/russia"
        android:label="@string/app_name" >
        <activity
            android:label="@string/app_name"
            android:name=".UseContentProviderGetContactsActivity" >
            <intent-filter >
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

 

代码打包下载地址

http://download.csdn.net/download/gogler/3725437

 

演示截图

 

 

 

相关文章
|
10月前
|
API 数据库 Android开发
Android ContentProvider内容提供者详解
Android ContentProvider内容提供者详解
47 2
|
10月前
|
Java Android开发
Android 四大组件之ContentProvider 访问通讯录进行增删改查操作
Android 四大组件之ContentProvider 访问通讯录进行增删改查操作
59 0
|
API 数据库 数据库管理
ContentProvider初探
ContentProvider初探
46 0
|
Android开发 iOS开发
Android6.0 源码修改之 Contacts应用
Android6.0 源码修改之 Contacts应用
68 0
|
数据库
ContentProvider
构建content URI public class TaskContract { /* COMPLETED (1) Add content provider constants to the Contract Clients need to know how to access the task data, and it's your job to provide these content URI's for the path to that data: 1) Content authority, 2) Base content
119 0
ContentProvider
|
Web App开发 数据库 Android开发
|
存储 Android开发 数据库管理