Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/224.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/video/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从联系人Android获取单个电话号码_Android_Android Contacts_Phone Number - Fatal编程技术网

从联系人Android获取单个电话号码

从联系人Android获取单个电话号码,android,android-contacts,phone-number,Android,Android Contacts,Phone Number,嗨,我想从联系人列表中找到一个电话号码。我找到了一个代码,它可以让我获得整个联系人列表的电话号码,我所需要的只是所单击项目的电话号码。任何帮助都将不胜感激。谢谢 public void onClick(View v) { Intent contactPickerIntent = new Intent(Intent.ACTION_PICK,Contacts.CONTENT_URI); startActivityForResult(contactPickerIntent,CONTACT_PICKER

嗨,我想从联系人列表中找到一个电话号码。我找到了一个代码,它可以让我获得整个联系人列表的电话号码,我所需要的只是所单击项目的电话号码。任何帮助都将不胜感激。谢谢

public void onClick(View v) {
Intent contactPickerIntent = new Intent(Intent.ACTION_PICK,Contacts.CONTENT_URI);
startActivityForResult(contactPickerIntent,CONTACT_PICKER_RESULT);

ContentResolver cr = getActivity().getContentResolver();
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null,null, null);

    if (cur.getCount() > 0) {
        while (cur.moveToNext()) {
            String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
            String name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));

            if (Integer.parseInt(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
                Cursor pCur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                null,ContactsContract.CommonDataKinds.Phone.CONTACT_ID+ " = ?",new String[] { id }, null);

                while (pCur.moveToNext()) {
                    String phoneNo = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                    Toast.makeText(getActivity(), phoneNo,Toast.LENGTH_SHORT).show();
                }
            }
        }
    }
}
});

当您在联系人列表中并单击某个联系人时,您将获得您的
意图的结果。操作\u选择
意图

因此,要处理结果,必须重写onActivityResult()
方法

您将在由
onActivityResult()
返回的数据
Intent
对象中收到联系人的查找URI

所以,你所要做的就是:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {

    if(requestCode == CONTACT_PICKER_RESULT){
        if(resultCode==Activity.RESULT_OK){

            Uri uri = data.getData();
            ContentResolver contentResolver = getContentResolver();
            Cursor contentCursor = contentResolver.query(uri, null, null,null, null);

            if(contentCursor.moveToFirst()){
                String id = contentCursor.getString(contentCursor.getColumnIndexOrThrow(ContactsContract.Contacts._ID));

                String hasPhone =
                        contentCursor.getString(contentCursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));

                if (hasPhone.equalsIgnoreCase("1")) 
                {
                    Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null, 
                            ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ id,null, null);
                    phones.moveToFirst();
                    String contactNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                    Log.i("phoneNUmber", "The phone number is "+ contactNumber);

                }
            }
            super.onActivityResult(requestCode, resultCode, data);
        }
    }
}
然后您只需使用
contactNumber
上的值执行任何操作


希望能对您有所帮助。

首先在AndroidManifest.xml中添加这一行,以获取用户的权限

移动用户还需要允许此权限。可以通过设置手动完成此操作

<uses-permission android:name="android.permission.READ_CONTACTS"/>
现在从onCreate()函数的外部开始。必须重写onActivityResult()函数。 与此类似

    @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
   }


@Override
protected 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();
                ContentResolver cr = getContentResolver();
                Cursor cur = cr.query(contactData, null, null, null, null);
                if (cur.getCount() > 0) {// thats mean some resutl has been found
                    if(cur.moveToNext()) {
                        String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
                        String name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
                        Log.e("Names", name);
                        if (Integer.parseInt(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0)
                        {
                            // Query phone here. Covered next
                            Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ id,null, null);
                            while (phones.moveToNext()) {
                                String phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                                Log.e("Number", phoneNumber);
                            }
                            phones.close();
                        }

                    }
                }
                cur.close();
            }
            break;
    }

}

哪个联系人?随机的还是第一个?打开联系人列表后单击的联系人。。非常感谢你工作得很好。。谢谢您的时间:-)
    @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
   }


@Override
protected 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();
                ContentResolver cr = getContentResolver();
                Cursor cur = cr.query(contactData, null, null, null, null);
                if (cur.getCount() > 0) {// thats mean some resutl has been found
                    if(cur.moveToNext()) {
                        String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
                        String name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
                        Log.e("Names", name);
                        if (Integer.parseInt(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0)
                        {
                            // Query phone here. Covered next
                            Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ id,null, null);
                            while (phones.moveToNext()) {
                                String phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                                Log.e("Number", phoneNumber);
                            }
                            phones.close();
                        }

                    }
                }
                cur.close();
            }
            break;
    }

}