{"id":21,"date":"2013-01-16T03:41:35","date_gmt":"2013-01-16T03:41:35","guid":{"rendered":"https:\/\/code4reference.com\/?p=21"},"modified":"2013-01-16T03:41:35","modified_gmt":"2013-01-16T03:41:35","slug":"code4referencetutorial-on-android-application-for-sending-sms","status":"publish","type":"post","link":"https:\/\/code4reference.com\/?p=21","title":{"rendered":"Code4ReferenceTutorial on Android application for sending sms"},"content":{"rendered":"<p>I have been playing around with Android application in past. I thought of writing a simple application which can send a SMS.<\/p>\n<p>Android has provided <em><strong>android.telephony.SmsManager<\/strong><\/em> class which has exposed different methods for sending SMS. If the message size is less than 160 character then we can use the the following method.<\/p>\n<pre>sendTextMessage(String destinationAddress, String scAddress,\n        String text, PendingIntent sentIntent, PendingIntent deliveryIntent)\n<\/pre>\n<p>If you just want to send the message and not bothered about the send result then just pass the destinalAddress(MDN) and text(Message).<\/p>\n<p>Here is the code snippet for main Activity class.<\/p>\n<pre>package com.rakesh.simpleSms;\nimport com.rakesh.broadcastreceiver.DeliverSMSBroadcastReceiver;\nimport com.rakesh.broadcastreceiver.OutgoingSMSBroadcastReceiver;\nimport android.app.Activity;\nimport android.app.PendingIntent;\nimport android.content.BroadcastReceiver;\nimport android.content.Intent;\nimport android.content.IntentFilter;\nimport android.os.Bundle;\nimport android.telephony.SmsManager;\nimport android.util.Log;\nimport android.view.View;\nimport android.widget.EditText;\nimport android.widget.Toast;\n\npublic class SimpleSMSAppActivity extends Activity {\n private EditText etNumber;\n private EditText etMessage;\n private SmsManager manager;\n public static final int  MAX_MESSAGE_SIZE = 160;\n public static final String SMS_SENT = \"SMS_SENT\";\n public static final String SMS_DELIVERED = \"SMS_DELIVERED\";\n private final BroadcastReceiver outgoingSMSBR = new OutgoingSMSBroadcastReceiver();\n private final BroadcastReceiver deliverSMSBR = new DeliverSMSBroadcastReceiver();\n @Override\n public void onCreate(Bundle savedInstanceState) {\n  super.onCreate(savedInstanceState);\n  setContentView(R.layout.main);\n  \/\/  btSendMessage = (Button)findViewById(R.id.btSendMessage);\n  etNumber = (EditText)findViewById(R.id.etNumber);\n  etMessage = (EditText)findViewById(R.id.etMessage);\n  manager =  SmsManager.getDefault();\n }\n\n public void sendMessage(View view){\n  String number = etNumber.getText().toString();\n  String message = etMessage.getText().toString();\n  Log.d(\"RK\",\"number : \"+number+\", message : \" + message  );\n  if(!isNullOrEmpty(number) &amp;&amp; !isNullOrEmpty(message)){\n   if(message.length() &gt; MAX_MESSAGE_SIZE){\n    Toast.makeText(this,\"Message is longer then allowed in SMS\",Toast.LENGTH_LONG).show();\n   } else{\n    PendingIntent piSend = PendingIntent.getBroadcast(this, 0, new Intent(SMS_SENT),0);\n    PendingIntent piDelivered = PendingIntent.getBroadcast(this, 0, new Intent(SMS_DELIVERED), 0);\n    manager.sendTextMessage(number, null, message, piSend, piDelivered);\n    etMessage.setText(\"\");\n   }\n  }\n }\n @Override\n protected void onResume() {\n  registerReceiver(outgoingSMSBR, new IntentFilter(SMS_SENT));\n  registerReceiver(deliverSMSBR, new IntentFilter(SMS_DELIVERED));\n  super.onResume();\n }\n @Override\n protected void onPause() {\n  unregisterReceiver(outgoingSMSBR);\n  unregisterReceiver(deliverSMSBR);\n  super.onPause();\n }\n private boolean isNullOrEmpty(String string){\n  return string == null || string.isEmpty();\n }\n}\n<\/pre>\n<p>and two broadcast receiver classes are as following.<\/p>\n<p>OutgoingSMSBroadcastReceiver.java<\/p>\n<pre>\npublic class OutgoingSMSBroadcastReceiver extends BroadcastReceiver {\n @Override\n public void onReceive(Context context, Intent intent) {\n  switch(getResultCode()){\n   case Activity.RESULT_OK:\n      Toast.makeText(context, \"SMS sent\",\n                 Toast.LENGTH_SHORT).show();\n      Log.d(\"RK\",\"RESULT_OK\");\n         break;\n      case SmsManager.RESULT_ERROR_GENERIC_FAILURE:\n          Toast.makeText(context, \"Generic failure\",\n                  Toast.LENGTH_SHORT).show();\n          Log.d(\"RK\",\"RESULT_ERROR_GENERIC_FAILURE\");\n          break;\n      case SmsManager.RESULT_ERROR_NO_SERVICE:\n          Toast.makeText(context, \"No service\",\n                  Toast.LENGTH_SHORT).show();\n          Log.d(\"RK\",\"RESULT_ERROR_NO_SERVICE\");\n          break;\n      case SmsManager.RESULT_ERROR_NULL_PDU:\n          Toast.makeText(context, \"Null PDU\",\n                  Toast.LENGTH_SHORT).show();\n          Log.d(\"RK\",\"RESULT_ERROR_NULL_PDU\");\n          break;\n      case SmsManager.RESULT_ERROR_RADIO_OFF:\n          Toast.makeText(context, \"Radio off\",\n                  Toast.LENGTH_SHORT).show();\n          Log.d(\"RK\",\"RESULT_ERROR_RADIO_OFF\");\n          break;\n  }\n }\n}\n<\/pre>\n<p>DeliverSMSBroadcastReceiver.java<\/p>\n<pre>\npackage com.rakesh.broadcastreceiver;\nimport android.app.Activity;\nimport android.content.BroadcastReceiver;\nimport android.content.Context;\nimport android.content.Intent;\nimport android.util.Log;\nimport android.widget.Toast;\n\npublic class DeliverSMSBroadcastReceiver extends BroadcastReceiver {\n @Override\n public void onReceive(Context context, Intent intent) {\n          switch (getResultCode())\n            {\n             case Activity.RESULT_OK:\n                Toast.makeText(context, \"SMS delivered\",\n                        Toast.LENGTH_SHORT).show();\n                Log.d(\"RK\",\"RESULT_OK=&gt; DELIVER\");\n                break;\n             case Activity.RESULT_CANCELED:\n                Toast.makeText(context, \"SMS not delivered\",\n                        Toast.LENGTH_SHORT).show();\n                Log.d(\"RK\",\"RESULT_CANCELED\");\n                break;\n            }\n }\n}\n<\/pre>\n<p>Make sure you have provided the right permission to application in manifest file.<\/p>\n<pre>\n\n    \n \n    \n        \n        \n            \n                \n                \n           \n        \n    \n\n<\/pre>\n<p>You can find the source code <a href=\"http:\/\/github.com\/rakeshcusat\/Code4Reference\/tree\/master\/AndroidProjects\/SimpleSMSApp\">here<\/a>.Please provide your valuable comments to improve this blog.<\/p>\n<p><em>\u201cBy three methods we may learn wisdom: First, by reflection, which is noblest; Second, by imitation, which is easiest; and third by experience, which is the bitterest.\u201d<\/em><br \/>\nBy : Confucius<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I have been playing around with Android application in past. I thought of writing a simple application which can send a SMS. Android has provided android.telephony.SmsManager class which has exposed different methods for sending SMS. If the message size is less than 160 character then we can use the the following method. sendTextMessage(String destinationAddress, String [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-21","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/code4reference.com\/index.php?rest_route=\/wp\/v2\/posts\/21","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/code4reference.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/code4reference.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/code4reference.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/code4reference.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=21"}],"version-history":[{"count":0,"href":"https:\/\/code4reference.com\/index.php?rest_route=\/wp\/v2\/posts\/21\/revisions"}],"wp:attachment":[{"href":"https:\/\/code4reference.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=21"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/code4reference.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=21"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/code4reference.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=21"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}