在 Android 中,如何选择联系人并将其显示在我的应用程序上?

发布于 2024-09-25 16:53:43 字数 1279 浏览 4 评论 0原文

我是android的初学者,我正在构建一个应用程序,当用户按下按钮时,会显示存储在手机中的联系人。当他从中选择联系人时,我必须获取所选联系人的姓名和号码。我尝试使用该代码,但仅显示所选联系人的姓名,而不显示电话号码。

public void readcontact(){
    try {
        Intent intent = new Intent(Intent.ACTION_PICK, Uri.parse("content://contacts/people"));
        startActivityForResult(intent, PICK_CONTACT);
    } catch (Exception e) {
            e.printStackTrace();
      }
}

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

      switch (reqCode) {
        case (PICK_CONTACT) :
          if (resultCode == Activity.RESULT_OK) {
              Uri contactData = data.getData();
                Cursor c =  managedQuery(contactData, null, null, null, null);
                startManagingCursor(c);
                if (c.moveToFirst()) {
                  String name = c.getString(c.getColumnIndexOrThrow(People.NAME));  
                  String number = c.getString(c.getColumnIndexOrThrow(People.NUMBER));
                  perrsonname.setText(name);
                  Toast.makeText(this,  name + " has number " + number, Toast.LENGTH_LONG).show();
                 }
           }
         break;
      }

  }

我什至需要所选联系人的附加号码(家庭、办公室等), 谁能帮我解决这个问题。对此的建议是值得重视的。

I am a beginner to android, i am building a application in which when the user presses a button, the contacts which is stored in the mobile are shown. When he selects a contact from that, i have to get the selected contact name and number. I tried using the code but only the name of the selected contact is shown and not the phone number.

public void readcontact(){
    try {
        Intent intent = new Intent(Intent.ACTION_PICK, Uri.parse("content://contacts/people"));
        startActivityForResult(intent, PICK_CONTACT);
    } catch (Exception e) {
            e.printStackTrace();
      }
}

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

      switch (reqCode) {
        case (PICK_CONTACT) :
          if (resultCode == Activity.RESULT_OK) {
              Uri contactData = data.getData();
                Cursor c =  managedQuery(contactData, null, null, null, null);
                startManagingCursor(c);
                if (c.moveToFirst()) {
                  String name = c.getString(c.getColumnIndexOrThrow(People.NAME));  
                  String number = c.getString(c.getColumnIndexOrThrow(People.NUMBER));
                  perrsonname.setText(name);
                  Toast.makeText(this,  name + " has number " + number, Toast.LENGTH_LONG).show();
                 }
           }
         break;
      }

  }

I even need the additional number(home,office etc.,)of the selected contacts,
Can anyone help me out with this. Suggestions on this is appreciable.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

寒尘 2024-10-02 16:53:43
  public void readcontact(){ 
    try { 
        Intent contactPickerIntent = new Intent(Intent.ACTION_PICK,
                PhoneLookup.CONTENT_FILTER_URI); // creates the contact list intent
        contactPickerIntent.setType(Contacts.Phones.CONTENT_TYPE); // selects contacts with phone only
        startActivityForResult(contactPickerIntent, PICK_CONTACT); 

    } catch (Exception e) { 
            e.printStackTrace(); 
    } 
} 
 
