如何选择一个电话号码与Android的联系人对话框对话框、如何选择、电话号码、联系人

2023-09-12 07:36:16 作者:九卿臣

我使用的是旧的联系人API来选择带有电话号码的联系人。我想使用较新的ContactsContracts API。我想...

...与有电话号码的所有联系人下面的对话框。 ...用户选择一个联系人,他们的电话号码。 ...进入所选择的电话号码。

的ContactsContracts是非常复杂的。我发现很多的例子,但没有一个适合我的需要。我不想选择一个联系人,然后查询该联系人的详细信息,因为这会给我的电话号码的列表。我需要的用户可以选择联系人的电话号码。我不希望有写我自己的对话框,显示联系人或让用户选择一个电话号码。有没有简单的方法来得到我想要的东西?

下面是旧的API code我使用:

 静态公共最终诠释触点= 0;
...
意向意图=新的意图(Intent.ACTION_PICK,Contacts.Phones.CONTENT_URI);
startActivityForResult(意向,联系方式);
...
公共无效onActivityResult(INT申请code,INT结果code,意图意图){
  如果(结果code = Activity.RESULT_OK ||请求code =联系我们!)回报;
  光标C = managedQuery(intent.getData(),NULL,NULL,NULL,NULL);
  如果(c.moveToFirst()){
     字符串手机= c.getString(c.getColumnIndexOrThrow(Contacts.Phones.NUMBER));
     // 好极了
  }
}
 

解决方案

 意向意图=新的意图(Intent.ACTION_PICK);
   intent.setType(ContactsContract.Contacts.CONTENT_TYPE);
  startActivityForResult(意向,PICK_CONTACT);
 
苹果7设置了黑名单阻止的电话号码为什么还能打进来

这些code有助于U,我认为挑选活动仅返回联系人的ID回升。 从可以查询联系人提供商和是否存在 多个电话号码,则提示用户选择其中之一。

