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);
}

No comments:

Post a Comment