Pick Phone Number from contacts

天涯浪子 提交于 2019-12-21 05:32:29

问题


I have doubt in that section. How to replace space between the string, when to get phone number from Contact list? And it's working fine, But some android devices(e.g. Samsung Tab) have spaces added in their contacts. I get number from contact.So I get string 99 99 99999 instead of 99999999.

And also, How to eliminate country code from that number. Ex: +91 999999999 instead of 9999999999 or +020 9696854549 instead of 9696854549

I know, remove that space by using .replace().
Is there any other process to remove space between the string.

I attached my code:::

   public void onClick(View view) {
            Intent contactPickerIntent = new Intent(Intent.ACTION_PICK,
                    ContactsContract.CommonDataKinds.Phone.CONTENT_URI);
              startActivityForResult(contactPickerIntent, 
RESULT_PICK_CONTACT);
        }

private void contactPicked(Intent data) {
    Cursor cursor = null;
    try {
        String phoneNo = null ;
        // getData() method will have the Content Uri of the selected contact
        Uri uri = data.getData();
        //Query the content uri
        cursor = getContentResolver().query(uri, null, null, null, null);
        cursor.moveToFirst();
        // column index of the phone number
        int  phoneIndex =cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);

        phoneNo = cursor.getString(phoneIndex);

        mobile_et.setText(phoneNo);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

回答1:


Just replace every white space in the phone number

phoneNo=phoneNo.replaceAll("\\s+","");



回答2:


The String class has a method:

.replace(char oldChar, char newChar)

It returns a new String resulting from replacing all occurrences of oldChar in this string with newChar. So you are just replacing all spaces with an empty String (i.e. ""):

phoneNo = cursor.getString(phoneIndex);
String phoneNumber= str.replaceAll(" ", "");  // your string without any spaces


来源:https://stackoverflow.com/questions/45435121/pick-phone-number-from-contacts

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!