How to send mutiple text messages from an android app? - android

Sorry if this question is already asked or seems stupid but i am stuck . I am writing an android application in which app sends different messages to different people the first one successfully send but there is a problem with the second one it doesn't show me error but it doesn't send the second message here is a portion of my code
public void sendSMSMessenger(){
Log.i("Send SMS", "");
try {
// SmsManager smsManager = SmsManager.getDefault();
SmsManager.getDefault().sendTextMessage(Emergencyphonenumber, null, "There is an emergency with your "+PatientRelationship+" "+Patientname+" please open the link to find the location "+"https://www.google.com/maps/place/"+latitude+","+longitude, null, null);
Toast.makeText(this, "Message Sent (Relative)", Toast.LENGTH_SHORT).show();
SmsManager.getDefault().sendTextMessage(Doctorphonenumber, null, "There is an emergency with your patient named "+Patientname+" please open the link to find the location "+"https://www.google.com/maps/place/"+latitude+","+longitude, null, null);
Toast.makeText(this, "Message Sent (Doctor)", Toast.LENGTH_SHORT).show();
} catch (Exception e) {
Toast.makeText(this, "Message Sending failed", Toast.LENGTH_SHORT).show();
e.printStackTrace();
}
}
Thank you....

You can not send messages simultaneously. In a way its hard for network to process all the messages collectively. Wait a little. Increase the time.

Related

How can i make SmsManager API not show sent message and received message at same time

I am writing an app that sends the user of the app a text message when such user performs a certain action with the app. Now, i am using the SmsManager API to achieve this. Only problem is that sent message and received message is showing at the same time in messages. How do i fix this. Also, i want to change the name of the sender of the message to the app name.
public void sendSMSEntering() {
String phone = "08187016641";
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(phone, null, "Welcome to the ICAN 2019 conference" , null, null);
Toast.makeText(getApplicationContext(), "SMS sent.", Toast.LENGTH_LONG).show();
}
public void sendSMSExiting() {
String phone = "08187016641";
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(phone, null, "Goodbye. We hope you enjoyed the conference" , null, null);
Toast.makeText(getApplicationContext(), "SMS sent.", Toast.LENGTH_LONG).show();
}
My button from xml onClickListener
sendMeAMessage.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
clickcount++;
if (clickcount % 2 == 0) {
sendSMSEntering();
} else {
sendSMSExiting();
}
}
});
Screenshot
As can be observed in the image above, the message the app sent and the message received by me are both there. I only want the message received to be displayed in messages. Also you will observe that the sender of the message is my phone number (String phone = "08187016641"). 'phone' is used as both the recipients phone number and the sender of the message. Perhaps smsManager.sendTextMessage(phone, null, "Goodbye. We hope you enjoyed the conference" , null, null); is not good enough to achieved this and needs to be changed to something else. I need suggestions
After so much research I found out that the SMSManager API is whack. There are so many good SMS API's out there that you can integrate with your app. You can then use a GET or POST as appropriate to request SMS messages. These SMS API's give you the option of using a custom Sender name and other powerful options to customize incoming text messages. Some of these SMS API's include Nexmo SMS API, Ozeki SMS API and so many others. The good thing is, they have a comprehensive documentation. Infact, integrating these SMS API's into your app takes just a few lines of code.

Android code to send SMS with no errors but still not sending

Here the address stores current location of the device, which works. I get a toast that says SMS Sent but no sms is actually sent from the device. If the same code is used before finding the location, it works perfectly.
public void sendSMS()
{
String phoneNo = "+918110020302";
String message="Our Customer "+" has booked Cab no "+" using our application CabBooking and is cureently at location: "+address;
try {
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(phoneNo, null, message, null, null);
q.setText(message +phoneNo);
Toast.makeText(getApplicationContext(), "SMS sent.", Toast.LENGTH_LONG).show();
}
catch (Exception e) {
Toast.makeText(getApplicationContext(), "SMS faild, please try again.", Toast.LENGTH_LONG).show();
e.printStackTrace();
}
}
If you haven't seen it yet, here is a working example: http://www.mkyong.com/android/how-to-send-sms-message-in-android/
It may depend on your network. If you are running this inside Android Studio's emulator, it may not work because it does not actually have cell service from within the emulator. You may need to be connected to a different network, or your cellular network.

Sending Mass Texts in Android

After users have logged into my app, I have an option to send a text to invite their friends (from mobile numbers) into the app. However, there does not appear to be a method of sending a mass text with potentially dozens of numbers without using a loop on the .sendTextMessage method.
What I have seen as typical:
protected void sendSMSMessage() {
Log.i("Send SMS", "");
String phoneNo = txtphoneNo.getText().toString();
String message = txtMessage.getText().toString();
try {
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(phoneNo, null, message, null, null);
Toast.makeText(getApplicationContext(), "SMS sent.", Toast.LENGTH_LONG).show();
}
catch (Exception e) {
Toast.makeText(getApplicationContext(), "SMS faild, please try again.", Toast.LENGTH_LONG).show();
e.printStackTrace();
}
}
Is there another way to send mass texts without looping?
You are right but I would suggest using SEND ACTION as sending dozens of sms can cost user and there are application like what's app and hike, which can send or share text with friends at no extra cost.
You can only send one message at a time. If you do not like looping, maybe you can try recursion instead?

How to prevent Outgoing SMS from getting stored in KitKat?

I am creating an application that will send/receive messages to/from a certain gateway number. I don't want the messages that are sent to this particular number to be stored in the inbox of my device.
The code I am using to send the SMS is:
protected void sendMessage(String strMessage) {
try {
SmsManager smsMan = SmsManager.getDefault();
smsMan.sendTextMessage(gatewayNumber, null, strMessage, null, null);
Toast.makeText(getApplicationContext(),
"SMS was sent successfully!",
Toast.LENGTH_LONG).show();
} catch (Exception e) {
Toast.makeText(getApplicationContext(),
"SMS was not sent, please contact developer!",
Toast.LENGTH_LONG).show();
}
}
I have managed to tackle the problem of storing incoming messages by using the abortBroadcast(); function. (I realize this doesn't work above KitKat, and I'm fine with it).
But the messages that are sent from the application are being stored in the inbox too (only on KitKat), and that bothers me. Any solutions?

Android - Don't receive sms sent by sms manager

In my application I want to send an sms to a list of contacts. I used this code:
private void sendSMS(String phoneNumber, String message)
{
try {
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(phoneNumber, null, message, null, null);
Toast.makeText(getActivity().getApplicationContext(), "SMS sent.",
Toast.LENGTH_LONG).show();
} catch (Exception e) {
Toast.makeText(getActivity().getApplicationContext(),
"SMS faild, please try again.",
Toast.LENGTH_LONG).show();
e.printStackTrace();
}
}
I didn't get any stacktraces, but I never received the message. Are there any formats for the phone number? Or is an emulator not able to send text messages? Any tips on how to actually receive the message are welcome!
Your given code seems correct but here are two quick recommendations:
Make sure you have added the permission to send SMS through your app. For that you need to add <uses-permission android:name="android.permission.SEND_SMS"/> in your app's manifest
You cannot send SMS through emulators as they do not have access to cellular network. For that you need to use an actual phone to test your code

Categories

Resources