public void onActivityResult(int reqCode, int resultCode, Intent data) { 
      super.onActivityResult(reqCode, resultCode, data); 
 
      switch (reqCode) { 
        case (PICK_CONTACT) : 
          if (resultCode == Activity.RESULT_OK) { 
              Uri contactData = data.getData(); // has the uri for picked contact
                Cursor c = getContentResolver().query(contactData, null, null, null, null); // creates the contact cursor with the returned uri
                if (c.moveToFirst()) { 
                   String name = c.getString(c.getColumnIndex(PhoneLookup.DISPLAY_NAME));
                   String number = c.getString(c.getColumnIndex(PhoneLookup.NUMBER));
                   Toast.makeText(this,  name + " has number " + number, Toast.LENGTH_LONG).show(); 
                 } 
           } 
         break; 
      } 
 
  } 
  public void readcontact(){ 
    try { 
        Intent contactPickerIntent = new Intent(Intent.ACTION_PICK,
                PhoneLookup.CONTENT_FILTER_URI); // creates the contact list intent
        contactPickerIntent.setType(Contacts.Phones.CONTENT_TYPE); // selects contacts with phone only
        startActivityForResult(contactPickerIntent, PICK_CONTACT); 

    } catch (Exception e) { 
            e.printStackTrace(); 
    } 
} 
 
public void onActivityResult(int reqCode, int resultCode, Intent data) { 
      super.onActivityResult(reqCode, resultCode, data); 
 
      switch (reqCode) { 
        case (PICK_CONTACT) : 
          if (resultCode == Activity.RESULT_OK) { 
              Uri contactData = data.getData(); // has the uri for picked contact
                Cursor c = getContentResolver().query(contactData, null, null, null, null); // creates the contact cursor with the returned uri
                if (c.moveToFirst()) { 
                   String name = c.getString(c.getColumnIndex(PhoneLookup.DISPLAY_NAME));
                   String number = c.getString(c.getColumnIndex(PhoneLookup.NUMBER));
                   Toast.makeText(this,  name + " has number " + number, Toast.LENGTH_LONG).show(); 
                 } 
           } 
         break; 
      } 
 
  } 
沉睡月亮 2024-10-02 16:53:43

在这里,我将向您演示如何在点击或焦点事件时从联系人列表中选择联系人号码和姓名,

焦点事件:-

     phoneNo.setOnFocusChangeListener(new OnFocusChangeListener()
      {   public void onFocusChange(View v, boolean hasFocus) 
          {
             Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
             startActivityForResult(intent,PICK_CONTACT );//PICK_CONTACT is private static final int, so declare in activity class
       } });
  • 获取联系人姓名和电话号码的函数是:

     public void onActivityResult(int reqCode, int resultCode, Intent data)
       {
         super.onActivityResult(reqCode, resultCode, 数据);
    
            开关(请求代码){
               案例(PICK_CONTACT):
                 if (resultCode == Activity.RESULT_OK)
                 {
                     Uri contactData = data.getData();
                     游标 c = ManagedQuery(contactData, null, null, null, null);
                  如果(c.moveToFirst()){
                  字符串 ID =   
                    c.getString(c.getColumnIndexOrThrow(ContactsContract.Contacts._ID));
    
                  字符串有电话=
                  c.getString(c.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));
    
                  如果(hasPhone.equalsIgnoreCase(“1”)){
                 光标电话 = getContentResolver().query( 
                              ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null, 
                              ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ id, 
                              空,空);
                    电话.moveToFirst();
                    String phn_no =phones.getString(phones.getColumnIndex("data1"));
                    字符串名称 = c.getString(c.getColumnIndex(StructuredPostal.DISPLAY_NAME));
                   Toast.makeText(this, "联系信息: "+ phn_no+"\n"+姓名, Toast.LENGTH_LONG).show();
    
                  }
                    }
                 }
            }
    

    }

Here i will demonstrate you that how to pick a contact no and name from a contact list on click or focus event,

