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

发布于 2024-11-15 10:13:29 字数 203 浏览 3 评论 0原文


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

提前致谢...


I am new to android.I want to use system built in contacts management facility that android provides.I have searched a lot but couldn't get an exact idea that how to proceed for that.So please help me about how to add contacts from code(and not from GUI),how to access them in my application(simply to show all records in textview).
Is it possible to do this without using SQLite or file operations (means just reading and writing in system memory) ???

Thanks in advance...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

蹲在坟头点根烟 2024-11-22 10:13:29

看看这段代码!

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();

        }
    }

}



}
}

Have a look at this code!

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();


        }
     });

after returning from contacts screen,

@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();

        }
    }

}



}
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文