android – 访问联系人并获取电子邮件地址

前端之家收集整理的这篇文章主要介绍了android – 访问联系人并获取电子邮件地址前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个用于访问联系人的代码段.当用户单击该按钮时,联系人列表将打开,用户可以从联系人中选择一个人,并且该人的电子邮件地址应该在edittext上写入.我可以收到用户选择的人的电子邮件.但我无法将其设置为edittext.
  1. static String email = "";
  2.  
  3.  
  4. imgbtnaddfromcontacts.setOnClickListener(new OnClickListener() {
  5. @Override
  6. public void onClick(View v) {
  7. if (v == imgbtnaddfromcontacts) {
  8. try
  9. {
  10. Intent intent = new Intent(Intent.ACTION_PICK,ContactsContract.Contacts.CONTENT_URI);
  11. startActivityForResult(intent,1);
  12.  
  13. } catch (Exception e) {
  14. e.printStackTrace();
  15. Log.e("Error in intent : ",e.toString());
  16. }
  17. }
  18. }
  19. });
  20. kimeTxt.setText(email);
  21. }
  22.  
  23. @Override
  24. public void onActivityResult(int reqCode,int resultCode,Intent data) {
  25. super.onActivityResult(reqCode,resultCode,data);
  26.  
  27. try {
  28. if (resultCode == Activity.RESULT_OK) {
  29. // Get data
  30. Uri contactData = data.getData();
  31. // Cursor
  32. Cursor cur = managedQuery(contactData,null,null);
  33. ContentResolver contect_resolver = getContentResolver();
  34.  
  35. // List
  36. if (cur.moveToFirst()) {
  37. String id = cur
  38. .getString(cur
  39. .getColumnIndexOrThrow(ContactsContract.Contacts._ID));
  40.  
  41. Cursor phoneCur = contect_resolver.query(
  42. ContactsContract.CommonDataKinds.Phone.CONTENT_URI,ContactsContract.CommonDataKinds.Phone.CONTACT_ID
  43. + " = ?",new String[] { id },null);
  44.  
  45. Cursor emailCur = contect_resolver.query(
  46. ContactsContract.CommonDataKinds.Email.CONTENT_URI,ContactsContract.CommonDataKinds.Email.CONTACT_ID
  47. + " = ?",null);
  48.  
  49. if (phoneCur.moveToFirst()) {
  50. name = phoneCur
  51. .getString(phoneCur
  52. .getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
  53. no = phoneCur
  54. .getString(phoneCur
  55. .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
  56.  
  57. }
  58.  
  59. while (emailCur.moveToNext()) {
  60. // This would allow you get several email addresses
  61. // if the email addresses were stored in an array
  62. email = emailCur
  63. .getString(emailCur
  64. .getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
  65.  
  66. if (email != null)
  67. {
  68. seciliEmail = email;
  69. } else {
  70. Toast.makeText(EpostaIletActivity.this,"Kişinin eposta hesabı bulunmamaktadır.",Toast.LENGTH_SHORT);
  71. Log.w("Error: ","Kişinin eposta hesabı yok.");
  72. }
  73. }
  74.  
  75. Log.e("Phone no & name & email :***: ",name + " : " + no + ":" + email);
  76. // txt.append(name + " : " + no + "\n");
  77.  
  78. id = null;
  79. name = null;
  80. no = null;
  81. seciliEmail = "xxx";
  82. phoneCur = null;
  83. emailCur.close();
  84. }
  85. contect_resolver = null;
  86. cur = null;
  87. // populateContacts();
  88.  
  89. }
  90. } catch (IllegalArgumentException e) {
  91. e.printStackTrace();
  92. Log.e("IllegalArgumentException :: ",e.toString());
  93. } catch (Exception e) {
  94. e.printStackTrace();
  95. Log.e("Error :: ",e.toString());
  96. }
  97. }

解决方法

使用以下代码从所选联系人处获取电子邮件地址 –
  1. public void doLaunchContactPicker(View view) {
  2. Intent contactPickerIntent = new Intent(Intent.ACTION_PICK,Contacts.CONTENT_URI);
  3. startActivityForResult(contactPickerIntent,CONTACT_PICKER_RESULT);
  4. }
  5.  
  6. @Override
  7. protected void onActivityResult(int requestCode,Intent data)
  8. {
  9. if (resultCode == RESULT_OK) {
  10. switch (requestCode)
  11. {
  12. case CONTACT_PICKER_RESULT:
  13. Cursor cursor = null;
  14. String email = "",name = "";
  15. try {
  16. Uri result = data.getData();
  17. Log.v(DEBUG_TAG,"Got a contact result: " + result.toString());
  18.  
  19. // get the contact id from the Uri
  20. String id = result.getLastPathSegment();
  21.  
  22. // query for everything email
  23. cursor = getContentResolver().query(Email.CONTENT_URI,Email.CONTACT_ID + "=?",null);
  24.  
  25. int nameId = cursor.getColumnIndex(Contacts.DISPLAY_NAME);
  26.  
  27. int emailIdx = cursor.getColumnIndex(Email.DATA);
  28.  
  29. // let's just get the first email
  30. if (cursor.moveToFirst()) {
  31. email = cursor.getString(emailIdx);
  32. name = cursor.getString(nameId);
  33. Log.v(DEBUG_TAG,"Got email: " + email);
  34. } else {
  35. Log.w(DEBUG_TAG,"No results");
  36. }
  37. } catch (Exception e) {
  38. Log.e(DEBUG_TAG,"Failed to get email data",e);
  39. } finally {
  40. if (cursor != null) {
  41. cursor.close();
  42. }
  43. EditText emailEntry = (EditText) findViewById(R.id.editTextv);
  44. EditText personEntry = (EditText) findViewById(R.id.person);
  45. emailEntry.setText(email);
  46. personEntry.setText(name);
  47. if (email.length() == 0 && name.length() == 0)
  48. {
  49. Toast.makeText(this,"No Email for Selected Contact",Toast.LENGTH_LONG).show();
  50. }
  51. }
  52. break;
  53. }
  54.  
  55. } else {
  56. Log.w(DEBUG_TAG,"Warning: activity result not ok");
  57. }
  58. }

doLaunchContactPicker是按钮的onclick使用任何你想要的代码.

猜你在找的Android相关文章