Mobile SMS Using ActionScript 3.0

In this tutorial we will create a Mobile SMS Application in ActionScript 3.0. This application is very useful for the android developers, the application create a message from the field and generate a group of contacts using drop-down from the database. This Mobile SMS application relies to the users mobile data to access or to send a message in every contacts. And it is compose of smsane file extension to send a message from the contacts.

Sample Code

The UI Form Function.
  1. package views
  2. {
  3. import commons.Manager;
  4. import commons.ScreenManager;
  5.  
  6. import events.ClassEvent;
  7.  
  8. import flash.events.MouseEvent;
  9. import flash.text.TextFormatAlign;
  10. import flash.text.engine.FontWeight;
  11.  
  12. import flashx.textLayout.formats.VerticalAlign;
  13.  
  14. import models.vo.CClass;
  15.  
  16. import mx.collections.ArrayList;
  17.  
  18. import org.osmf.layout.HorizontalAlign;
  19.  
  20. import spark.components.Button;
  21. import spark.components.DropDownList;
  22. import spark.components.HGroup;
  23. import spark.components.Label;
  24. import spark.components.TextArea;
  25. import spark.components.VGroup;
  26. import spark.components.View;
  27. import spark.events.DropDownEvent;
  28. import spark.events.TextOperationEvent;
  29.  
  30. public class SMSView extends View
  31. {
  32. protected var mainContent:VGroup;
  33. protected const SCALE:Number = ScreenManager.scale;
  34. private const LABEL_FONT_SIZE:Number = 32 * SCALE;
  35.  
  36. private var titleLabel:Label;
  37. private var mainMenuButton:Button;
  38. private var actionButton:Button;
  39. private var textarea:TextArea;
  40. private var btnSend:Button;
  41. private var classesDropwDown:DropDownList;
  42.  
  43. private var _classes:Array;
  44.  
  45. public function SMSView()
  46. {
  47. super();
  48.  
  49. titleLabel = new Label();
  50. titleLabel.percentWidth = 100;
  51. titleLabel.setStyle("fontSize", 32 * SCALE);
  52. titleLabel.setStyle("color", 0xFFFFFF);
  53. titleLabel.setStyle("textAlign", TextFormatAlign.CENTER);
  54. titleLabel.setStyle("fontFamily", "Arial");
  55. titleLabel.setStyle("fontWeight", FontWeight.BOLD);
  56. titleLabel.text = "SMS";
  57.  
  58. var myHGrp:HGroup = new HGroup();
  59. myHGrp.verticalAlign = VerticalAlign.MIDDLE;
  60. myHGrp.horizontalAlign = HorizontalAlign.RIGHT;
  61. myHGrp.percentWidth = 100;
  62. myHGrp.height = headerHeight;
  63. myHGrp.maxHeight = headerHeight;
  64. myHGrp.gap = 20 * SCALE;
  65. myHGrp.addElement(titleLabel);
  66.  
  67. titleContent = new Array();
  68. titleContent.push(myHGrp);
  69.  
  70. actionButton = new Button();
  71. actionButton.width = headerHeight;
  72. actionButton.height = headerHeight;
  73. actionButton.visible = false;
  74.  
  75. actionContent = new Array();
  76. actionContent.push(actionButton);
  77.  
  78. mainContent = new VGroup();
  79. mainContent.paddingTop = 20 * SCALE;
  80. mainContent.paddingRight = 20 * SCALE;
  81. mainContent.paddingLeft = 20 * SCALE;
  82. mainContent.paddingBottom = 20 * SCALE;
  83. mainContent.percentWidth = 100;
  84. mainContent.percentHeight = 100;
  85. mainContent.horizontalAlign = HorizontalAlign.CENTER;
  86. mainContent.gap = 10 * SCALE;
  87. this.addElement(mainContent);
  88.  
  89. textarea = new TextArea();
  90. textarea.percentWidth = 100;
  91. textarea.height = 550 * SCALE;
  92. textarea.setStyle("fontSize", 35 * SCALE);
  93. textarea.addEventListener(TextOperationEvent.CHANGE, onChangeText);
  94. mainContent.addElement(textarea);
  95.  
  96. classesDropwDown = new DropDownList();
  97. classesDropwDown.percentWidth = 100;
  98. classesDropwDown.height = 70 * SCALE;
  99. classesDropwDown.selectedIndex = 0;
  100. classesDropwDown.setStyle("borderColor",0xdddddd);
  101. classesDropwDown.setStyle("dropShadowVisible",false);
  102. classesDropwDown.setStyle("editable",false);
  103. classesDropwDown.setStyle("fontSize", LABEL_FONT_SIZE);
  104. classesDropwDown.buttonMode = true;
  105. classesDropwDown.addEventListener(DropDownEvent.CLOSE, onSelectClass);
  106. classesDropwDown.addEventListener(MouseEvent.CLICK, onClickDropwDown);
  107. mainContent.addElement(classesDropwDown);
  108.  
  109. btnSend = new Button();
  110. btnSend.label = "Send";
  111. btnSend.enabled = false;
  112. btnSend.percentWidth = 100;
  113. btnSend.height = 70 * SCALE;
  114. btnSend.setStyle("fontSize", 35 * SCALE);
  115. btnSend.addEventListener(MouseEvent.CLICK, onSendSMS);
  116. mainContent.addElement(btnSend);
  117. }
  118. }
  119. }
