【问题标题】:Android Contact list getting phone numberAndroid联系人列表获取电话号码
【发布时间】:2011-02-22 23:06:45
【问题描述】:

嗨,我正在我的应用程序中尝试此代码,我可以获得联系人列表,但是当我按下联系人姓名时,我的编辑文本中没有任何内容,我希望获得联系人电话号码 对不起我的英语不好

         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=RESULT_OK) {
            case CONTACT_PICKER_RESULT:
                Cursor cursor = null;
                String phone = "";
                try {
                    Bundle extras = data.getExtras();
                    Set<String> keys = extras.keySet();
                    Iterator<String> iterate = keys.iterator();
                    while (iterate.hasNext()) {
                        String key = iterate.next();
                        Log.v(DEBUG_TAG, key + "[" + extras.get(key) + "]");
                    }

                    Uri result = data.getData();
                    Log.v(DEBUG_TAG, "Got a contact result: "
                            + result.toString());

                    // get the contact id from the Uri
                    String id = result.getLastPathSegment();


                    cursor = getContentResolver().query(Phone.CONTENT_URI,
                            null, Phone.CONTACT_ID + "=?", new String[] { id },
                            null);

                    int PhoneIdx = cursor.getColumnIndex(Phone.DATA);


                    if (cursor.moveToFirst()) {

                                                                        phone = cursor.getString(PhoneIdx);

                        Log.v(DEBUG_TAG, "Got number: " + phone);

                    } else {
                        Log.w(DEBUG_TAG, "No results");
                    }
                } catch (Exception e) {
                    Log.e(DEBUG_TAG, "Failed to Number", e);
                } finally {
                    if (cursor != null) {
                        cursor.close();
                    }
                    EditText ponenumber = (EditText) findViewById(R.id.ednum);
                    ponenumber.setText(phone);

                    if (phone.length() == 0) {
                        Toast.makeText(this, "No number found for contact.",
                                Toast.LENGTH_LONG).show();
                    }

                }

                break;
            }

        } else {
            Log.w(DEBUG_TAG, "Warning: activity result not ok");
        }
    }

【问题讨论】:

    标签: android contact picker


    【解决方案1】:
    Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,null,null, null);
    
    while (phones.moveToNext())
    {
         String Name=phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME)
         String Number=phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
    
    }
    

    【讨论】:

    • 到目前为止最快的答案在这里,但应该注意的是,需要执行自己的重复检查。
    【解决方案2】:

    你实际上真的很接近 - 一些事情。

    • 您的 switch 语句基于 resultCode,而不是请求代码。改变它 - 它应该是“switch(requestCode)”

    • Phone.DATA 可以使用,但为了便于阅读,请改用 Phone.NUMBER :)

    • 确保您在您的 AndroidManifest.xml 文件中设置了 android.permission.READ_CONTACTS 的权限,作为清单元素中的一个元素,而不是在应用程序元素中。该行应如下所示。

      &lt;uses-permission android:name="android.permission.READ_CONTACTS" /&gt;

    我对您的示例进行了这些修改,并获得了工作代码。

    【讨论】:

    • Thx Alexander 我更改为“switch(requestCode)”,将 phone.Data 更改为 Phone.Number,我确信我有权限但它不起作用我可以获得联系人列表但是当我选择联系什么都没发生,所以请帮助我,我不知道该怎么做,这对我来说真的很重要
    • 您的日志输出中显示了什么?你用什么设备来测试?最小/目标 SDK 设置为什么?
    【解决方案3】:

    您可以更改您想要获取电话号码的代码,如下所示

    
      List numberList = new ArrayList();
            Uri baseUri = ContentUris.withAppendedId(Contacts.CONTENT_URI,contactId);
            Uri targetUri = Uri.withAppendedPath(baseUri,Contacts.Data.CONTENT_DIRECTORY);
            Cursor cursor = getContentResolver().query(targetUri,
                        new String[] {Phone.NUMBER},Data.MIMETYPE+"='"+Phone.CONTENT_ITEM_TYPE+"'",null, null);
            startManagingCursor(cursor);
            while(cursor.moveToNext()){
                numberList.add(cursor.getString(0));
            }
            cursor.close();
    

    【讨论】:

      【解决方案4】:

      这是一个旧帖子,但它可以帮助某人。

      如果你只是要接一个联系人,你可以使用帖子的第一个代码,没有这部分:

      Bundle extras = data.getExtras();
      Set<String> keys = extras.keySet();
      Iterator<String> iterate = keys.iterator();
      while (iterate.hasNext()) {
          String key = iterate.next();
          Log.v(DEBUG_TAG, key + "[" + extras.get(key) + "]");
      }
      

      【讨论】:

        猜你喜欢
        • 2013-05-19
        • 1970-01-01
        • 2017-04-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-04-01
        • 1970-01-01
        相关资源
        最近更新 更多