访问联系人和获得的电子邮件地址电子邮件地址

2023-09-13 00:14:55 作者:慕斯 こ

我有一个code snappet用于访问联系人。当用户点击该按钮,然后在联系人列表将是开放的,用户可以从通讯录中选择一个人,这个人的电子邮件地址应写在的EditText。我可以从人哪个用户选择接收电子邮件。但我不能将它设置为EditText上。

 静态字符串email =;


imgbtnaddfromcontacts.setOnClickListener(新OnClickListener(){
            @覆盖
            公共无效的onClick(视图v){
                如果(V == imgbtnaddfromcontacts){
                    尝试
                    {
                        意向意图=新的意图(Intent.ACTION_PICK,
                                ContactsContract.Contacts.CONTENT_URI);
                        startActivityForResult(意向,1);

                    }赶上(例外五){
                        e.printStackTrace();
                        Log.e(在意图错误:,e.toString());
                    }
                }
            }
        });
        kimeTxt.setText(电子邮件);
    }

    @覆盖
    公共无效onActivityResult(INT REQ code,INT结果code,意图数据){
        super.onActivityResult(REQ code,因此code,数据);

        尝试 {
            如果(结果code == Activity.RESULT_OK){
                //获取数据
                乌里联系数据= data.getData();
                // 光标
                光标CUR = managedQuery(联络人资料,NULL,NULL,NULL,NULL);
                ContentResolver的contect_resolver = getContentResolver();

                //名单
                如果(cur.moveToFirst()){
                    字符串ID = CUR
                            .getString(CUR
                                    .getColumnIndexOrThrow(ContactsContract.Contacts._ID));

                    光标phoneCur = contect_resolver.query(
                            ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                            空值,
                            ContactsContract.CommonDataKinds.Phone.CONTACT_ID
                                    +=?,新的String [] {ID},NULL);

                    光标emailCur = contect_resolver.query(
                            ContactsContract.CommonDataKinds.Email.CONTENT_URI,
                            空值,
                            ContactsContract.CommonDataKinds.Email.CONTACT_ID
                                    +=?,新的String [] {ID},NULL);

                    如果(phoneCur.moveToFirst()){
                        NAME = phoneCur
                                .getString(phoneCur
                                        .getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
                        无= phoneCur
                                .getString(phoneCur
                                        .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));

                    }

                    而(emailCur.moveToNext()){
                        //这将让你获得多个电子邮件地址
                        //如果电子邮件地址被存储在一个阵列
                        电子邮件= emailCur
                                .getString(emailCur
                                        .getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));

                        如果(电子邮件!= NULL)
                        {
                            seciliEmail =电子邮件;
                        } 其他 {
                            Toast.makeText(EpostaIletActivity.this,
                                    Kişininepostahesabıbulunmamaktadır。
                                    Toast.LENGTH_SHORT);
                            Log.w(错误:,Kişininepostahesabı郁慕明。);
                        }
                    }

                    Log.e(电话号码和放大器;名称和放大器;电子邮件:***:姓名+:+ NO +:+电子邮件);
                    // txt.append(名称+:+ NO +\ N);

                    的id =无效;
                    NAME = NULL;
                    无=无效;
                    seciliEmail =XXX;
                    phoneCur = NULL;
                    emailCur.close();
                }
                contect_resolver = NULL;
                CUR = NULL;
                // populateContacts();

            }
        }赶上(抛出:IllegalArgumentException E){
            e.printStackTrace();
            Log.e(抛出:IllegalArgumentException ::,e.toString());
        }赶上(例外五){
            e.printStackTrace();
            Log.e(错误::,e.toString());
        }
    }
 

解决方案

现在用低于code从选定的联系人收到的电子邮件地址 -

 公共无效doLaunchContactPicker(查看视图){
    意图contactPickerIntent =新的意图(Intent.ACTION_PICK,Contacts.CONTENT_URI);
    startActivityForResult(contactPickerIntent,CONTACT_PICKER_RESULT);
}

@覆盖
保护无效onActivityResult(INT申请code,INT结果code,意图数据)
{
    如果(结果code == RESULT_OK){
        开关(要求code)
        {
        案例CONTACT_PICKER_RESULT:
            光标光标= NULL;
            字符串email =,NAME =;
            尝试 {
                开放的结果= data.getData();
                Log.v(DEBUG_TAG,有一个接触的结果:+ result.toString());

                //从URI获得接触式ID
                字符串ID = result.getLastPathSegment();

                //查询所有电子邮件
                光标= getContentResolver()查询(Email.CONTENT_URI,空,Email.CONTACT_ID +=?,新的String [] {ID},空)。

                INT nameId = cursor.getColumnIndex(Contacts.DISPLAY_NAME);

                INT emailIdx = cursor.getColumnIndex(Email.DATA);

                //让我们只得到了第一封电子邮件
                如果(cursor.moveToFirst()){
                    电子邮件= cursor.getString(emailIdx);
                    名称= cursor.getString(nameId);
                    Log.v(DEBUG_TAG,得到了电子邮件:+电子邮件);
                } 其他 {
                    Log.w(DEBUG_TAG,无结果);
                }
            }赶上(例外五){
                Log.e(DEBUG_TAG,无法获得电子邮件数据,E);
            } 最后 {
                如果(光标!= NULL){
                    cursor.close();
                }
                EditText上emailEntry =(EditText上)findViewById(R.id.editTextv);
                EditText上personEntry =(EditText上)findViewById(R.id.person);
                emailEntry.setText(电子邮件);
                personEntry.setText(名称);
                如果(email.length()== 0安培;&安培; name.length()== 0)
                {
                    Toast.makeText(这一点,没有Email为选定的联系人,Toast.LENGTH_LONG).show();
                }
            }
            打破;
        }

    } 其他 {
        Log.w(DEBUG_TAG,警告:活动的结果也不行);
    }
}
 

