In Android, how to pick a contact and display it on my app?

前端 未结 6 1742
暖寄归人
暖寄归人 2020-12-21 09:28

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 cont

6条回答
  •  抹茶落季
    2020-12-21 09:36

      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; 
          } 
    
      } 
    

提交回复
热议问题