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
Related
App is for my personal use, so no issue with Play store policies.
For regular phone number the SmsManager works perfectly.
I have something different.
I am receiving message from some special number ex. JOHN.1234567#domain.com, see snap from sms details. Note: In above number the Numeric value is not phone number.
First I will show you how I am working
I have created below observer and I am getting message properly.
String url = "content://sms/";
Uri uri = Uri.parse(url);
this.getApplicationContext().getContentResolver()
.registerContentObserver(uri, true, new MessageObserver(new Handler(), this));
Now in this I used content observer to fetch the message details like below.
Uri loUriAllMessages = Uri.parse("content://sms/");
Cursor loCursor = moContext.getContentResolver().query(
loUriAllMessages, null, null, null, "_id DESC");
if (loCursor != null && loCursor.getCount() > 0) {
Log.i(TAG, "Cursor Count: " + loCursor.getCount());
if (loCursor.moveToFirst()) {
final int liMessageId = loCursor.getInt(loCursor
.getColumnIndex("_id"));
final String lsMessageText = loCursor.getString(loCursor
.getColumnIndex("body"));
String lsPhoneNumber = loCursor.getString(loCursor
.getColumnIndex("address"));
final String lsDate = loCursor.getString(loCursor
.getColumnIndex("date"));
String lsType = loCursor.getString(loCursor.getColumnIndex("type"))
.trim();
long llTime = Long.parseLong(lsDate);
}
}
All above things work perfectly.
In lsPhoneNumber field I am getting JOHN.1234567#domain.com
Now from my app if I send message using SmsManager on this number then it did not working, all message fail.
SmsManager smsManager = SmsManager.getDefault();
ArrayList<String> loParts = smsManager.divideMessage(message);
smsManager.sendMultipartTextMessage(number, null, loParts, null, null);
But if I go to Messaging app and use same above in send To field and send message then successfully sent on that number.
In short manually sending on the same working fine, But using SmsManager getting fail.
Hopping for the proper guidelines.
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
I've got some problem with sending sms from Android app.
In strings.xml I have text1 and text2 with some text.
When I'm trying to send sms with only one string, e.g
sms.sendTextMessage(number, null,message, null,null);
where message i getString(R.string.text1) it works fine. But what I need is send
String text = getString(R.string.text1) + someVariable + getString(R.string.text2); but it doesn't work.
I've tried to make getResources().getString() in both but still nothing, it's not sending;/ What am I doing wrong?
You can use String Builder to append all those string and put it together.
StringBuilder sb = new StringBuilder(some_appropriate_size);
sb.append(getResources().getString(R.string.text1));
sb.append(someVariable);
sb.append(getResources().getString(R.string.text2));
String final_string = sb.toString();
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);
}
I have developed an application which listens for all incoming SMS (using Broadcast Receiver) but I want to make my application activate an activity when it listen a Particular SMS (Ex. "LOCATIONRECQEST" sended from mobile no. say - 98765...).
So, basically I want to know is how to check the content of SMS (and do some task accordingly).
Also I want to get the number from which the SMS had been sent.
Any Help is awaited.
Thank you.
To read SMS content, you can use a ContentResolver :
// Uri of the SMS inbox
Uri uri = Uri.parse("content://sms/inbox");
// Query
Cursor c = getContentResolver().query(uri, null, null, null, null);
if (null != c && c.moveToFirst()) {
// For all SMS
// do {
String date = c.getString(c.getColumnIndex("date")); // Date (Timestamp)
String adress = c.getString(c.getColumnIndex("address")); // Phone number
String body = c.getString(c.getColumnIndex("body")); // Message
// For all SMS
// }while(c.moveToNext());
}
To find a String in your SMS content :
String body = "this is the text of the sms - code 25D45 - ...";
String stringToFind = "25D45";
// return the position of the "stringToFind"
if(body.indexOf(stringToFind) != -1) {
// your action here
}