doLaunchContactPicker 按钮使用code,无论你想要的一个onclick。

outlook 或web邮箱发邮件,怎样实现输入电子邮件地址时自动联想 通讯录 里的邮件地址

I have a code snappet for accessing to Contacts . When user click the button then the contacts list will be open and user can choose a person from contacts and the person's email adress should be write on a edittext. I can receive the email from the persons which user select. But I cant set it to the edittext.

static String email = "";


imgbtnaddfromcontacts.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (v == imgbtnaddfromcontacts) {
                    try 
                    {
                        Intent intent = new Intent(Intent.ACTION_PICK,
                                ContactsContract.Contacts.CONTENT_URI);
                        startActivityForResult(intent, 1);

                    } catch (Exception e) {
                        e.printStackTrace();
                        Log.e("Error in intent : ", e.toString());
                    }
                }
            }
        });
        kimeTxt.setText(email);
    }

    @Override
    public void onActivityResult(int reqCode, int resultCode, Intent data) {
        super.onActivityResult(reqCode, resultCode, data);

        try {
            if (resultCode == Activity.RESULT_OK) {
                // Get data
                Uri contactData = data.getData();
                // Cursor
                Cursor cur = managedQuery(contactData, null, null, null, null);
                ContentResolver contect_resolver = getContentResolver();

                // List
                if (cur.moveToFirst()) {
                    String id = cur
                            .getString(cur
                                    .getColumnIndexOrThrow(ContactsContract.Contacts._ID));

                    Cursor phoneCur = contect_resolver.query(
                            ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                            null,
                            ContactsContract.CommonDataKinds.Phone.CONTACT_ID
                                    + " = ?", new String[] { id }, null);

                    Cursor emailCur = contect_resolver.query(
                            ContactsContract.CommonDataKinds.Email.CONTENT_URI,
                            null,
                            ContactsContract.CommonDataKinds.Email.CONTACT_ID
                                    + " = ?", new String[] { id }, null);

                    if (phoneCur.moveToFirst()) {
                        name = phoneCur
                                .getString(phoneCur
                                        .getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
                        no = phoneCur
                                .getString(phoneCur
                                        .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));

                    }

                    while (emailCur.moveToNext()) {
                        // This would allow you get several email addresses
                        // if the email addresses were stored in an array
                        email = emailCur
                                .getString(emailCur
                                        .getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));

                        if (email != null) 
                        {
                            seciliEmail = email;
                        } else {
                            Toast.makeText(EpostaIletActivity.this,
                                    "Kişinin eposta hesabı bulunmamaktadır.",
                                    Toast.LENGTH_SHORT);
                            Log.w("Error: ", "Kişinin eposta hesabı yok.");
                        }
                    }

                    Log.e("Phone no & name & email :***: ", name + " : " + no + ":" + email);
                    // txt.append(name + " : " + no + "\n");

                    id = null;
                    name = null;
                    no = null;
                    seciliEmail = "xxx";
                    phoneCur = null;
                    emailCur.close();
                }
                contect_resolver = null;
                cur = null;
                // populateContacts();

            }
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
            Log.e("IllegalArgumentException :: ", e.toString());
        } catch (Exception e) {
            e.printStackTrace();
            Log.e("Error :: ", e.toString());
        }
    }

解决方案

Am using below code for getting email address from selected contact -

public void doLaunchContactPicker(View view) {
    Intent contactPickerIntent = new Intent(Intent.ACTION_PICK,Contacts.CONTENT_URI);
    startActivityForResult(contactPickerIntent, CONTACT_PICKER_RESULT);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) 
{
    if (resultCode == RESULT_OK) {
        switch (requestCode) 
        {
        case CONTACT_PICKER_RESULT:
            Cursor cursor = null;
            String email = "", name = "";
            try {
                Uri result = data.getData();
                Log.v(DEBUG_TAG, "Got a contact result: " + result.toString());

                // get the contact id from the Uri
                String id = result.getLastPathSegment();

                // query for everything email
                cursor = getContentResolver().query(Email.CONTENT_URI,  null, Email.CONTACT_ID + "=?", new String[] { id }, null);

                int nameId = cursor.getColumnIndex(Contacts.DISPLAY_NAME);

                int emailIdx = cursor.getColumnIndex(Email.DATA);

                // let's just get the first email
                if (cursor.moveToFirst()) {
                    email = cursor.getString(emailIdx);
                    name = cursor.getString(nameId);
                    Log.v(DEBUG_TAG, "Got email: " + email);
                } else {
                    Log.w(DEBUG_TAG, "No results");
                }
            } catch (Exception e) {
                Log.e(DEBUG_TAG, "Failed to get email data", e);
            } finally {
                if (cursor != null) {
                    cursor.close();
                }
                EditText emailEntry = (EditText) findViewById(R.id.editTextv);
                EditText personEntry = (EditText) findViewById(R.id.person);
                emailEntry.setText(email);
                personEntry.setText(name);
                if (email.length() == 0 && name.length() == 0) 
                {
                    Toast.makeText(this, "No Email for Selected Contact",Toast.LENGTH_LONG).show();
                }
            }
            break;
        }

    } else {
        Log.w(DEBUG_TAG, "Warning: activity result not ok");
    }
}

doLaunchContactPicker is an onclick of Button Use the code wherever you wants.