【问题标题】:how to access contact details in android如何在android中访问联系方式
【发布时间】:2010-11-10 04:52:13
【问题描述】:

我有一个edittext和一个按钮,我想通过单击按钮打开联系人详细信息,用户可以从中选择一个联系人。然后我需要将所选联系人的手机号码放在edittext中。我该​​怎么做这样做吗?谢谢

【问题讨论】:

    标签: android-contacts


    【解决方案1】:

    这很简单。首先你需要创建编辑文本和按钮,然后编写如下代码:

     private EditText e;
        private static final String DEBUG_TAG = "InviteActivity";
        private static final int CONTACT_PICKER_RESULT = 1001;
        @Override
        protected void onCreate(Bundle savedInstanceState) 
        {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            e=(EditText)findViewById(R.id.editText1);      
        }
    
        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 phone="";
                    try
                    { Uri result=data.getData();
                       String id=result.getLastPathSegment();
                        cursor=getContentResolver().query(Phone.CONTENT_URI,null, Phone.CONTACT_ID +"=?",new String[] {id},null);
                        int phoneIdx=cursor.getColumnIndex(Phone.NUMBER);
                        if(cursor.moveToFirst())
                        {
                        phone=cursor.getString(phoneIdx);
                        }
                        if(phone.equals(""))
                        {
                            Toast.makeText(getBaseContext(),"There is no phone number for that contac", Toast.LENGTH_LONG).show();
                        }
                        else
                        {
                        e.setText(phone);
                        }               
                    }
                    catch(Exception e)
                    {
                        Toast.makeText(getBaseContext(),"There is no phone number for that contact", Toast.LENGTH_SHORT).show();
                    }
                    finally
                    {
                        if(cursor!=null)
                        {
                            cursor.close();
    
                    }//finally
                    break;
                }//switch
            }//if
    
         else {
                Log.w(DEBUG_TAG, "Warning: activity result not ok");
            }
        }//on activity result
    
    }//class
    

    对于按钮属性给出:

    android:onClick="doLaunchContactPicker"
    

    别忘了给使用权限:

    <uses-permission android:name="android.permission.READ_CONTACTS" />
    

    如果这个答案有用请不要忘记接受..

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-11-11
      • 1970-01-01
      • 2011-10-02
      • 1970-01-01
      • 2023-04-08
      • 1970-01-01
      • 2019-02-22
      相关资源
      最近更新 更多