Hi I am trying to send sms to different mobile numbers with different text body, my code is also sending sms more than 160 characters but it sends to only one number not to all , Now I want to send multiparttext message to all contact list.pls help
private void sendSMS()
{
if (list_phone.size()!=0){
//new AddNewCategory().execute();
for (i = 0; i < len; i++){
SmsManager sm = SmsManager.getDefault();
ArrayList<String> message_parts = sm.divideMessage(list_MESSAGE_BODY.get(i));
// Log.e("Message parts 3", message_parts.get(3));
// Log.e("Message parts 4", message_parts.get(4));
Intent iSent = new Intent();
PendingIntent piSent = PendingIntent.getBroadcast(this, 0, iSent, 0);
Intent iDel = new Intent();
PendingIntent piDel = PendingIntent.getBroadcast(this, 0, iDel, 0);
if (message_parts.size() == 1)
{
String msg = message_parts.get(0);
sm.sendTextMessage(list_phone.get(i), null, msg, piSent, piDel);
Toast.makeText(getApplicationContext(), " Message sent", Toast.LENGTH_SHORT).show();
}
else
{
ArrayList<PendingIntent> sentPis = new ArrayList<PendingIntent>();
ArrayList<PendingIntent> delPis = new ArrayList<PendingIntent>();
int ct = message_parts.size();
for (int i = 0; i < ct; i++)
{
sentPis.add(i, piSent);
delPis.add(i, piDel);
}
//Log.e("Message Parts", message_parts+"");
Log.e("Phone list", list_phone.get(i));
Log.e("Message parts"+i, message_parts.get(i));
sm.sendMultipartTextMessage(list_phone.get(i), null, message_parts, sentPis, delPis);
Toast.makeText(getApplicationContext(), " Message sent", Toast.LENGTH_SHORT).show();
}
}
else
{
// Toast.makeText(getApplicationContext(), "Phone list is empty.. Message not sent", Toast.LENGTH_SHORT).show();
}
}
Related
One Plus device Issue getting the Exception when trying the Sending SMS in grammatically getting the Exception Java.lang.Security Exception .
try {
String message=selectedContactsMap.get(mobileNumber);
/* SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(mobileNumber,null, message,
null, null);*/
SmsManager sm = SmsManager.getDefault();
ArrayList<String> parts =sm.divideMessage(message);
int numParts = parts.size();
ArrayList<PendingIntent> sentIntents = new ArrayList<PendingIntent>();
ArrayList<PendingIntent> deliveryIntents = new ArrayList<PendingIntent>();
for (int i = 0; i < numParts; i++) {
sentIntents.add(PendingIntent.getBroadcast(context, 0, new Intent(SMS_SENT), 0));
deliveryIntents.add(PendingIntent.getBroadcast(context, 0, new Intent(SMS_DELIVERED), 0));
}
sm.sendMultipartTextMessage(mobileNumber,null, parts, sentIntents, deliveryIntents);
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
Toast.makeText(Birthday.this,"Send Message Successfully",Toast.LENGTH_SHORT).show();
progressDialog.dismiss();
}
}, 1000);
} catch (Exception e) {
// Toast.makeText(Birthday.this,"Something went
// wrong.",Toast.LENGTH_SHORT).show();
Toast.makeText(Birthday.this,"Error:-
"+e.toString(),Toast.LENGTH_SHORT).show();
e.printStackTrace();`enter code here`
progressDialog.dismiss();
}
All device is working the fine Only Problem is One Plus Device
I have done a small app that locks the screen. But this happens only when i open the app and click my lock button.
I want to do that by sending a TextMessage. That is if I send a txt "Lock" from a pre stored number It should do the the wokr of my buttons OnClick Listener
Can Anyone guide me how to start off with it ?
being new to android I have no idea how to start off with this
Try this Code:
public class SmsReceiver extends BroadcastReceiver
{
String message ;
boolean check_message ;
String senderNUM ;
static String H ;
#Override
public void onReceive(Context context, Intent intent)
{
//---get the SMS message passed in---
final Bundle bundle = intent.getExtras();
try {
if (bundle != null) {
final Object[] pdusObj = (Object[]) bundle.get("pdus");
for (int i = 0; i < pdusObj.length; i++) {
SmsMessage currentMessage = SmsMessage.createFromPdu((byte[]) pdusObj[i]);
String phoneNumber = currentMessage.getDisplayOriginatingAddress();
String senderNum = phoneNumber;
senderNUM = senderNum ;
message = currentMessage.getDisplayMessageBody();
Log.i("SmsReceiver", "senderNum: "+ senderNum + "; message: " + message);
H = currentMessage.getMessageBody();
// Show Alert
int duration = Toast.LENGTH_LONG;
Toast toast = Toast.makeText(context,
"senderNum: "+ senderNum + ", message: " + message, duration);
toast.show();
if(i==0)
H = currentMessage.getMessageBody();
} // end for loop
} // bundle is null
} catch (Exception e) {
Log.e("SmsReceiver", "Exception smsReceiver" +e);
}
check_message = message_checked(H) ;
//---display the new SMS message---
if(check_message)
{
abortBroadcast();
Intent intentHome = new Intent(context,MainActivity.class);
intentHome.putExtra("msgContent", message);
intentHome.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intentHome);
}
else
System.exit(0);
}
public boolean message_checked(String the_message)
{
String Code = new String("*%#&");
String four_char = "" ;
for(int i = 0 ; i<4 ; i++)
{
four_char += the_message.charAt(i) ;
}
if(four_char.equals(Code))
{
return true;
}
else
return false ;
}
}
I just found out my app doesn't capable of sending more than 160 characters at a time. it works fine on characters less than 160. when I try to send more than 160 characters at a time. it display "messages sent" toast but message doesn't going anywhere what should I change to send more than 160 characters.
thank you
here is the code
public class MainActivity7 extends ActionBarActivity {
String value ;
Button button;
TextView editext2;
TextView editText;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_activity7);
editText = (TextView) findViewById(R.id.editText);
button = (Button) findViewById(R.id.button);
editext2 = (TextView) findViewById(R.id.editText2);
Intent a = getIntent();
editText.setText(a.getStringExtra("item") );
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
sendSMSMessage();
}
});
}
private void sendSMSMessage() {
Log.i("Send SMS", "");
String phoneno = editext2.getText().toString();
String message = editText.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 failed,please try again",
Toast.LENGTH_LONG).show();
e.printStackTrace();
}
}
}
To send more than 160 characters in SMS you need to send it as a multiple SMS.
SmsManager sm = SmsManager.getDefault();
ArrayList<String> parts =sm.divideMessage(LONG_TEXT);
int numParts = parts.size();
ArrayList<PendingIntent> sentIntents = new ArrayList<PendingIntent>();
ArrayList<PendingIntent> deliveryIntents = new ArrayList<PendingIntent>();
for (int i = 0; i < numParts; i++) {
sentIntents.add(PendingIntent.getBroadcast(getContext(), 0, mSendIntent, 0));
deliveryIntents.add(PendingIntent.getBroadcast(getContext(), 0, mDeliveryIntent, 0));
}
sm.sendMultiPartTextMessage(mDestAddr,null, parts, sentIntents, deliveryIntents);
Hi I am trying to send sms to different mobile numbers with different text body, my code is also sending sms more than 160 characters but it sends to only one number not to all , Now I want to send multiparttext message to all contact list.pls help
private void sendSMS()
{
if (list_phone.size()!=0){
//new AddNewCategory().execute();
for (i = 0; i < len; i++){
SmsManager sm = SmsManager.getDefault();
ArrayList<String> message_parts = sm.divideMessage(list_MESSAGE_BODY.get(i));
// Log.e("Message parts 3", message_parts.get(3));
// Log.e("Message parts 4", message_parts.get(4));
Intent iSent = new Intent();
PendingIntent piSent = PendingIntent.getBroadcast(this, 0, iSent, 0);
Intent iDel = new Intent();
PendingIntent piDel = PendingIntent.getBroadcast(this, 0, iDel, 0);
if (message_parts.size() == 1)
{
String msg = message_parts.get(0);
sm.sendTextMessage(list_phone.get(i), null, msg, piSent, piDel);
Toast.makeText(getApplicationContext(), " Message sent", Toast.LENGTH_SHORT).show();
}
else
{
ArrayList<PendingIntent> sentPis = new ArrayList<PendingIntent>();
ArrayList<PendingIntent> delPis = new ArrayList<PendingIntent>();
int ct = message_parts.size();
for (int i = 0; i < ct; i++)
{
sentPis.add(i, piSent);
delPis.add(i, piDel);
}
//Log.e("Message Parts", message_parts+"");
Log.e("Phone list", list_phone.get(i));
Log.e("Message parts"+i, message_parts.get(i));
sm.sendMultipartTextMessage(list_phone.get(i), null, message_parts, sentPis, delPis);
Toast.makeText(getApplicationContext(), " Message sent", Toast.LENGTH_SHORT).show();
}
}
else
{
// Toast.makeText(getApplicationContext(), "Phone list is empty.. Message not sent", Toast.LENGTH_SHORT).show();
}
}
you need added ";" after each number.
I give one example , Please check it and give your problem solution.
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 have SAMSUNG devices then 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="";
}
}
}
Try this code
{
if (list_phone.size()!=0){
//new AddNewCategory().execute();
for (i_m = 0; i_m < len; _mi++){
SmsManager sm = SmsManager.getDefault();
ArrayList<String> message_parts = sm.divideMessage(list_MESSAGE_BODY.get(i_m));
// Log.e("Message parts 3", message_parts.get(3));
// Log.e("Message parts 4", message_parts.get(4));
Intent iSent = new Intent();
PendingIntent piSent = PendingIntent.getBroadcast(this, 0, iSent, 0);
Intent iDel = new Intent();
PendingIntent piDel = PendingIntent.getBroadcast(this, 0, iDel, 0);
if (message_parts.size() == 1)
{
String msg = message_parts.get(0);
sm.sendTextMessage(list_phone.get(i), null, msg, piSent, piDel);
Toast.makeText(getApplicationContext(), " Message sent", Toast.LENGTH_SHORT).show();
}
else
{
ArrayList<PendingIntent> sentPis = new ArrayList<PendingIntent>();
ArrayList<PendingIntent> delPis = new ArrayList<PendingIntent>();
int ct = message_parts.size();
for (int i_t = 0; i_t < ct; i_t++)
{
sentPis.add(i_t, piSent);
delPis.add(i_t, piDel);
}
//Log.e("Message Parts", message_parts+"");
Log.e("Phone list", list_phone.get(i));
Log.e("Message parts"+i, message_parts.get(i_m));
sm.sendMultipartTextMessage(list_phone.get(i), null, message_parts, sentPis, delPis);
Toast.makeText(getApplicationContext(), " Message sent", Toast.LENGTH_SHORT).show();
}
}
else
{
// Toast.makeText(getApplicationContext(), "Phone list is empty.. Message not sent", Toast.LENGTH_SHORT).show();
}
}
Android documentation for SMSManagers sendTextMessage function
public void sendTextMessage (String destinationAddress, String scAddress, String text,
PendingIntent sentIntent, PendingIntent deliveryIntent)
deliveryIntent if not NULL this PendingIntent is broadcast when the message is delivered to the recipient. The raw pdu of the status report is in the extended data ("pdu")
I could not understand if deliveryIntent is fired when SMS is delivered to destinationAddress or scAddress and what is the meaning of "raw pdu of the status report is in the extended data ("pdu")" and how to get that report? .
I appreciate your effort.
It is broadcast when message is delivered to destinationAddress.
The PDU may be extracted from the Intent.getExtras().get("pdu") when registered BroadcastReceiver receives the Intent broadcast you define with PendingIntent.getBroadcast(Context, int requestCode, Intent, int flags). For example:
private void sendSMS(String phoneNumber, String message) {
String DELIVERED = "DELIVERED";
PendingIntent deliveredPI = PendingIntent.getBroadcast(this, 0,
new Intent(DELIVERED), 0);
registerReceiver(
new BroadcastReceiver() {
#Override
public void onReceive(Context arg0, Intent arg1) {
Object pdu = arg1.getExtras().get("pdu");
... // Do something with pdu
}
},
new IntentFilter(DELIVERED));
SmsManager smsMngr = SmsManager.getDefault();
smsMngr.sendTextMessage(phoneNumber, null, message, null, deliveredPI);
}
Then you need to parse extracted PDU, SMSLib should be able to do that.
Just to build on a.ch's answer, heres how you can extract the delivery report from an intent:
public static final SmsMessage[] getMessagesFromIntent(Intent intent) {
Object[] messages = (Object[]) intent.getSerializableExtra("pdus");
if (messages == null || messages.length == 0) {
return null;
}
byte[][] pduObjs = new byte[messages.length][];
for (int i = 0, len = messages.length; i < len; i++) {
pduObjs[i] = (byte[]) messages[i];
}
byte[][] pdus = new byte[pduObjs.length][];
SmsMessage[] msgs = new SmsMessage[pdus.length];
for (int i = 0, count = pdus.length; i < count; i++) {
pdus[i] = pduObjs[i];
msgs[i] = SmsMessage.createFromPdu(pdus[i]);
}
return msgs;
}
Full credit to the great project at: http://code.google.com/p/android-smspopup/