Android how to check mobile number - android

In Android We can save mobile number/landline number/etc..how To check that the number is mobile number?
while (phones.moveToNext())
{
int phoneType = phones.getInt(phones.getColumnIndex(Phone.TYPE));
if (phoneType == Phone.TYPE_MOBILE)
{
phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DATA));
break;
}
}
with this code it fetches the mobile number of TYPE_MOBILE ..but what if the user put land line number in TYPE_MOBILE?

This is best way given inbuilt.
private boolean isValidMobile(String phone)
{
return android.util.Patterns.PHONE.matcher(phone).matches();
}

In android you will not get mobile number, if user save our own number in contact as owner then you will get. If you want user mobile number then you can use OTP verification related libraries and then you will got number

Related

How to match phone numbers from user's contact list if numbers are in different format?

I have a question about how to match phone numbers from user's contact list with phone numbers I have on remote database. Flow goes like this:
User registers on my app with his phone number (so does any other user)
App ask for contact permission
App sends contacts (phone numbers) to server to match against other registered numbers
The problem I have is that users register their phone number in format: +1XXXYYY.
For example person A registers with number +1222333. It might happen that person B has person A in his contact list as 0222333, how should I match that number? I can't know if prefix is "+1" or some other number.
I would like to recommend the libphonenumber library: https://github.com/google/libphonenumber
It can parse numbers and then output then to a standardized format. The official library has support for Java, C++ and JavaScript but there are also ports to other languages (see the bottom of the Github page)
Here is a quick example on how to format a national number as an international one in java
public static String getInternationalNumber(String localNumber, String regionCode) {
PhoneNumberUtil phoneUtil = PhoneNumberUtil.getInstance();
Phonenumber.PhoneNumber phoneNumber;
try {
phoneNumber = phoneUtil.parse(localNumber, regionCode);
}
catch (NumberParseException e) {
return null;
}
return (phoneUtil.format(phoneNumber, PhoneNumberUtil.PhoneNumberFormat.INTERNATIONAL));
}
You can probably assume that the numbers in the user's contact list have the same country code as the user.
To find which country code your user's phone number has you can do something like this (assuming it is an international number)
public static String getRegionCode(String phone) {
PhoneNumberUtil phoneUtil = PhoneNumberUtil.getInstance();
Phonenumber.PhoneNumber phoneNumber;
try {
phoneNumber = phoneUtil.parse(phone, "");
}
catch (NumberParseException e) {
return null;
}
return phoneUtil.getRegionCodeForNumber(phoneNumber);
}

Kotlin: How to get the sender's phone number of an SMS message

I am trying to read a user's SMS messages and get the sender's phone number of those messages. When I try getting the sender's phone number of the message through the "address" column, it returns the phone number of the text's conversation (for example, if I send a message to a user with phone number X, the address column returns X instead of my phone number), not the phone number of the person that sent the message. Below is my Kotlin code:
var cursor = contentResolver.query(
Uri.parse("content://sms/"),
null,
null,
null,
null
)
// Retrieve the IDs of the sender's name
var senderID = cursor!!.getColumnIndex("address")
// Iterate through every message
while (cursor!!.moveToNext()) {
var messageSender = cursor.getString(senderID) // Get the sender of the message
System.out.println("---------------------------------------------------------")
System.out.println(messageSender) // Returns phone number of the conversation, not the sender
}
For example: user with phone number 123456789 sends a message to you. I want to retrieve phone number 123456789.
I found a solution. You must use the type column to identify whether the message has been sent or received.
When the message has been sent, you can read the phone number from the Telephony Manager.
fun getMessageSender(cursor: Cursor): String {
val partnerAddressId = cursor.getColumnIndex("address")
val typeId = cursor.getColumnIndex("type")
val partnerAddress = cursor.getString(partnerAddressId)
val type = cursor.getString(typeId)
return if (type.equals("1", true)) {
partnerAddress
} else {
getPhoneNumber()
}
}
private fun getPhoneNumber(): String {
val telephonyManager = getSystemService(Context.TELEPHONY_SERVICE) as TelephonyManager
return telephonyManager.line1Number
}
But please note that I do not know how this affects devices with Multi Sim. I could imagine that the wrong number will be returned here.
I have worked out this solution in combination with the following posts:
Getting phone number of each sms via content://sms/
Programmatically obtain the phone number of the Android phone

Find Country code by using Mobile Number Android

