访问联系人姓名,电话号码和电子邮件ID? [英] Accessing the Contact name, Number and Email ID?

查看:74
本文介绍了访问联系人姓名,电话号码和电子邮件ID?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用以下代码访问联系人姓名和电子邮件ID:

I am trying to access the contact name number and email ID using this code :

import android.app.Activity;
import android.content.ContentResolver;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.ContactsContract;

public class GetAllDatas extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    readContacts();
}   

private void readContacts() {
    // TODO Auto-generated method stub

    ContentResolver cr = getContentResolver();
    Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,
           null, null, null, null);
    if (cur.getCount() > 0) {
        while (cur.moveToNext()) {
            String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
            String name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
            if (Integer.parseInt(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
                System.out.println("name : " + name + ", ID : " + id);

                    // get the <span id="IL_AD1" class="IL_AD">phone number</span>


                Cursor pCur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,
                                       ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?",
                                       new String[]{id}, null);
                while (pCur.moveToNext()) {
                      String phone = pCur.getString(
                             pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                      System.out.println("phone" + phone);
                }
                pCur.close();

                // get email and type


                Cursor emailCur = cr.query(
                         ContactsContract.CommonDataKinds.Email.CONTENT_URI,
                         null,
                         ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = ?",
                         new String[]{id}, null);
                 while (emailCur.moveToNext()) {
                     // This would allow you get several email addresses
                         // if the email addresses were stored in an array
                     String email = emailCur.getString(
                                   emailCur.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
                     String emailType = emailCur.getString(
                                   emailCur.getColumnIndex(ContactsContract.CommonDataKinds.Email.TYPE));

                   System.out.println("Email " + email + " Email Type : " + emailType);
                 }
                 emailCur.close();
            }
        }
    }

}
    }

当我运行此代码时,我的活动中将不会显示任何内容.

When I run this code nothing would be displayed in my activity.

我需要在活动窗口的整个联系人列表中访问联系人姓名,电话号码和电子邮件ID.

I need to access Contact name, number and email ID from my entire contact list on the activity window.

推荐答案

对此进行检查

   package stack.examples;

import java.util.ArrayList;
import android.app.Activity;
import android.content.ContentResolver;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.provider.ContactsContract.CommonDataKinds.Email;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;

public class StackOverFlowGetContactsActivity extends Activity {

    ListView lvItem;
    private Button btnAdd;
    String displayName="", emailAddress="", phoneNumber="";
    ArrayList<String> contactlist=new ArrayList<String>();
    ArrayAdapter<String> itemAdapter;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       lvItem = (ListView)this.findViewById(R.id.listView_items);  
       btnAdd = (Button)this.findViewById(R.id.btnAddItem);
       itemAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,contactlist);
       lvItem.setAdapter(itemAdapter);
       btnAdd.setOnClickListener(new View.OnClickListener() {
           public void onClick(View v) {
               readContacts();
           }
       });
    }

    private void readContacts()
    {
        ContentResolver cr =getContentResolver();
        Cursor cursor = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
        while (cursor.moveToNext()) 
        {
            displayName="";emailAddress=""; phoneNumber="";
            displayName = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));       
            String id = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
            Cursor emails = cr.query(Email.CONTENT_URI,null,Email.CONTACT_ID + " = " + id, null, null);
            while (emails.moveToNext()) 
            { 
                emailAddress = emails.getString(emails.getColumnIndex(Email.DATA));
                break;
            }
            emails.close();
            if(Integer.parseInt(cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0)
            {
                Cursor pCur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?",new String[]{id}, null);
                while (pCur.moveToNext()) 
                {
                     phoneNumber = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                    break;
                }
                pCur.close();
            }
                contactlist.add("DisplayName: "+displayName+", PhoneNumber: "+phoneNumber+", EmailAddress: "+ emailAddress+"\n");
                itemAdapter.notifyDataSetChanged();
        }
        cursor.close(); 
    }
}

main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <ListView
        android:id="@+id/listView_items"
        android:layout_width="match_parent"
        android:layout_height="288dp"
        android:layout_weight="0.03" >

    </ListView>

    <Button
        android:id="@+id/btnAddItem"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="0.01"
        android:text="@string/add" />

</LinearLayout>

这篇关于访问联系人姓名,电话号码和电子邮件ID?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