imageFor The Group of contacts we use a Dropdown Function To get the data.
  1. private function onClickDropwDown(event:MouseEvent):void
  2. {
  3. DropDownList(event.currentTarget).openDropDown();
  4. }
  5.  
  6. private function onSelectClass(event:DropDownEvent):void
  7. {
  8. if(DropDownList(event.target).selectedItem)
  9. {
  10. if(!Manager.isTextEmpty(textarea.text) && int(DropDownList(event.target).selectedItem.data) > 0)
  11. {
  12. btnSend.enabled = true;
  13. }
  14. else
  15. {
  16. btnSend.enabled = false;
  17. }
  18. }
  19. }
  20.  
  21. public function set classes(value:Array):void
  22. {
  23. _classes = value;
  24.  
  25. if(_classes != null && _classes.length)
  26. {
  27. var classesLists:ArrayList = new ArrayList();
  28.  
  29. for (var i:int = 0; i < _classes.length; i++)
  30. {
  31. var cclass:CClass = CClass(_classes[i]);
  32. classesLists.addItem({label: cclass.className, data: cclass.id});
  33. }
  34.  
  35. classesDropwDown.dataProvider = classesLists;
  36.  
  37. textarea.enabled = true;
  38. classesDropwDown.enabled = true;
  39. }
  40. else
  41. {
  42. textarea.enabled = false;
  43. classesDropwDown.enabled = false;
  44. }
  45. }
The Text Field Function.
  1. public function get smsText():String
  2. {
  3. return textarea.text;
  4. }
  5.  
  6. public function clearSmsText():void
  7. {
  8. textarea.text = "";
  9. }
The Button Function to send and generate a message.
  1. private function onSendSMS(event:MouseEvent):void
  2. {
  3. var selectedClass:CClass = new CClass();
  4.  
  5. for (var i:int = 0; i < _classes.length; i++)
  6.  
  7. var cclass:CClass = CClass(_classes[i]);
  8.  
  9. if(cclass.id == int(classesDropwDown.selectedItem.data))
  10. {
  11. selectedClass = cclass;
  12. break;
  13. }
  14. }
  15.  
  16. dispatchEvent(new ClassEvent(ClassEvent.SEND_SMS,selectedClass));
  17. }
And For the Mediator of the SMSANE extension function to enable and access the data of the mobile phone.
  1. package mediators
  2. {
  3. import com.sms.ane.SmsAne;
  4.  
  5. import commons.Manager;
  6.  
  7. import events.ClassEvent;
  8.  
  9. import models.ClassModel;
  10. import models.vo.Student;
  11.  
  12. import org.robotlegs.mvcs.Mediator;
  13.  
  14. import views.SMSView;
  15.  
  16. public class SMSViewMediator extends Mediator
  17. {
  18. [Inject] public var view:SMSView;
  19. [Inject] public var classModel:ClassModel;
  20.  
  21. public function SMSViewMediator()
  22. {
  23. }
  24.  
  25. override public function onRegister():void
  26. {
  27. eventMap.mapListener(view, ClassEvent.SEND_SMS, onSendSMS);
  28. init();
  29. }
  30.  
  31. private function init():void
  32. {
  33. classModel.getAllClasses();
  34. view.classes = classModel.classes;
  35. }
  36.  
  37. private function onSendSMS(event:ClassEvent):void
  38. {
  39. classModel.selectedClass = event.cclass;
  40. classModel.getAllClassStudents();
  41.  
  42. for each(var student:Student in classModel.classStudents)
  43. {
  44. if(!Manager.isTextEmpty(student.cellnumber))
  45. {
  46. try
  47. {
  48. if(student.cellnumber.length == 10 || student.cellnumber.length == 11)
  49. {
  50. var cellNumber:String = (student.cellnumber.length == 10) ? String("0" + student.cellnumber.length) : student.cellnumber;
  51. SmsAne.getInstance().sendSMS(cellNumber, view.smsText);
  52. }
  53. }
  54. catch(e:Error)
  55. {
  56. trace("");
  57. }
  58. }
  59. }
  60. }
  61. }
  62. }
Hope that you learn in this tutorial. And for more updates and programming tutorials don't hesitate to ask and we will answer your questions and suggestions. Don't forget to LIKE & SHARE this website.

Add new comment