Difference of Contact No. Format - android

I'm developing an sms blocker for which I need to save contact numbers in DB and compare them to SMS sender. I've only default SMS application installed on my android phone. But there is difference of contact number format as picked from following code:
Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
intent.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_TYPE);
startActivityForResult(intent, PICK_CONTACT);
and when I receive SMS using broadcast receiver (see following code segment):
SmsMessage currentMessage = SmsMessage.createFromPdu((byte[]) pdusObj[i]);
String phoneNumber = currentMessage.getDisplayOriginatingAddress();
String number = phoneNumber;
String message = currentMessage.getDisplayMessageBody();
The format of phone # in my contact list is like "033-xxxx-xxxx" without country code but SMS Broadcast Receiver adds country code to the number like "+92331xxxxxxx".
My question (a) is why SMS receiver is using different format then that is saved in my contact lists and (b) how do I compare these numbers in my SMS broadcast receiver.

Check out PhoneNumberUtils.You need to let the O/S help you because it can be international number.
Use String incomingNumber = PhoneNumberUtils.formatNumber(incomingNumber,defaultCountryIso);
on both numbers, then compare them.
To compare you do this:
boolean SameNumber= PhoneNumberUtils.compare(phoneNumber, otherPhoneNumber)
//if true, numbers are the same

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

Send SMS to multiple contacts

I want to send SMS at once to multiple contacts.
The second thing I want is to use the phone's regular SMS service and not to get a window where I need to select the program (i.e. select between SMS, Whatsapp, Skype and so on).
I am using this very short code:
numbers = "050-1234567;051-1234567;052-1234567";
String message= "this is a message";
Uri sendSmsTo = Uri.parse("smsto:" + numbers);
Intent intent = new Intent(android.content.Intent.ACTION_SENDTO, sendSmsTo);
intent.putExtra("sms_body", message);
startActivity(intent);
It is not working. I get opened only the last number in the 'numbers' string and not to all of them.
What am I doing wrong?
The two questions are:
How to send SMS to all the numbers in the string?
How to pass automatically the 'select service window' and simply use the default SMS service built-in every phone?
Thanks!
AJ
For multiple contact using array and SmsManager to use SMS Service:
String[] numbers = new String {"46654","4654","16548"};
for(int i = 0; i < numbers.length; i++) {
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(numbers[i], null, "Text Message", null, null);
}

Get address details from a Contact in android

I am trying to access the contact details of a person which i selected from contact picker intent.
here is what my contact looks like:
Here is the code which i am using to open the contact picker:
Intent pickContactIntent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
startActivityForResult(pickContactIntent, PICK_CONTACT_REQUEST);
Now, i am able to get Phone number and email using following API's:
android.provider.ContactsContract.CommonDataKinds.Email;
android.provider.ContactsContract.CommonDataKinds.Phone;
but i am not able to get the address which is stored. I want to get both the address value and custom tag associated with it.
Any help is appreciated.
if You have a contact ID and you want to fetch the Postal Address then use this :
Uri postal_uri = ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_URI;
Cursor postal_cursor = getContentResolver().query(postal_uri,null, ContactsContract.Data.CONTACT_ID + "="+contactId.toString(), null,null);
while(postal_cursor.moveToNext())
{
String Strt = postal_cursor.getString(postal_cursor.getColumnIndex(StructuredPostal.STREET));
String Cty = postal_cursor.getString(postal_cursor.getColumnIndex(StructuredPostal.CITY));
String cntry = postal_cursor.getString(postal_cursor.getColumnIndex(StructuredPostal.COUNTRY));
}
postal_cursor.close();
http://gabrielaradu.com/?p=367
https://stackoverflow.com/a/13471370/2480911

country code of incoming calls in android

I've just built an application that detecting incoming calls. I see that in some phones(or in different version of android) incoming call number has country code, some incoming numbers has not. Is there a way to get incoming calls with country codes in any android phone and in any version of android?
I use broadcast receiver and PhoneStateListener, I get the parameter of incomingNumber at onCallStateChanged. So I didn't use telephonymanager.EXTRA_PHONE_NUMBER (In fact I don't what exactly EXTRA_PHONE_NUMBER does)
Here is a code-snippet that you can use in your BroadcastReceiver to extract the country-code using libphonenumber library.
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Intent.ACTION_NEW_OUTGOING_CALL)) {
// get phone number from bundle
String phoneNumber = intent.getExtras().getString(Intent.EXTRA_PHONE_NUMBER);
// get country-code from the phoneNumber
PhoneNumberUtil phoneUtil = PhoneNumberUtil.getInstance();
try {
PhoneNumber numberProto = phoneUtil.parse(phoneNumber, Locale.getDefault().getCountry());
if (phoneUtil.isValidNumber(numberProto)) {
log.d("TAG", "Country Code: " + numberProto.getCountryCode());
} else {
log.d("TAG", "Invalid number format: " + phoneNumber);
}
} catch (NumberParseException e) {
Log.d(TAG, "Unable to parse phoneNumber " + e.toString());
}
}
}
You can get this done with libphonenumber library https://code.google.com/p/libphonenumber/
EXTRA_PHONE_NUMBER holds number entered by user
A String holding the phone number originally entered in
ACTION_NEW_OUTGOING_CALL, or the actual number to call in a
ACTION_CALL.
via http://developer.android.com/reference/android/content/Intent.html#EXTRA_PHONE_NUMBER
Also, have you seen: How to get phone number from an incoming call? ?

How to send text message to more than 1 number?

I am trying to have a list of contact numbers.
I want to know is there a way to send a text message to more than 1 number iprogrammatically in Android?
If so how?
You can't do this via Intent, as the android SMS app doesn't allow multiple recipients.
You can try using the SmsManager class.
First of all you need to request the permission android.permission.SEND_SMS in your AndroidManifest.
Then you can do something along these lines.
// you need to import the Sms Manager
import android.telephony.SmsManager;
// fetch the Sms Manager
SmsManager sms = SmsManager.getDefault();
// the message
String message = "Hello";
// the phone numbers we want to send to
String numbers[] = {"555123456789", "555987654321"};
for(String number : numbers) {
sms.sendTextMessage(number, null, message, null, null);
}
Update: Added how to split a comma-separated string
// string input by a user
String userInput = "122323,12344221,1323442";
// split it between any commas, stripping whitespace afterwards
String numbers[] = userInput.split(", *");
for group sms or multiple sms use this
Intent i = new Intent(android.content.Intent.ACTION_VIEW);
i.putExtra("address", "987385438; 750313; 971855;84393");
i.putExtra("sms_body", "Testing you!");
i.setType("vnd.android-dir/mms-sms");
startActivity(i);
//use permission: <uses-permission android:name="android.permission.SEND_SMS"/>
you can modify this "9873854; 750313; 971855; 84393" with your contact number

Categories

Resources