Android send SMS message with two getString() - android

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();

Related

How to add something into string that read by String.xml?

I used this:
String message += getResources().getString(R.string.string1) + "some more word...";
and I wanted to send this string via sms, but it is not working. It works fine without the string resource. Am I missing something?
#forpas answer is absolutely correct, but you can also concat string resource this way.
<string name="name">Name %s</string>
String nameText = getString(R.string.name,"khemraj");
When you use += operator with a String the result is a concatenation of the previous value of the String with some new String.
When you define a String variable like this:
String s;
the variable s is not initialized, so this:
s+="something";
is not allowed.
So instead of
String message += getResources().getString(R.string.string1) + "some more word...";
do
String message = getResources().getString(R.string.string1) + "some more word...";

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

How to get Text written after "," in MultiAutoCompleteTextView?

I am developing an application that uses MultiAutoCompleteTextView for showing hints in the drop down list.In this application I retrieve the value written in the MultiAutoCompleteTextView by using
multitextview.getText();
and then query this value to server to recieve JSON response which is shown as suggestions in the drop down list.
If a user types Mu and then Selects music from the list and then types box for another suggestion the content in the MultiAutoCompleteTextView becomes Music,box and now the value for querying to the server is Music,box instead of this I want to select only box.
My question is how to retrieve text written after "," in MultiAutoCompleteTextView?
Can this be achieved using getText()?
I solved this issue
String intermediate_text=multitextview.getText().toString();
String final_string=intermediate_text.substring(intermediate_text.lastIndexOf(",")+1);
I'm sure there are several ways to get around this. One way to do it would be:
String textToQuerryServer = null;
String str = multitextview.getText().toString(); // i.e "music, box" or "any, thing, you , want";
Pattern p = Pattern.compile(".*,\\s*(.*)");
Matcher m = p.matcher(str);
if (m.find()) {
textToQuerryServer = m.group(1);
System.out.println("Pattern found: "+ textToQuerryServer);
}else {
textToQuerryServer = str;
System.out.println("No pattern: "+ textToQuerryServer);
}

Trouble sending hyperlink via json

I use Gson classes in my android apps to flatten out ojects and send them over REST to the server and back using HTTP posts and responses. It has always worked well.
But in new app that I am writing I'm trying to do the same thing but using SMS messages instead of HTTP Posts. For instance I have a class ...
class LocReturn
{
public String error; //one char y = error
public String accuracy;
public String hyperlink;
public String stationary; //one char y = stationary
public String speedmph;
public String speedkph;
public String bearing;
}
Before I flatten the class, the hyperlink string gets the following value . . .
http://maps.google.com/maps?z=17&t=h&q=loc:31.7898,-111.0354
However when I examine the json string I see that the hyperlink has been changed to . . .
http://maps.google.com/maps?z\u003d17\u0026t\u003dh\u0026q\u003dloc:31.7898,-111.0354
Also, the SMS send is getting a NullPointer Exception.
The code that send the SMS message is . . .
Gson gson = new Gson();
String jsonstring = gson.toJson(myReturn);
String SMSBody = "###2" + jsonstring;
DebugLog.debugLog("Mole is sending Back: " + " num= " + GlobalStuff.Mobileno + " SMSBody= " + SMSBody, false);
SmsManager
.getDefault()
.sendTextMessage(GlobalStuff.Mobileno, null, SMSBody, null, null);
DebugLog.debugLog("After SMS Send " + SMSBody, true);
finish();
So, (1) I can't see why I'm getting the NullPointer and (2) why is the hyperlink getting changed and does that have anything to do with it.
Thanks,
Gary
EDIT: the new json object creation...
Gson gson = new GsonBuilder().disableHtmlEscaping().create();
did indeed keep the hyperlink from being modified however the nullPointerException continues. It is definitely the sendText line that is causing the exception as when I substitute SMSBody for "hello world" it works fine. The contents of SMSBody is...
###2{"accuracy":"Excellent","bearing":"","error":"n","hyperlink":"http://maps.google.com/maps?z=17&t=h&q=loc:31.7898,-111.0353","speedkph":"","speedmph":"","stationary":"y"}
this is somehow causing the nullPointerException
Answering my own question for future googlers . . .
I forgot that SMS stands for SHORT Message Service. My message was too long and, guess what happens when you send too long a message in SMSManager.sendTextMessage ? You get a nullPointerException (instead of getting a MessageTooLong exception).
Gary

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