U可以使用,这也(更新):

 公共无效readcontact(){
    尝试 {
        意向意图=新的意图(Intent.ACTION_PICK,Uri.parse(内容://联系人/人));
        startActivityForResult(意向,PICK_CONTACT);
    }赶上(例外五){
            e.printStackTrace();
      }
}

公共无效onActivityResult(INT REQ code,INT结果code,意图数据){
      super.onActivityResult(REQ code,因此code,数据);

      开关(REQ code){
        案例(PICK_CONTACT):
          如果(结果code == Activity.RESULT_OK){
              乌里联系数据= data.getData();
                光标C = managedQuery(联络人资料,NULL,NULL,NULL,NULL);
                startManagingCursor(C);
                如果(c.moveToFirst()){
                  字符串名称= c.getString(c.getColumnIndexOrThrow(People.NAME));
                  串号= c.getString(c.getColumnIndexOrThrow(People.NUMBER));
                  perrsonname.setText(名称);
                  Toast.makeText(此,名+的编号为+号,Toast.LENGTH_LONG).show();
                 }
           }
         打破;
      }

  }
 

嘿更新的28/12内容:  U可以使用这样的:

  @覆盖
保护无效onActivityResult(INT申请code,INT结果code,意图数据){
    如果(结果code == RESULT_OK){
        开关(要求code){
        案例CONTACT_PICKER_RESULT:
            最后的EditText phoneInput =(EditText上)findViewById(R.id.phoneNumberInput);
            光标光标= NULL;
            字符串phoneNumber的=;
            名单<字符串> allNumbers =新的ArrayList<字符串>();
            INT phoneIdx = 0;
            尝试 {
                开放的结果= data.getData();
                字符串ID = result.getLastPathSegment();
                光标= getContentResolver()查询(Phone.CONTENT_URI,空,Phone.CONTACT_ID +=?,新的String [] {ID},空)。
                phoneIdx = cursor.getColumnIndex(Phone.DATA);
                如果(cursor.moveToFirst()){
                    而(cursor.isAfterLast()== FALSE){
                        phoneNumber的= cursor.getString(phoneIdx);
                        allNumbers.add(phoneNumber的);
                        cursor.moveToNext();
                    }
                } 其他 {
                    //没有结果的行动
                }
            }赶上(例外五){
               //错误操作
            } 最后 {
                如果(光标!= NULL){
                    cursor.close();
                }

                最后的CharSequence []项目= allNumbers.toArray(新的String [allNumbers.size());
                AlertDialog.Builder建设者=新AlertDialog.Builder(your_class.this);
                builder.setTitle(选择一个数字);
                builder.setItems(项目,新DialogInterface.OnClickListener(){
                    公共无效的onClick(DialogInterface对话,诠释项){
                        字符串selectedNumber =项目[项目]的ToString();
                        selectedNumber = selectedNumber.replace( - ,);
                        phoneInput.setText(selectedNumber);
                    }
                });
                AlertDialog警报= builder.create();
                如果(allNumbers.size()→1){
                    alert.show();
                } 其他 {
                    串selectedNumber = phoneNumber.toString();
                    selectedNumber = selectedNumber.replace( - ,);
                    phoneInput.setText(selectedNumber);
                }

                如果(phoneNumber.length()== 0){
                    //没有数字发现行动
                }
            }
            打破;
        }
    } 其他 {
       //活动结果错误操作
    }
}
 

您需要适应这与你的应用程序的工作

I'm using the old Contacts API to choose a contact with a phone number. I want to use the newer ContactsContracts API. I want...

...a dialog shown with all contacts that have phone numbers. ...the user to choose a contact AND one of their phone numbers. ...access to the chosen phone number.

The ContactsContracts is very complicated. I found many examples, but none that fit my needs. I don't want to choose a contact and then query for the contact's details because this will give me a list of their phone numbers. I need the user to choose ONE of the contact's phone numbers. I don't want to have to write my own dialogs to display the contacts or to have the user pick a phone number. Is there any simple way to get what I want?

Here is the old API code I'm using:

static public final int CONTACT = 0;
...
Intent intent = new Intent(Intent.ACTION_PICK, Contacts.Phones.CONTENT_URI);
startActivityForResult(intent, CONTACT);
...
public void onActivityResult (int requestCode, int resultCode, Intent intent) {
  if (resultCode != Activity.RESULT_OK || requestCode != CONTACT) return;
  Cursor c = managedQuery(intent.getData(), null, null, null, null);
  if (c.moveToFirst()) {
     String phone = c.getString(c.getColumnIndexOrThrow(Contacts.Phones.NUMBER));
     // yay
  }
}      

解决方案

   Intent intent = new Intent(Intent.ACTION_PICK);
   intent.setType(ContactsContract.Contacts.CONTENT_TYPE);
  startActivityForResult(intent, PICK_CONTACT); 

These code helps u ,I think the PICK activity only returns the ID of the contact picked. From that you could query the Contact provider and if there are multiple phone numbers, prompt the user to select one of them.

U can use this also(updated ) :

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

  }

Hey Updated Content of 28/12 : U can use this :

@Override  
protected void onActivityResult(int requestCode, int resultCode, Intent data) {  
    if (resultCode == RESULT_OK) {  
        switch (requestCode) {  
        case CONTACT_PICKER_RESULT:
            final EditText phoneInput = (EditText) findViewById(R.id.phoneNumberInput);
            Cursor cursor = null;  
            String phoneNumber = "";
            List<String> allNumbers = new ArrayList<String>();
            int phoneIdx = 0;
            try {  
                Uri result = data.getData();  
                String id = result.getLastPathSegment();  
                cursor = getContentResolver().query(Phone.CONTENT_URI, null, Phone.CONTACT_ID + "=?", new String[] { id }, null);  
                phoneIdx = cursor.getColumnIndex(Phone.DATA);
                if (cursor.moveToFirst()) {
                    while (cursor.isAfterLast() == false) {
                        phoneNumber = cursor.getString(phoneIdx);
                        allNumbers.add(phoneNumber);
                        cursor.moveToNext();
                    }
                } else {
                    //no results actions
                }  
            } catch (Exception e) {  
               //error actions
            } finally {  
                if (cursor != null) {  
                    cursor.close();
                }

                final CharSequence[] items = allNumbers.toArray(new String[allNumbers.size()]);
                AlertDialog.Builder builder = new AlertDialog.Builder(your_class.this);
                builder.setTitle("Choose a number");
                builder.setItems(items, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int item) {
                        String selectedNumber = items[item].toString();
                        selectedNumber = selectedNumber.replace("-", "");
                        phoneInput.setText(selectedNumber);
                    }
                });
                AlertDialog alert = builder.create();
                if(allNumbers.size() > 1) {
                    alert.show();
                } else {
                    String selectedNumber = phoneNumber.toString();
                    selectedNumber = selectedNumber.replace("-", "");
                    phoneInput.setText(selectedNumber);
                }

                if (phoneNumber.length() == 0) {  
                    //no numbers found actions  
                }  
            }  
            break;  
        }  
    } else {
       //activity result error actions
    }  
}

You need to adapt this to work with your app

 
精彩推荐