Showing posts with label mo sms. Show all posts
Showing posts with label mo sms. Show all posts

Friday, December 20, 2013

Android: How to send SMS


Add Permission into Manifest

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



Add API into Source
private void sendSMS(String m_phoneNumber, final String m_smsMessage)
{      
    String SENT         = "SMS_SENT";
    String DELIVERED = "SMS_DELIVERED";
     
    Log.d(TAG, "In sendSMS()");

    PendingIntent sentPI    
                             = PendingIntent.getBroadcast(this, 0, new Intent(SENT),      0);
    PendingIntent deliveredPI
                             = PendingIntent.getBroadcast(this, 0, new Intent(DELIVERED), 0);
             
    Log.d(TAG, "sendTextMessage() : number = " + m_phoneNumber
                                                                     + ", Message = " + m_smsMessage);
     
    // Write SMS data into DB
    ContentValues values = new ContentValues();
    values.put("address",   m_phoneNumber.replace("-", ""));
    values.put("body",       m_smsMessage);
    values.put("protocol",  MESSAGE_TYPE_SENT);
    getContentResolver().insert(Uri.parse("content://sms/sent"), values);
     
    // Send Text Message
    SmsManager.getDefault().sendTextMessage(m_phoneNumber.replace("-", ""),
                                                               null, m_smsMessage, sentPI, deliveredPI);
}

Android: Group SMS which is based on contact group

This post is just for introduce "Group SMS" application.
Sometimes, we need to send SMS to my customer, friends or anyone.
In this case, this application can help you to send SMS to people.

  

  

We can input prefix and postfix about each contact name and message body.
For example,
   Prefix               : Dear
   Contact Name  : Victor, Crum
   Postfix             : .
   Message Body : Thanks for your APP.

   Then 2 messages will be made automatically as below.
   - Dear Victor.
      Thanks for your APP.
   - Dear Crum.
      Thanks for your APP.

   Finally, this messages will be sent to each person.


You can download this APP for nothing as the below path.
App Download

Android: Saving SMS data into DB

We can send the SMS by using the below API.
    SmsManager.getDefault().sendTextMessage()

But it is not saved into the conversation list.
For this reason, we can't know what message is sent.

Now, I wanna let you know how to save it.

1. Add <uses-permission>

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


2. Add Code
    public class TestActivity extends Activity {

        public static final int MESSAGE_TYPE_ALL    = 0;
        public static final int MESSAGE_TYPE_INBOX  = 1;
        public static final int MESSAGE_TYPE_SENT   = 2;
        public static final int MESSAGE_TYPE_DRAFT  = 3;
        public static final int MESSAGE_TYPE_OUTBOX = 4;
        public static final int MESSAGE_TYPE_FAILED = 5; // for failed outgoing messages
        public static final int MESSAGE_TYPE_QUEUED = 6; // for messages to send later



       @Override
        protected void onCreate(Bundle savedInstanceState) {

             ContentValues values = new ContentValues();

             values.put("address",  m_phoneNumber.replace("-", ""));
             values.put("body",       m_smsMessage);
             values.put("protocol",  MESSAGE_TYPE_SENT);
             getContentResolver().insert(Uri.parse("content://sms/sent"), values);

    In this point, "content://sms/sent" is really really important.
    Please take a look at the below picture.
    The left side is MT SMS and right side is MO SMS.

    "content://sms/sent"   : Save as MO SMS
    "content://sms/inbox" : Save as MT SMS