0

I need to open the sms application from my application so the user can send a message and I need to get the status of the message even it was sent or not. Is it possible?

I know how to define broadcast receivers to send a message programmatically but is it possible to do it using the sms application?

Thanks!

  • why do you need to do it using the sms application? – cYrixmorten Oct 14 '13 at 20:53
  • Because I need to allow the user to select a contact. I would open the sms application with a meesage already written but without a receiver. – cheloperel Oct 14 '13 at 20:55
  • I see.. well the tricky part is to get the status of the sms in that case. If you would make your own Activity that enables selecting a contact, it would be possible to get SENT and DELIVERED callbacks plus if an error occured. But if you call another texting app, there is no guarantees as to which broadcasts to listen for to get this information. – cYrixmorten Oct 14 '13 at 21:01
  • So you say that using the sms application it's not possible to get the status?? – cheloperel Oct 14 '13 at 21:55
  • I am afraid so, as you, I think, cannot be certain how to retrieve the status, as different sms applications might use different intents for the status. Not that I know for sure, perhaps the default sms application has the same behaviour on all devices, which would mean that you would know exactly which broadcasts to listen for. I really think the best option would be to write you own contact selection combined with programmatically sending of sms with SENT and DELIVERED pending intents like: http://stackoverflow.com/questions/19083158/send-sms-until-it-is-successful/19084559#19084559 – cYrixmorten Oct 14 '13 at 22:56

1 Answers1

0

You can send msg programmatically

SmsManager smsManager = SmsManager.getDefault();

smsManager.sendTextMessage(phoneNo, null, sms, null, null);

Toast.makeText(getApplicationContext(), "SMS Sent!",Toast.LENGTH_LONG).show();

For Sms Receiver

public class SmsReceiver extends BroadcastReceiver {

public static final String SMS_EXTRA_NAME = "pdus";
public static final String SMS_URI = "content://sms";

public void onReceive( Context context, Intent intent ) 
{
    // Get SMS map from Intent
    Bundle extras = intent.getExtras();

    if ( extras != null )
    {
        // Get received SMS array
        Object[] smsExtra = (Object[]) extras.get( SMS_EXTRA_NAME );

        for ( int i = 0; i < smsExtra.length; ++i )
        {
            SmsMessage sms = SmsMessage.createFromPdu((byte[])smsExtra[i]);

            if(IsBlackListNumber(context,sms)){
                this.abortBroadcast();
            //  Toast.makeText( context, "BlackList", Toast.LENGTH_SHORT ).show();
            }
            else{
                //Toast.makeText( context, "No BlackList", Toast.LENGTH_SHORT ).show();
            }
        }
    }

    // WARNING!!! 
    // If you uncomment next line then received SMS will not be put to incoming.
    // Be careful!
    // this.abortBroadcast(); 
}

private boolean IsBlackListNumber(Context context, SmsMessage sms){
      boolean isExist = false;

    ContactInfoDataSource datasource = new ContactInfoDataSource(context);
    datasource.read();
    if(datasource.IsBlackListNumber(sms.getOriginatingAddress())){

        SmsInfoEnt smsInfoEnt = new SmsInfoEnt();
        smsInfoEnt.setMessage(sms.getMessageBody());
        smsInfoEnt.setName(datasource.GetName(sms.getOriginatingAddress()));
        smsInfoEnt.setPhoneNo(sms.getOriginatingAddress());

        SmsInfoDataSource Smsdatasource = new SmsInfoDataSource(context);
        Smsdatasource.open();
        Smsdatasource.AddBlockSMS(smsInfoEnt);
        Smsdatasource.close();
        isExist = true;
    }
    datasource.close();

    return isExist;
}
}
rene
  • 41,474
  • 78
  • 114
  • 152