如何将选定的电子邮件从 android 联系人获取到应用程序? [复制]

Posted

技术标签:

【中文标题】如何将选定的电子邮件从 android 联系人获取到应用程序? [复制]【英文标题】:How to get the selected email from android contacts to the app? [duplicate] 【发布时间】:2012-06-06 12:00:42 【问题描述】:

我想选择构建一个应用程序,该应用程序包括从本地联系人中选择电子邮件 ID。如何在应用程序中获取选定的电子邮件值。如何实现这一点,任何独立示例都会很有用。谢谢。

【问题讨论】:

【参考方案1】:

    如下实现联系人列表:

       public class contDetail extends Activity implements OnItemClickListener     
    private static final String[] PROJECTION = new String[]             
    ContactsContract.Contacts._ID,            
    ContactsContract.Contacts.DISPLAY_NAME,            
    ContactsContract.Contacts.HAS_PHONE_NUMBER,            
    ContactsContract.Contacts.LOOKUP_KEY  
    ;
    private int mIdColumnIndex;
    private int mHasPhoneColumnIndex;
    private TextView mPhone;
    @Override    
     public void onCreate(Bundle savedInstanceState) 
         super.onCreate(savedInstanceState);
           setContentView(R.layout.pickcontact);
       mPhone = (TextView) findViewById(R.id.test); 
    ListView myList=(ListView)findViewById(R.id.list1); 
    Cursor c = managedQuery(ContactsContract.Contacts.CONTENT_URI,
            PROJECTION, null, null, null);       
            mIdColumnIndex = c.getColumnIndex(ContactsContract.Contacts._ID);      
            mHasPhoneColumnIndex = c.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER);
            ListAdapter adapter = new SimpleCursorAdapter(this, 
                    android.R.layout.simple_list_item_1,c, 
                    new String[]  ContactsContract.Contacts.HAS_PHONE_NUMBER ,
                    new int[]  android.R.id.text1 ); 
            myList.setAdapter(adapter); 
            myList.setOnItemClickListener(this);
            //setOnItemSelectedListener(this);      
    
       
    
       public void onItemClick(AdapterView<?>parent, View v, int position, long id) 
    if (position >= 0)  
    final Cursor c = (Cursor) parent.getItemAtPosition(position); 
    if (c.getInt(mHasPhoneColumnIndex) > 0)  
    final long contactId = c.getLong(mIdColumnIndex);   
    final Cursor phones = getContentResolver().query(  
    ContactsContract.CommonDataKinds.Phone.CONTENT_URI, 
    new String[]  ContactsContract.CommonDataKinds.Phone.NUMBER , 
    ContactsContract.CommonDataKinds.Phone.CONTACT_ID + "=" + contactId, null, 
    ContactsContract.CommonDataKinds.Phone.IS_SUPER_PRIMARY + " DESC"); 
    try              
    phones.moveToFirst();   
        mPhone.setText(phones.getString(0));
        String ph = phones.getString(0);
        Intent intent = this.getIntent(); 
        intent.putExtra("SOMETHING",ph ); 
        this.setResult(RESULT_OK, intent); 
        finish(); 
    
     finally                  
    phones.close();    
              
     else   
    mPhone.setText(R.string.button_info_text);  
      
       
    
       public void onNothingSelected(AdapterView parent) 
    mPhone.setText(R.string.button_info_text);
    
    
     
    

    将所选联系人复制到您的活动的 EditText 框中,如下所示:

          @Override
     protected void onActivityResult(int requestCode, int resultCode, Intent data)                  
      super.onActivityResult(requestCode, resultCode, data);
      if(resultCode==RESULT_OK && requestCode==1)       
       ed1.setText(data.getStringExtra("SOMETHING"));
      if(resultCode==RESULT_OK && requestCode==2)       
         ed2.setText(data.getStringExtra("SOMETHING"));   
       
    

【讨论】:

以上是关于如何将选定的电子邮件从 android 联系人获取到应用程序? [复制]的主要内容,如果未能解决你的问题,请参考以下文章

如何从android中的单个查询中获取联系人的名字,姓氏,电子邮件ID

从电话联系人中获取姓名、电话号码和电子邮件地址[关闭]

如何从具有电子邮件或电话或两者的Android手机中选择联系人[关闭]

如何从 Android 中的 Intent 中获取联系信息?

如何从 Android 设备中仅获取电话号码联系人

如何从用户手机获取联系人 URI 并在 android 中使用 glide 加载