【问题标题】:How to get phone number type label from given phone number in android如何从android中的给定电话号码获取电话号码类型标签
【发布时间】:2016-08-16 23:24:55
【问题描述】:

我尝试从给定特定电话号码的电话联系人中打印联系人姓名(例如“Jose”)和联系人电话号码类型值(例如“Home”、“Work”等)(例如“9600515852” ")

为此,我使用了以下代码。但我只能获取联系人姓名。当我尝试获取电话号码类型时,应用程序崩溃了。

请指导我。

String number = getContactName(this, "9600515852");
Toast.makeText(getApplicationContext(),number,Toast.LENGTH_LONG).show();

private String getContactName(Context context, String number) {
String TAG ="" ;
String name = null, label=null;
int type=0;

// define the columns I want the query to return
String[] projection = new String[] { ContactsContract.PhoneLookup.DISPLAY_NAME,ContactsContract.PhoneLookup._ID };

// encode the phone number and build the filter URI
   Uri contactUri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(number));

// query time
Cursor cursor = context.getContentResolver().query(contactUri, projection, null, null, null);

        if(cursor != null) {
            if (cursor.moveToFirst()) {
                name =      cursor.getString(cursor.getColumnIndex(ContactsContract.PhoneLookup.DISPLAY_NAME));
               // type = cursor.getInt(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE));
            } else {
                Log.v(TAG, "Contact Not Found @ " + number);
            }
            cursor.close();
        }
        return name+" - "+number;
    }

【问题讨论】:

    标签: android android-contacts phone-number


    【解决方案1】:

    使用

    name = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
    

    而不是

    name = cursor.getString(cursor.getColumnIndex(ContactsContract.PhoneLookup.DISPLAY_NAME));
    

    在我的情况下,单击按钮从联系人应用程序获取联系信息是这样的。它正在工作

    调用意图

    Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
    intent.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_TYPE);
    startActivityForResult(intent, 1);
    

    处理结果

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
    
        switch (requestCode) {
            case (1):
                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.getColumnIndex(ContactsContract.Contacts._ID));
                        String name = c.getString(c.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
    
                        String resultedPhoneNum = pop.getContactNumber(id, MainActiv.this);
                        String resultedName=pop.getContactName(name, MainActiv.this);
    
                        contactName.setText(resultedName);
                    }
                }
                break;
            }
        }
    

    希望有帮助!

    【讨论】:

      【解决方案2】:

      正确的方法是在 Projection 查询中包含 Phone.TYPE。它仅包含 DISPLAY_NAMEID,并且由于您的查询中没有“类型”字段而崩溃。

      这肯定会解决你的问题。

      【讨论】:

        【解决方案3】:

        试试这个:

        type = cursor.getString(cursor.getColumnIndex(ContactsContract.PhoneLookup.TYPE));
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2016-02-18
          • 1970-01-01
          • 2012-01-01
          • 2012-02-11
          • 1970-01-01
          • 2011-09-05
          • 2023-03-22
          • 1970-01-01
          相关资源
          最近更新 更多