如何删除国家代码,当从联系人中选择电话号码时

New*_*New 3 java android telephonymanager

在此处输入图片说明

我对该部分有疑问。当我从联系人列表中选择电话号码时,如何删除国家代码?

例如:+91 999999999 代替 9999999999 或 +020 9696854549 代替 9696854549 谁能知道我的问题的答案。请给出这个问题的解决方案

我在这里附上了我的代码和图片。

     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);
   String phoneNumber = phoneNo.replaceAll(" ","");
   mobile_et.setText(phoneNumber);
} catch (Exception e) {
    e.printStackTrace();
}
}
Run Code Online (Sandbox Code Playgroud)

iju*_*yce 5

我的英语很差,但我会尽力回答你的问题。

首先,将此行添加到您的 build.gradle

compile 'com.googlecode.libphonenumber:libphonenumber:8.7.0'
Run Code Online (Sandbox Code Playgroud)

下面是 kotlin 编写的示例

fun deleteCountry(phone: String) : String{
    val phoneInstance = PhoneNumberUtil.getInstance()
    try {
        val phoneNumber = phoneInstance.parse(phone, null)
        return phoneNumber?.nationalNumber?.toString()?:phone
    }catch (_ : Exception) {
    }
    return phone
}
Run Code Online (Sandbox Code Playgroud)

如果电话号码不是以“+”开头,后跟国家电话代码,那么您应该传递地区信息,例如:

val phoneNumber = phoneInstance.parse(phone, "CN")


Dee*_*ana 0

使用这个功能希望它能帮助你:

public String phoeNumberWithOutCountryCode(String phoneNumberWithCountryCode) {
        Pattern complie = Pattern.compile(" ");
        String[] phonenUmber = complie.split(phoneNumberWithCountryCode);
        Log.e("number is", phonenUmber[1]);
        return phonenUmber[1];
    }
Run Code Online (Sandbox Code Playgroud)