Android small function implementation-send SMS, android send SMS
Create an Android project.
1 Layout
Open main. xml and modify the content as follows:
<TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/number" /> <EditText android:layout_width="fill_parent" android:layout_height="wrap_content" android:inputType="text" android:id="@+id/number"/> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/content" /> <EditText android:layout_width="fill_parent" android:layout_height="wrap_content" android:inputType="text" android:minLines="3" android:id="@+id/content"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/button" android:id="@+id/button" />
Binary definition string
Open strings. xml and add the following content:
<String name = "number"> enter the mobile phone number </string> <string name = "content"> enter the text message content </string> <string name = "button"> send SMS </string> <string name = "success"> sent successfully </string>
3. Response to click events
Open MainActivity. java and add the following code:
Public EditText numberText; public EditText contentText; public void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. main); numberText = (EditText) this. findViewById (R. id. number); contentText = (EditText) this. findViewById (R. id. contentText); Button button = (Button) this. findViewById (R. id. button); button. setOnClickListener (new ButtonClickListener ();} private final class ButtonClickListener implements View. onClickListener {public void onClick (View v) {String number = numberText. getText (). toString (); String content = contentText. getText (). toString (); SmsManager manger = SmsManager. getDefault (); // The number of characters in the text message. If 36 characters are left blank, ArrayList <String> texts = manger. divideMessage (content); for (String text: texts) {// parameter: Number, center address, content, sending status, whether the recipient receives status manger. sendTextMessage (number, null, text, null, null);} // notify the user Toast after the SMS is sent. makeText (getApplicationContext (), R. string. success, Toast. LENGTH_LONG ). show ();}}
4. Add Permissions
Add permissions to Manifest. xml:
<uses-permission android:name="android.permission.SEND_SMS"/>