Mer*_*rve 6 email android contacts
我有一个用于访问联系人的代码段.当用户单击该按钮时,联系人列表将打开,用户可以从联系人中选择一个人,并且该人的电子邮件地址应该在edittext上写入.我可以收到用户选择的人的电子邮件.但我无法将其设置为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());
}
}
Run Code Online (Sandbox Code Playgroud)
Pra*_*mar 22
使用以下代码从所选联系人处获取电子邮件地址 -
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");
}
}
Run Code Online (Sandbox Code Playgroud)
doLaunchContactPicker是一个Button在任何地方使用代码的onclick .
You can achieve it like this \n\n public class Abc extends Activity{\n EditText kimeTxt;\n Intent i; \n Bundle b ; \n @Override \n\n protected void onCreate(Bundle savedInstanceState) {\n // TODO Auto-generated method stub\n super.onCreate(savedInstanceState);\n setContentView(R.layout.add_course);\n kimeTxt= (EditText)findViewById(R.id.emailid);\n\n }\n\n\n @Override\n public void onActivityResult(int reqCode, int resultCode, Intent data) {\n super.onActivityResult(reqCode, resultCode, data);\n\n try {\n if (resultCode == Activity.RESULT_OK) {\n // Get data\n Uri contactData = data.getData();\n // Cursor\n Cursor cur = managedQuery(contactData, null, null, null, null);\n ContentResolver contect_resolver = getContentResolver();\n\n // List\n if (cur.moveToFirst()) {\n String id = cur\n .getString(cur\n .getColumnIndexOrThrow(ContactsContract.Contacts._ID));\n\n Cursor phoneCur = contect_resolver.query(\n ContactsContract.CommonDataKinds.Phone.CONTENT_URI,\n null,\n ContactsContract.CommonDataKinds.Phone.CONTACT_ID\n + " = ?", new String[] { id }, null);\n\n Cursor emailCur = contect_resolver.query(\n ContactsContract.CommonDataKinds.Email.CONTENT_URI,\n null,\n ContactsContract.CommonDataKinds.Email.CONTACT_ID\n + " = ?", new String[] { id }, null);\n\n if (phoneCur.moveToFirst()) {\n name = phoneCur\n .getString(phoneCur\n .getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));\n no = phoneCur\n .getString(phoneCur\n .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));\n\n }\n\n while (emailCur.moveToNext()) {\n // This would allow you get several email addresses\n // if the email addresses were stored in an array\n email = emailCur\n .getString(emailCur\n .getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));\n\n if (email != null) \n {\n seciliEmail = email;\n } else {\n Toast.makeText(EpostaIletActivity.this,\n "Ki\xc5\x9finin eposta hesab\xc4\xb1 bulunmamaktad\xc4\xb1r.",\n Toast.LENGTH_SHORT);\n Log.w("Error: ", "Ki\xc5\x9finin eposta hesab\xc4\xb1 yok.");\n }\n }\n\n Log.e("Phone no & name & email :***: ", name + " : " + no + ":" + email);\n // txt.append(name + " : " + no + "\\n");\n\n id = null;\n name = null;\n no = null;\n seciliEmail = "xxx";\n phoneCur = null;\n emailCur.close();\n }\n\n\n // can set email id here \n\n\n\n kimeTxt.setText(email);\n\n\n contect_resolver = null;\n cur = null;\n // populateContacts();\n\n }\n } catch (IllegalArgumentException e) {\n e.printStackTrace();\n Log.e("IllegalArgumentException :: ", e.toString());\n } catch (Exception e) {\n e.printStackTrace();\n Log.e("Error :: ", e.toString());\n }\n }\nRun Code Online (Sandbox Code Playgroud)\n
| 归档时间: |
|
| 查看次数: |
13838 次 |
| 最近记录: |