【问题标题】:How can i get only all mobile numbers from android phone book?我怎样才能从android电话簿中只获取所有手机号码?
【发布时间】:2015-09-22 18:14:18
【问题描述】:

这是我的代码,它提供了我所有的 Android 设备号码,例如手机号码、whatsapp 号码和固定电话号码。但我只想将安卓电话簿中的手机号码和座机号码放入我的应用程序中。如何只获取每个联系人姓名的手机号码和座机号码?

private void addContactsInList() {
    // TODO Auto-generated method stub

    Cursor cursor = getContentResolver().query(
            ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
            null, null, null, null);

    try {
        ContactsListClass.phoneList.clear();
    } catch (Exception e) {

    }

    while (cursor.moveToNext()) {
        String phoneName = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
        String phoneNumber = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
        int phoneId = cursor.getInt(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.CONTACT_ID));


        Contact cp = new Contact();

        cp.setName(phoneName);
        cp.setNumber(phoneNumber);
        cp.setId(phoneId);


        ContactsListClass.phoneList.add(cp);
       // Log.e("add to list", "content" + phoneId +phoneName +phoneNumber);
    }
    cursor.close();

    lv = new ListView(context);

    lv.setLayoutParams(new LayoutParams(
            LayoutParams.MATCH_PARENT,
            LayoutParams.MATCH_PARENT));


    llContainer.addView(lv);

    Collections.sort(ContactsListClass.phoneList, new Comparator<Contact>() {
        @Override
        public int compare(Contact lhs,
                           Contact rhs) {
            return lhs.getName().compareTo(
                    rhs.getName());
        }
    });

    contactAdapter = new ContactsAdapter(MainActivity.this,
            ContactsListClass.phoneList);
    lv.setAdapter(contactAdapter);

【问题讨论】:

  • @brennanyoung,我怎样才能在我的代码中获得这三种类型的内容,你能帮我吗?请
  • ContactsContract.CommonDataKinds.Phone.TYPE专栏
  • @ pskink , String phoneNumber = cursor.getString(cursor.getColumnIndex(String.valueOf(ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE)) 这给了我错误
  • 列名是ContactsContract.CommonDataKinds.Phone.TYPE 而不是ContactsContract.CommonDat‌aKinds.Phone.TYPE_MOBILE

标签: android sqlite


【解决方案1】:

在你的 while 循环中,输入如下内容:

    while (phoneCursor.moveToNext()) {
                phoneNumber = phoneCursor.getString(phoneCursor.getColumnIndex(NUMBER));
                //String number = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                int type = phoneCursor.getInt(phoneCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE));
                switch (type) {
                    case ContactsContract.CommonDataKinds.Phone.TYPE_HOME:
                        // do something with the Home number here...
                        break;
                    case ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE:
                        output.append("\n Phone number:" + phoneNumber);
                        break;
                    case ContactsContract.CommonDataKinds.Phone.TYPE_WORK:
                        // do something with the Work number here...
                        break;
                }
            }

this 回答中所见

【讨论】:

    【解决方案2】:

    试试这个,

    Cursor cursor = null;
                ArrayList<String> numberList=new ArrayList<String>();
                try {
                    cursor = context.getContentResolver().query(Phone.CONTENT_URI, null, null, null, null);
                    int contactIdIdx = cursor.getColumnIndex(Phone._ID);
                    int nameIdx = cursor.getColumnIndex(Phone.DISPLAY_NAME);
                    int phoneNumberIdx = cursor.getColumnIndex(Phone.NUMBER);
                    int photoIdIdx = cursor.getColumnIndex(Phone.PHOTO_ID);
                    cursor.moveToFirst();
                    do {
                        String phoneNumber = cursor.getString(phoneNumberIdx);
                        numberList.add(phoneNumber);
                    } while (cursor.moveToNext());  
                } catch (Exception e) {
                    e.printStackTrace();
                } finally {
                    if (cursor != null) {
                        cursor.close();
                    }
                }
        }
    

    【讨论】:

      【解决方案3】:
         Uri personUri = ContentUris.withAppendedId(People.CONTENT_URI, personId);
         Uri phonesUri = Uri.withAppendedPath(personUri,                   People.Phones.CONTENT_DIRECTORY);
         String[] proj = new String[] {Phones._ID, Phones.TYPE, Phones.NUMBER, Phones.LABEL}
              Cursor cursor = contentResolver.query(phonesUri, proj, null, null, null);
          if(cursor != null && cursor.moveToFirst()){
          do{
          String number = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                          int type = phoneCursor.getInt(phoneCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE));
                          switch (type) {
      
                              case ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE:
                              break;   
                          }
          }while(cursor.moveToNext());
          }
      

      【讨论】:

      • 如果您对此答案感到满意,请接受并点赞。这对我会有帮助
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-04-05
      • 2018-03-02
      • 2014-05-05
      • 2015-07-20
      • 2023-01-30
      • 2013-12-22
      • 2014-12-10
      相关资源
      最近更新 更多