SMS not sending in android - android

i am trying to send an sms text message with my android application, however i am receiving nothing when i run it
here is the code
int minSms = 100001;
int maxSms = 999998;
Random rSms = new Random();
int iSms = rSms.nextInt(maxSms - minSms +1) + minSms;
try {
SmsManager sms = SmsManager.getDefault();
PendingIntent sentPI;
String SENT = "SMS_SENT";
sentPI = PendingIntent.getBroadcast(getActivity(), 0,new Intent(SENT), 0);
sms.sendTextMessage(mobileNumber, null, Integer.toString(iSms), sentPI, null);
Toast.makeText(getActivity(), "SMS sent.",
Toast.LENGTH_SHORT).show();
} catch (Exception e) {
Toast.makeText(getActivity(),
"SMS failed, please try again.",
Toast.LENGTH_LONG).show();
e.printStackTrace();
}
I am testing with my own mobile number which i am not going to share but it is in the format 07789123456
Do i need to add any area codes with it, or should it work with the straight 11 digit number?

Please take a look at SMS Manager send mutlipart message when there is less than 160 characters . Im pretty sure the text you want to send is to long. Depending on the alphabet (i.e. including emojis) you are using, the message is limited to 70 characters. You have to split the message into a multipart message:
SmsManager sms = SmsManager.getDefault();
ArrayList<String> parts = sms.divideMessage(longMessage);
sms.sendMultipartTextMessage(phoneNumber, null, parts, null, null);

Related

how to send sms automatically in android

i am using this code to send sms automatically but is not working ,i am able to send sms by using intent
SmsManager sms = SmsManager.getDefault();
PendingIntent sentPI;
String SENT = "SMS_SENT";
sentPI = PendingIntent.getBroadcast(this, 0,new Intent(SENT), 0);
sms.sendTextMessage("+91"+"**********", null, "hii param", sentPI, null);
Toast.makeText(getApplicationContext(), "Your sms sent check your inbox",Toast.LENGTH_LONG).show();
Use Permissions in your Android.manifest file like this
<uses-permission android:name="android.permission.SEND_SMS"/>
And then invoke the SmsManager i.e
SmsManager managerForSms = SmsManager.getDefault();
managerForSms.sendTextMessage("Your text message");
Or you may refer to this Stack's question How to send sms in Android. (See accepted Answer)
Try this code,
public void sendSMS(String phoneNo, String msg){
try {
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(phoneNo, null, msg, null, null);
}
catch (Exception ex) {
ex.printStackTrace();
}
}
And use this permission in your manifest file
<uses-permission android:name="android.permission.SEND_SMS" />
Update:
Add your country code before pass the phone number
String phoneNo="+91"+editText.getText().toString();
Update 2:
Another possible cause for not working
If u are testing this code in dual sim phone then sim slot 1 always keep active otherwise it " no service" error.
reference - https://stackoverflow.com/a/32090923/3879847
Code Which works for me is this.. Before you need to provide permission in manifest and also need to ask during runtime
<uses-permission android:name="android.permission.SEND_SMS" />
and in Activity
runOnUiThread(new Runnable() {
#Override
public void run() {
String number = SMSEditText.getText().toString();
try {
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(number, null, Message, null, null);
} catch (Exception e) {
e.printStackTrace();
}
}
});
also if message lenght is greater than 140 or something.. you need to split the message otherwise. it wont work .. which can be acheived by mulitpart text message

How to send SMS in languages other than english using SmsManager?

I have tried to send Text Message using SmsManager in android. Message is delivering fine. When i tried to send message in different language it delivers as ??????. I don't know what's the problem.
try {
SmsManager smsManager = SmsManager.getDefault();
String phno = "+919715361062";
String msg = "டெஸ்டிங்";
smsManager.sendTextMessage(phno, null, msg, null, null);
}catch (Exception e){
e.printStackTrace();
Toast.makeText(MainActivity.this,"Failed",Toast.LENGTH_LONG).show();
}
try this :
ArrayList<String> arrSMS = smsManager.divideMessage("Text to send");
smsManager.sendMultipartTextMessage("Number", null, arrSMS, null, null);
You have to use sendMultipartTextMessage().
Look at this question: Android: Unicode/Charset problems when sending an SMS (sendTextMessage)

