Android current users account information - android

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;
}

Related

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

Obtaining phone type in string, when type is custom

I obtained contact list from phone with names, phone numbers and phone types. Phone types may be 1 (home), 2 (mobile), etc... And when phone type is custom (for example, "CustomType"), value of phone type returns 0. So, how can i obtain string "CustomType", when phone type is 0 ?
Maybe this code will be useful:
String contactType = "";
final int labelType = cur.getInt(cur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE));
if(labelType == ContactsContract.CommonDataKinds.Phone.TYPE_CUSTOM)
{
contactType = cur.getString(cur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.LABEL));
}
else
{
//Easy case
}
cur is your cursor that execute the query ....getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI...

Android how to check mobile number

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

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 get contact names by phone number efficiently for displaying in ListView

I'm creating an Sms app which contains a ListView in the Main Activity that displays all the conversations from the sms inbox. Each ListView row displays one conversation along with the phone number, message body and time of the message. Now instead of the phone number I want to display the contact name if it exists.
So far, for getting the contact name by phone number I found this code
private String getDisplayNameByNumber(String number) {
Uri uri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(number));
Cursor contactLookup = context.getContentResolver().query(uri, new String[] {ContactsContract.PhoneLookup._ID,
ContactsContract.PhoneLookup.DISPLAY_NAME }, null, null, null);
int indexName = contactLookup.getColumnIndex(ContactsContract.Data.DISPLAY_NAME);
try {
if (contactLookup != null && contactLookup.moveToNext()) {
number = contactLookup.getString(indexName);
}
} finally {
if (contactLookup != null) {
contactLookup.close();
}
}
return number;
}
But this seems inefficient as it has to make a query for each contact name individually and lags the app. So instead I tried to get all the contact names from the phone and store it in an HashMap with the phone number as the key and the contact name as the value, so that I can get the contact name any time I want from the HashMap. But there seems to be another problem, the phone numbers are stored in many different formats, for eg:
+91 4324244434
04324244434
0224324244434
So how do I search for a phone number from the HashMap since it can be stored in many different formats?
Before you add the contact to the HashMap, use regular expression to grab the phone number. This way, no matter what format the phone number is in, the regular expression will be able to grab the appropriate number needed.
Once you grab the number, add it to the HashMap accordingly.
Hope that answers your question.

Categories

Resources