需要帮助来访问android中的系统内置联系人

时间:2011-06-15 10:57:19

标签: android android-layout android-contacts android-contentprovider


我是android的新手。我想使用android提供的系统内置的联系人管理工具。我已经搜索了很多,但无法确切知道如何继续。所以请帮助我如何从代码中添加联系人(而不是从GUI),如何在我的应用程序中访问它们(只是为了显示textview中的所有记录)。 是否可以在不使用SQLite或文件操作的情况下执行此操作(仅表示在系统内存中读取和写入)???

提前致谢...

1 个答案:

答案 0 :(得分:1)

看看这段代码!

btn_existing_contacts.setOnClickListener(new OnClickListener() {
        public void onClick(View v){
            Intent intent_contacts = new Intent(Intent.ACTION_PICK,ContactsContract.Contacts.CONTENT_URI);
            /*contacts.setAction(android.content.Intent.ACTION_VIEW);
            contacts.setData(People.CONTENT_URI);*/
            startActivityForResult(intent_contacts, 0);
            //displayContacts();


        }
     });

从联系人屏幕返回后

@Override
protected void onActivityResult(int requestCode, int resultCode,Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
String name,mailid,id;
switch(requestCode)
{
case 0:
{
    if(resultCode == RESULT_OK )
    {
        Uri contactdata  = data.getData();

        Cursor cur = managedQuery(contactdata, null, null, null, null);


        if(cur.moveToFirst())
        {
            id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
            Cursor emailCur = getContentResolver().query(ContactsContract.CommonDataKinds.Email.CONTENT_URI,null,
                    ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = ?",new String[]{id}, null); 
            emailCur.moveToFirst();
            String email = emailCur.getString(emailCur.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));


            name = cur.getString(cur.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME));
            //mailid = cur.getString(cur.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
            //mailid = cur.getString(cur.getColumnIndex(ContactsContract.CommonDataKinds.Email._ID));
            //Toast.makeText(context_contact, "Name:"+name+"\nmailid:"+email, Toast.LENGTH_SHORT).show();
            Intent intent_add_invitees = new Intent(<ClassContext>,<ur classname>.class);
            intent_add_invitees.putExtra("invitee_name", name);
            intent_add_invitees.putExtra("invitee_mailid", email);
            setResult(RESULT_OK, intent_add_invitees);
            finish();

        }
    }

}



}
}