Send SMS with out saving it in sent item in kitkat

I am developing an application which needs to send SMS.
I am using SmsManager.SendTextMessage and SmsManager.sendMultipartTextMessage , Both of them are working well , the main problem is sent message will be saved on sent items(only in Kitkat , the older version don't save SMS) , but I don't need to save message.
My app is default messageing app.
What should I do to prevent saving SMS in kitkat?
Here is my code:
try
{
SmsManager smsManager = SmsManager.getDefault();
String body ="Test";
String to = "5556";
ArrayList<String> parts =smsManager.divideMessage(body);
smsManager.sendMultipartTextMessage(to,null, parts, null, null);
}
catch(Exception ex)
{
Toast.makeText(this, ex.getMessage(), Toast.LENGTH_LONG).show();
}

Receiving only part of send text via SMS from one android device to another

I am sending a text (Encrypted text) from one android device to another but while receiving I am getting only part of text.
1.
Text I am sending: O34S/6hQqmbsYeWPVqZK/g==]wwOyeaKvTvV5ytoxGri1kj96jDIukzu45je+U822TM=
Text I am receiving: O34S/6hQqmbsYeWPVqZK/g==] e e =
2.
Text I am sending: B+zbCj1D2DcMO1/j9131TA==]zh5sYADg0v8liAZJjEefik52czrwpNlhZ5VgAMU=
Text I am receiving: B+zbCj1D2DcMO1/j9131TA==] / %
It seems that after "]" the text is trimmed.
Code I am using to send the text
String phoneNo = txtPhoneNo.getText().toString();
final String sendtext = text_to_send;
String message = sendtext;
if (phoneNo.length()>0 && message.length()>0)
sendSMS(phoneNo, message);
private void sendSMS(String phoneNumber, String message)
{
PendingIntent pi = PendingIntent.getActivity(this, 0,
new Intent(this, CorporateActivity.class), 0);
#SuppressWarnings("deprecation")
SmsManager sms = SmsManager.getDefault();
sms.sendTextMessage(phoneNumber, null, message, pi, null);
}
I found this solution there:
It may be useful to look at how gTalkSMS handles incoming SMS'es, as it appears to handle multipart messages correctly.

Sending text messages programmatically in android

