I am new to android . In my application I want to send sms with contacts. How can i send sms to other through my appplication
private String SENT = "SENT";
private String DELIVERED = "DELIVERED";
...
PendingIntent sentPI = PendingIntent.getBroadcast(this.context, this.getUniqueId(), new Intent(this.SENT), 0);
PendingIntent deliveredPI = PendingIntent.getBroadcast(this.context, this.getUniqueId(), new Intent(this.DELIVERED), 0);
String smsNumber = "+123456";
SmsManager sms = SmsManager.getDefault();
sms.sendTextMessage(smsNumber, null, "sms content", this.sentPI, this.deliveredPI);
Related
I have pending intent to show message status after send
private void sendSMS(String phoneNumber, String message)
{
SmsManager sms = SmsManager.getDefault();
sms.sendTextMessage(phoneNumber, null, message, sentPI, deliveredPI);
}
This is my pending intent
deliveredPI = PendingIntent.getBroadcast(this, id, new Intent(DELIVERED), 0);
When sms is delivered, I want to get requestcode from PendingIntent to show which sms is delivered, but in onReceive() method I can't get requestcode
You should add your SMS id to the PendingIntent like this:
Intent deliveryIntent = new Intent(DELIVERED);
deliveryIntent.putExtra("id", id);
deliveredPI = PendingIntent.getBroadcast(this, id, deliveryIntent, 0);
Then, in onReceive() you can get the SMS id like this:
int id = intent.getIntExtra("id", -1);
if (id >= 0) {
// Got a valid id...
}
I have a very strange issue: my apps can send text message and many users reports they pay the sms even when their subscription can send sms for free. The sms is sended to a national phonenumber, a classic smartphone (a friend).
Here the code:
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);
SmsManager sms = SmsManager.getDefault();
sms.sendTextMessage(phoneNumber, null, message, sentPI, deliveredPI);
MyLog.log("send message to " + phoneNumber + " string: " + message);
}
I asked my carrier (WIND ITALY) what the problem was, and they told me that it seems that the number was international and the sms plan do not cover the international sms.
The numbers that I've tried are all local, no one is international. Change a Carrier is not the solution ... i try this with all of the national carrier.
Someone out there have an advice ?
I am using the below code to call an MMS intent :
{
Intent mmsIntent = new Intent(Intent.ACTION_SEND);
mmsIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
mmsIntent.putExtra("address", temp);
mmsIntent.putExtra("sms_body", msgstr);
mmsIntent.putExtra(Intent.EXTRA_STREAM, mediaUri);
}
Here 'temp' is the string containing multiple numbers and differentiated with ';'. It's working fine when we use this code for only a single number but when i add multiple numbers it doesn't attached to the messaging app. I have tried the same thing with ',' to separate the phone numbers but it also doesn't work. Any help is appreciated.
Intent mmsIntent= new Intent(Intent.ACTION_SENDTO,Uri.parse("smsto:9858254511;9858526521"));
mmsIntent.putExtra("sms_body", msgstr);
mmsIntent.putExtra(Intent.EXTRA_STREAM, mediaUri);
startActivity(smsIntent);
Add a semicolon delimited list of phone numbers to "smsto:" as the URI in the Intent constructor. Also refer this LINK
protected void sendMsg(Context context, SmsMessage smsMessage) {
SmsManager smsMgr = SmsManager.getDefault();
ArrayList<string> smsMessageText = smsMgr.divideMessage(smsMessage.getMsgBody());
PendingIntent sentPI = PendingIntent.getBroadcast(context, 0, new Intent("SMS_SENT"), 0);
PendingIntent deliveredPI = PendingIntent.getBroadcast(context, 0, new Intent("SMS_DELIVERED"), 0);
int AddresseesPerMessage = 10;
StringBuilder builder = new StringBuilder();
String delim = "";
for (ContactItem c:smsMessage.getAddresseeList()) {
// For every phone number in our list
builder.append(delim).append(c.getPhoneNumber().toString());
delim=";";
if (((smsMessage.getAddresseeList().indexOf(c)+1) % AddresseesPerMessage) == 0 || smsMessage.getAddresseeList().indexOf(c)+1 == smsMessage.getAddresseeList().size()) {
// using +1 because index 0 mod 9 == 0
for(String text : smsMessageText){
// Send 160 bytes of the total message until all parts are sent
smsMgr.sendTextMessage(builder.toString(), null, text, sentPI, deliveredPI);
}
builder.setLength(0);
delim="";
}
}
}
use this code...i hope its useful to you.
I created simple code to send SMS which is working on Xperia U and QMobile (local brand).
But it is not working on Samsung Galaxy S3 LTE
They code is
import android.telephony.SmsManager;
SmsManager sms = SmsManager.getDefault();
PendingIntent sentPI;
String SENT = "SMS_SENT";
sentPI = PendingIntent.getBroadcast(activity, 0,new Intent(SENT), 0);
sms.sendTextMessage("01234567890", null, msg, sentPI, null);
first be sure to add the permission to send SMSs
<uses-permission android:name="android.permission.SEND_SMS" />
and then surround your code with try and catch to find the error that prevents sending in Samsung s3 lte ..
try {
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage("01234567890", null, msg, sentPI, null);
Toast.makeText(getApplicationContext(), "SMS Sent!",
Toast.LENGTH_LONG).show();
} catch (Exception e) {
Toast.makeText(getApplicationContext(),
"SMS faild, please try again later!",
Toast.LENGTH_LONG).show();
e.printStackTrace();
}
You have missed to call PendingIntent deliveredPI
Try this code..!!
PendingIntent sentPI =PendingIntent.getBroadcast(activity, 0,new Intent(SENT), 0);
PendingIntent deliveredPI = PendingIntent.getBroadcast(this, 0,new Intent(DELIVERED), 0);
sms.sendTextMessage("01234567890", null, msg, sentPI, deliveredPI);
You can try using this library android-smsmms to send SMS
Settings sendSettings = new Settings();
sendSettings.setDeliveryReports(true);
sendSettings.setSplit(true);
Message mMessage = new Message(textToSend, addressToSendTo);.
sendTransaction.sendNewMessage(message, null)
Hope this helps you
EDIT
com.klinker.android.send_message.Settings sendSettings = new com.klinker.android.send_message.Settings();
sendSettings.setDeliveryReports(true);
sendSettings.setSplit(true);
sendSettings.setSplitCounter(true);
sendSettings.setStripUnicode(true);
com.klinker.android.send_message.Transaction sendTransaction = new com.klinker.android.send_message.Transaction(getApplicationContext(), sendSettings);
com.klinker.android.send_message.Message mMessage = new com.klinker.android.send_message.Message("Message", "9999999999");
sendTransaction.sendNewMessage(mMessage, 0);
i have read multiple threads on how to send and receive multipart messages. I have implemented the following code and it works!
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(SMSBroadcastReceiver1, new IntentFilter(SENT));
//---when the SMS has been delivered---
registerReceiver(SMSBroadcastReceiver2, new IntentFilter(DELIVERED));
SmsManager smsManager = SmsManager.getDefault();
ArrayList<String> parts = smsManager.divideMessage(smsToSend);
ArrayList<PendingIntent> sentList = new ArrayList<PendingIntent>();
ArrayList<PendingIntent> deliveredList = new ArrayList<PendingIntent>();
for (int i = 0; i < parts.size(); i++) {
sentList.add(sentPI);
deliveredList.add(deliveredPI);
}
//smsManager.sendTextMessage(phoneNumber, null, smsToSend, sentPI, deliveredPI);
smsManager.sendMultipartTextMessage(phoneNumber, null, parts, sentList, deliveredList);
and i have one registered SMSBroadcastReceiver2 and SMSBroadcastReceiver1.
The thing that worries me is:
I have one single PendingIntent sentPi and deliveredPi, that are registered with SMSBroadcastReceiver1 and SMSBroadcastReceiver2.
And then i put them in ArrayList's multiple times, depending on how long is the message.
Is this a good thing? Or should i have different Intents and Receivers
for each part of the message.
Also when do receivers fire in my code? I have noticed it only fires once when the message is sent and once when its received(i coded them to display Toast Messages at those moments), no mater how long the message is. Is it when the first part is delivered or last...?