Focus Event:-

     phoneNo.setOnFocusChangeListener(new OnFocusChangeListener()
      {   public void onFocusChange(View v, boolean hasFocus) 
          {
             Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
             startActivityForResult(intent,PICK_CONTACT );//PICK_CONTACT is private static final int, so declare in activity class
       } });
  • A FUNCTION TO GET THE CONTACT NAME AND PHONE NO IS :

      public void onActivityResult(int reqCode, int resultCode, Intent data)
       {
         super.onActivityResult(reqCode, resultCode, data);
    
            switch(reqCode){
               case (PICK_CONTACT):
                 if (resultCode == Activity.RESULT_OK)
                 {
                     Uri contactData = data.getData();
                     Cursor c = managedQuery(contactData, null, null, null, null);
                  if (c.moveToFirst()) {
                  String id =   
                    c.getString(c.getColumnIndexOrThrow(ContactsContract.Contacts._ID));
    
                  String hasPhone =
                  c.getString(c.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));
    
                  if (hasPhone.equalsIgnoreCase("1")) {
                 Cursor phones = getContentResolver().query( 
                              ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null, 
                              ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ id, 
                              null, null);
                    phones.moveToFirst();
                    String phn_no = phones.getString(phones.getColumnIndex("data1"));
                    String name = c.getString(c.getColumnIndex(StructuredPostal.DISPLAY_NAME));
                   Toast.makeText(this, "contact info : "+ phn_no+"\n"+name, Toast.LENGTH_LONG).show();
    
                  }
                    }
                 }
            }
    

    }

メ斷腸人バ 2024-10-02 16:53:43
Intent intent1 = new Intent(Intent.ACTION_PICK, Contacts.Phones.CONTENT_URI);
            startActivityForResult(intent1, PICK_CONTACT_RQCODE_OLD);
            startActivity(intent1);

这在我的实现中有效。

Intent intent1 = new Intent(Intent.ACTION_PICK, Contacts.Phones.CONTENT_URI);
            startActivityForResult(intent1, PICK_CONTACT_RQCODE_OLD);
            startActivity(intent1);

This works in my implementation.

已下线请稍等 2024-10-02 16:53:43

这可以轻松地提供所选联系人的所有电话号码

启动联系人活动,

Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);

startActivityForResult(intent, PICK_CONTACT);

现在使用以下命令获取所有数据,

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

    switch(reqCode){
       case (PICK_CONTACT):
         if (resultCode == Activity.RESULT_OK){
             Uri contactData = data.getData();
             Cursor c = managedQuery(contactData, null, null, null, null);
             ContentResolver cr = getContentResolver();

             if (c.moveToFirst())
             {
                 try
                 {
                     String id = c.getString(c.getColumnIndex(ContactsContract.Contacts._ID));
                     String name = c.getString(c.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME));
                     if (Integer.parseInt(c.getString(c.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) 
                     {
                        Cursor pCur = cr.query(
                        ContactsContract.CommonDataKinds.Phone.CONTENT_URI, 
                        null, 
                        ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?", 
                        new String[]{id}, null);
                        String phone = "";
                        while (pCur.moveToNext()) 
                        {
                            try
                            {
                                phone = phone + pCur.getString(pCur.getColumnIndex(ContactsContract.Contacts.Data.DATA1)) + ",";
                            }
                            catch(Exception ex)
                            {

                            }
                        } 
                        pCur.close();
                        if(phone.length() > 0)
                        {
                            phone = phone.substring(0,phone.length()-1);
                        }
                        txtPhone.setText(phone);
                     }
                     txtName.setText(name); 
                 }
                 catch(Exception ex)
                 {
                     Toast.makeText(AddToInnerCircle.this, "No name selected", Toast.LENGTH_SHORT).show();
                 }
             }
         }
    }
}

This gives all the phone numbers from a picked contact in a hassle free manner

Start the Contact Activity,

Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);

startActivityForResult(intent, PICK_CONTACT);