OK. I am sending text messages through my app. After a text message is sent, it sends a status update to a server. This portion works okay, but the problem I am running into is twofold. I am not sure if they are related, but I assume that they are.
My app has the ability to send a single text to multiple users. Here is a sample of the code...
if(phoneNumbers.length > 0 && message.getText().toString().equals("") == false)
{
for(int i=0;i<phoneNumbers.length;i++)
{
sms = SmsManager.getDefault();
try
{
sms.sendTextMessage(phoneNumbers[i], null, message.getText().toString(), null, null);
sentQuantity++;
}
catch(IllegalArgumentException e)
{
}
}
}
Basically, it just loops through an array of phone numbers, and sends the text one at a time. Here is where part of my issue lies. If I choose 3 or more numbers to send the text to, sometimes not all of the texts actually get sent. It happens very randomly.
I assume it is because there is a delay between sending each individual message, but the code doesn't wait long enough. I reached this assumption because if I step into the program using eclipse and manually go through the app, everything always works just fine.
My other issue is when I send off the text message status update to a web server.
Immediately after the text messages get sent, the app then connects to the internet and tells the server via an http post the number of texts that were sent. Here is my snippet of internet code...
for(int i = 0; i < postNames.length; i++)
{
nameValuePairs.add(new BasicNameValuePair(postNames[i], postValues[i]));
}
//http post
try{
HttpParams httpParameters = new BasicHttpParams();
int timeoutConnection = 10000;
HttpConnectionParams.setConnectionTimeout(httpParameters,timeoutConnection );
HttpClient httpclient = new DefaultHttpClient(httpParameters);
HttpPost httppost = new HttpPost(webAddress);
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
This section just compiles the items for the post, connects to a web page, and sends the post. The key here is the 10 second connection timeout. Once again, like I said earlier, the internet connection happens immediately after sending the texts. So, if I go into debug mode and manually step through the process, everything works fine. But if I just run the app on my phone, I will get a connection time out error.
Now, I am hoping that if I can reduce the number of text messages to one single text, regardless of the number of recipients, that would be awesome. I have tried separating the phone numbers with a comma, and that didn't work. I also tried separating the numbers with a semi-colon (exactly like how Microsoft Outlook, or GMail lets you add multiple recipients to an email). None of those worked for me. Does anyone have any suggestions? Even if there is a different approach altogether, that would be appreciated. Oh, and I don't want to use the Google Messaging intent to send the messages, I need to use my own app.
You actually need to send the next sms after the previous one is sent, for this you need to check the status of the sms sent, please see this tutorial, it says:
If you need to monitor the status of the SMS message sending process, you can actually use two PendingIntent objects together with two BroadcastReceiver objects, like this:
//---sends an SMS message to another device---
private void sendSMS(String phoneNumber, String message)
{
String SENT = "SMS_SENT";
String DELIVERED = "SMS_DELIVERED";
PendingIntent sentPI = PendingIntent.getBroadcast(this, 0,
new Intent(SENT), 0);
PendingIntent deliveredPI = PendingIntent.getBroadcast(this, 0,
new Intent(DELIVERED), 0);
//---when the SMS has been sent---
registerReceiver(new BroadcastReceiver(){
#Override
public void onReceive(Context arg0, Intent arg1) {
switch (getResultCode())
{
case Activity.RESULT_OK:
Toast.makeText(getBaseContext(), "SMS sent",
Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
Toast.makeText(getBaseContext(), "Generic failure",
Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_NO_SERVICE:
Toast.makeText(getBaseContext(), "No service",
Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_NULL_PDU:
Toast.makeText(getBaseContext(), "Null PDU",
Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_RADIO_OFF:
Toast.makeText(getBaseContext(), "Radio off",
Toast.LENGTH_SHORT).show();
break;
}
}
}, new IntentFilter(SENT));
//---when the SMS has been delivered---
registerReceiver(new BroadcastReceiver(){
#Override
public void onReceive(Context arg0, Intent arg1) {
switch (getResultCode())
{
case Activity.RESULT_OK:
Toast.makeText(getBaseContext(), "SMS delivered",
Toast.LENGTH_SHORT).show();
break;
case Activity.RESULT_CANCELED:
Toast.makeText(getBaseContext(), "SMS not delivered",
Toast.LENGTH_SHORT).show();
break;
}
}
}, new IntentFilter(DELIVERED));
SmsManager sms = SmsManager.getDefault();
sms.sendTextMessage(phoneNumber, null, message, sentPI, deliveredPI);
}
I guess this is what u need.
The below snippet provides to to enter long message and divide them into part and send each of it individual to a given contact or even for a group of contacts
public void sendLongSMS() {
String phoneNumber = "0123456789";
String message = "Hello World! Now we are going to demonstrate " +
"how to send a message with more than 160 characters from your Android application.";
SmsManager smsManager = SmsManager.getDefault();
ArrayList<String> parts = smsManager.divideMessage(message);
smsManager.sendMultipartTextMessage(phoneNumber, null, parts, null, null);
}
First you must add permission in AndroidManifest.xml file
<uses-permission android:name="android.permission.SEND_SMS"></uses-permission>
then write this code
try {
String ph="1234568790";
String msg="Haiii friend";
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(ph, null,msg, null, null);
Toast.makeText(MainActivity.this, "Message Sent",
Toast.LENGTH_LONG).show();
}
catch (Exception e)
{
Toast.makeText(MainActivity.this, "Message not Sent",
Toast.LENGTH_LONG).show();
}
You can try this
SmsManager.getDefault().sendTextMessage(phoneNUmber, null, messageToSend, null, null);
Thanks

Categories

Resources