Is there any way to find the country code from my mobile number. Basically, I am working for a chat application I need to find the country code by using the mobile number. Is it possible?
For example:
My Country - India,
Country Code - +91,
My number - 9787248566
Now I have only my number . I don't know the country code. I don't know which country it is. Is it possible to achieve this in Android programmatically?
If you already have complete mobile number in a specific format e.g. +91-99xxxxxxxx and want to fetch country code = IN, then here is a reference to a solution.
Use a lib in your gradle.
//Phone Utils lib.
implementation 'com.googlecode.libphonenumber:libphonenumber:7.0'
And the code which will help to find the country code and other information is as below.
PhoneNumberUtil utils = PhoneNumberUtil.getInstance();
try {
for (String region : utils.getSupportedRegions()) {
// Check whether it's a valid number.
boolean isValid = utils.isPossibleNumber(mobileNo, region);
if (isValid) {
Phonenumber.PhoneNumber number = util.parse(mobileNo, region);
// Check whether it's a valid number for the given region.
isValid = utils.isValidNumberForRegion(number, region);
if (isValid) {
Log.d("Region:" , region); // IN
Log.d("Phone Code", number.getCountryCode()); // 91
Log.d("Phone No.", number.getNationalNumber()); // 99xxxxxxxxxx
}
}
}
} catch (NumberParseException e) {
e.printStackTrace();
}
Use TelephonyManager.getSimCountryIso().
If you want to map that to the number, see how to get country phone prefix from iso

Android - phone number formatting and removing the country/area code

I have built an app where I loop through and collect the users phone contacts, my aim is to then use these numbers and query my parse database and look for records that contain the users contacts (this will be to check if any of the users contacts are a user of my app, a users phone number will be saved to my parse database when they register). The problem I've got is that when collecting the users contacts numbers they are returned in different formats, some +447966000000, some 07966000000, some 07 966000 000000, etc.
My question is, what would be the best way to format my numbers when saving them to the database and retrieving them from the users contacts so that all numbers are saved and retrieved in the same format so that when I do a conditional check on them they will be easy to compare?
I have downloaded phone Number Utils library but I am not sure what in the library could be used to do something like this.
Code so far:
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 phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
Toast.makeText(getApplicationContext(),name + " " + phoneNumber, Toast.LENGTH_LONG).show();
}
phones.close();
You can use PhoneNumberUtils.compare to compare and check if they are same or not.It returns true if they are same ignoring country codes etc.
Example:
PhoneNumberUtils.compare(context, 1234567890, +911234567890);
returns true
I have done it for Indian mobile number format
private String getNumber(String moNumber) {
Pattern special = Pattern.compile ("[!##$%&*()_+=|<>?{}\\[\\]~-]");
if (moNumber.isEmpty()||moNumber.length()<10) {
MydebugClass.showToast(getContext(), "Please input valid Number");
return null;
}else if (moNumber.length()>10 && !special.matcher(moNumber).find()){
String[] number=moNumber.split("");
StringBuilder stringBuilder=new StringBuilder();
for(int i=moNumber.length();i>moNumber.length()-10;i--){
stringBuilder.append(number[i]);
}
String reverse=new StringBuffer(stringBuilder).reverse().toString();
return reverse;
}else if(moNumber.length()>10&&special.matcher(moNumber).find()){
String numberOnly= moNumber.replaceAll("[^0-9]", "");
String[] number=numberOnly.split("");
StringBuilder stringBuilder=new StringBuilder();
for(int i=moNumber.length();i>moNumber.length()-10;i--){
stringBuilder.append(number[i]);
}
String reverse=new StringBuffer(stringBuilder).reverse().toString();
Log.d("mobilenumberspecial",reverse);
return reverse;
}
else {
return moNumber;
}
return null;
}

Android current users account information

I am getting the name and phone number of all the contact information on the user's phone but the name and picture of the phone's current user doesn't appear in this list.
Uri uri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
String trimedNumber = phoneNumber.replaceAll("[^A-Za-z0-9 ]", "").replaceAll(" ","");
if(trimedNumber.length()<10)
{
return returnValue;
}
trimedNumber = trimedNumber.substring(trimedNumber.length()-10, trimedNumber.length()-1);
Cursor cursor = getContentResolver().query(uri, null, null, null, null);
if (!cursor.moveToFirst()) {
return returnValue;
How can I get the current user's information?
By current user, I assume you mean the owner of the phone. This problem cannot always be solved, but here is a method to get the owners number:
/*
* Tries to get the user's phone number. Note that there is no guaranteed solution to this problem because the phone
* number is not physically stored on all SIM-cards, or broadcasted from the network to the phone. This is especially
* true in some countries which requires physical address verification, with number assignment only happening
* afterwards. Phone number assignment happens on the network - and can be changed without changing the SIM card
* or device (e.g. this is how porting is supported).
*/
private String getPhoneNumber() {
TelephonyManager telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
String phoneNumber = telephonyManager.getLine1Number();
if (phoneNumber != null) return PhoneNumberUtils.formatNumber(phoneNumber);
else return phoneNumber;
}

Categories

Resources