Now use the following to get all the data,

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

    switch(reqCode){
       case (PICK_CONTACT):
         if (resultCode == Activity.RESULT_OK){
             Uri contactData = data.getData();
             Cursor c = managedQuery(contactData, null, null, null, null);
             ContentResolver cr = getContentResolver();

             if (c.moveToFirst())
             {
                 try
                 {
                     String id = c.getString(c.getColumnIndex(ContactsContract.Contacts._ID));
                     String name = c.getString(c.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME));
                     if (Integer.parseInt(c.getString(c.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) 
                     {
                        Cursor pCur = cr.query(
                        ContactsContract.CommonDataKinds.Phone.CONTENT_URI, 
                        null, 
                        ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?", 
                        new String[]{id}, null);
                        String phone = "";
                        while (pCur.moveToNext()) 
                        {
                            try
                            {
                                phone = phone + pCur.getString(pCur.getColumnIndex(ContactsContract.Contacts.Data.DATA1)) + ",";
                            }
                            catch(Exception ex)
                            {

                            }
                        } 
                        pCur.close();
                        if(phone.length() > 0)
                        {
                            phone = phone.substring(0,phone.length()-1);
                        }
                        txtPhone.setText(phone);
                     }
                     txtName.setText(name); 
                 }
                 catch(Exception ex)
                 {
                     Toast.makeText(AddToInnerCircle.this, "No name selected", Toast.LENGTH_SHORT).show();
                 }
             }
         }
    }
}
提笔落墨 2024-10-02 16:53:43

我发现选择带有电话号码的联系人并显示显示名称和所选电话号码的最简单方法。另请参阅:完整的可执行示例作为要点

private final static int PICK_CONTACT = 0;

private void pickContact() {
    Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);

    // Show only contacts with phone number. (Also forces user to choose ONE 
    // phone number for a contact that has several.)
    intent.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_TYPE);

    startActivityForResult(intent, PICK_CONTACT);
}

@Override
public void onActivityResult(int reqCode, int resultCode, Intent data) {
    super.onActivityResult(reqCode, resultCode, data);
    switch (reqCode) {
        case (PICK_CONTACT):
            if (resultCode == Activity.RESULT_OK) {
                handleSelectedContact(data);
            }
            break;
    }
}

private void handleSelectedContact(Intent intent) {
    Uri contactUri = intent.getData();
    Cursor c = getContentResolver().query(contactUri, null, null, null, null);
    if (c.moveToFirst()) {
        String name = c.getString(c.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
        String selectedPhone = c.getString(c.getColumnIndex(ContactsContract.Contacts.Entity.DATA1));

        String message = String.format("Name %s, selected phone %s", name, selectedPhone);
        Snackbar.make(contentView, message, Snackbar.LENGTH_LONG).show();
    }
    c.close();
}

如果您需要其他字段,例如电话号码和电子邮件,请查看此问题中的答案

Simplest way I've found to pick a contact with phone number, and show display name and the selected phone number. See also: full, executable example as gist.

private final static int PICK_CONTACT = 0;

private void pickContact() {
    Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);

    // Show only contacts with phone number. (Also forces user to choose ONE 
    // phone number for a contact that has several.)
    intent.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_TYPE);

    startActivityForResult(intent, PICK_CONTACT);
}

@Override
public void onActivityResult(int reqCode, int resultCode, Intent data) {
    super.onActivityResult(reqCode, resultCode, data);
    switch (reqCode) {
        case (PICK_CONTACT):
            if (resultCode == Activity.RESULT_OK) {
                handleSelectedContact(data);
            }
            break;
    }
}

private void handleSelectedContact(Intent intent) {
    Uri contactUri = intent.getData();
    Cursor c = getContentResolver().query(contactUri, null, null, null, null);
    if (c.moveToFirst()) {
        String name = c.getString(c.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
        String selectedPhone = c.getString(c.getColumnIndex(ContactsContract.Contacts.Entity.DATA1));

        String message = String.format("Name %s, selected phone %s", name, selectedPhone);
        Snackbar.make(contentView, message, Snackbar.LENGTH_LONG).show();
    }
    c.close();
}

If you need other fields, for example both phone number and email, check out answers in this question.

纵山崖 2024-10-02 16:53:43

请参阅 Android 源附带的联系人应用程序

Refer Contacts Application which comes with Android Source

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文