14

In my application I do not want to use the default message sender. For doing that I followed the following link In Android is it possible to send sms message to more than one recipient in code?

  • And that code worked too. But the messages I am sending from this code are not saved on the phones outbox and inbox.
  • I am using sms manager like this in my code

    SmsManager sms = SmsManager.getDefault(); sms.sendTextMessage(phoneNumber, null, message, null, null);

But it is not sending sms.please help me with how can i send sms in android - i have tried following too 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);

It's also not working.
SMSAPPActivity.java

EDIT :

btnSendSMS.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                String message = txtMessage.getText().toString();
                String[] PhnNoArray = new String[2];
                PhnNoArray[0] = "9999999999";
                PhnNoArray[1] = "8888888888";
                // StringTokenizer st = new StringTokenizer(phoneNo, ",");
                smsManager = SmsManager.getDefault();
                for (int i = 0; i < PhnNoArray.length; i++) {
                    smsManager = SmsManager.getDefault();
                        // this is the function that does all the magic
//                      sms.sendTextMessage(phoneNumber, null, msg, pi, null);
                    smsManager.sendTextMessage(PhnNoArray[i], null, message, null,
                            null);
                    Toast.makeText(getBaseContext(), "SMS sent : " + i,
                            Toast.LENGTH_SHORT).show();
                }
}
        });

Please see the edit and tell me what i have done wrong.tost is showing up but sms is not received on other phone by using this code

Community
  • 1
  • 1
Shruti
  • 1
  • 13
  • 55
  • 95
  • what do you mean "default message sender" ? Please clarify – JoxTraex Jan 25 '12 at 07:15
  • @JoxTraex : by default message sender i mean i do not want to open system's messaging app to send the message i want to send the message from my application only... –  Jan 25 '12 at 08:06
  • I am doing something similar HERE!!! http://stackoverflow.com/questions/14452808/sending-and-receiving-sms-mms-in-android – Etienne Lawlor Jan 22 '13 at 20:46

5 Answers5

4

If you are using dual sim device then you must have to mention the sender number, You can't pass null at that time. Otherwise, SmsManager will throw an error called SmsManager.RESULT_ERROR_GENERIC_FAILURE.

Code to check numbers of active sims:

public static int getNumberOfActiveSim(Context context) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP_MR1) {
                SubscriptionManager subscriptionManager = SubscriptionManager.from(context);
                List<SubscriptionInfo> subscriptionInfo = subscriptionManager.getActiveSubscriptionInfoList();
                return subscriptionInfo != null ? subscriptionInfo.size() : 0;
            }
            TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
            if(telephonyManager != null && telephonyManager.getSimState() == TelephonyManager.SIM_STATE_ABSENT){
                return 1;
            }
            return 0;
        }
Shahbaz Akhtar
  • 303
  • 2
  • 9
3

1) add messages in Sent instead of Outbox, as Outbox contains messages which are suppose to send or in sending state.

2) when you send message add them at the same time in "content://sms/sent uri.

what is stopping u to store them in database. and what you tried yet.

use below code to sendSMS

 smsManager.sendTextMessage(number, null,desc, null, null);

and by using content://sms/sent URI, you can insert the same text message into Message database

AAnkit
  • 27,299
  • 12
  • 60
  • 71
  • :thanks for replying.. see my edit i have added code too it shows toast for sms sent –  Jan 25 '12 at 08:02
  • i edited the answer please go through it once. the above line "content://sms/sent" is a content uri, which points to the database table of sent messages, insert your message in this URI, so the message will sent and inserted into message db which reflects into message app too... – AAnkit Jan 25 '12 at 08:29
  • but i have to send message to multiple numbers –  Jan 25 '12 at 08:36
  • hi @Ankita i tried to send msg using loop but its throwing error please see my edited code .. –  Jan 25 '12 at 12:54
  • 1st its Ankit A, not Ankita, 2nd is this error coming in very first message. as error says the destination address is not correct. check it.or print your destination address/phoneNumber in the loop itself by Log.d() dont generate toast unnecessarily. 3rd do you have SMS send permission., – AAnkit Jan 25 '12 at 14:15
2

Hope this can help you.

MainActivity.java

import android.os.Bundle;
import android.app.Activity;
import android.telephony.SmsManager;
import android.view.Menu;
import android.view.inputmethod.InputMethodManager;
import android.widget.*;
import android.view.View.OnClickListener;
import android.view.*;


public class MainActivity extends Activity implements OnClickListener{


Button click;
EditText txt;
TextView txtvw;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

click = (Button)findViewById(R.id.button);
txt = (EditText)findViewById(R.id.editText);
txtvw = (TextView)findViewById(R.id.textView1);

click.setOnClickListener(this);
}

@Override
public void onClick(View v){


txt.setText("");
v = this.getCurrentFocus();

try{
    SmsManager sms = SmsManager.getDefault();
    sms.sendTextMessage("8017891398",null,"Sent from Android",null,null);
}
catch(Exception e){
    txtvw.setText("Message not sent!");
}
if(v != null){
    InputMethodManager imm =   (InputMethodManager)getSystemService(INPUT_METHOD_SERVICE);
    imm.hideSoftInputFromWindow(v.getWindowToken(),0);
  }

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
   }

}

add this line in AndroidManifest.xml

<uses-permission android:name="android.permission.SEND_SMS" />

enter image description here

Charuක
  • 12,953
  • 5
  • 50
  • 88
Sudip Das
  • 1,178
  • 1
  • 9
  • 24
1

just send it directly... using the SmsManager. Only problem is that is that the user won't know of it.

Charuක
  • 12,953
  • 5
  • 50
  • 88
JoxTraex
  • 13,423
  • 6
  • 32
  • 45
0

In my case, it was the fact that the message body exceeded 160 characters and I had to use sendMultipartTextMessage in lieu of sendTextMessage.

Iman Akbari
  • 2,167
  • 26
  • 31