This essay will show you how to complete a simple third-party SMS transmitter (do not open the SMS interface, call the Android API to complete the function)
1. First, let's do the layout
As I write here is a simple,, SMS sent, so just a linearlayout decentralized two edittext used to save numbers and content, there is a button to send the buttons, if you want a more gorgeous layout, interested people can go to add AO.
1<?xml version="1.0"encoding="Utf-8"?>2<linearlayout xmlns:android="http://schemas.android.com/apk/res/android"3xmlns:app="Http://schemas.android.com/apk/res-auto"4xmlns:tools="Http://schemas.android.com/tools"5Android:layout_width="match_parent"6android:layout_height="match_parent"7android:orientation="Vertical"8tools:context="com.example.sendmessage.MainActivity">9 Ten<!-- One 1. Use hint to display cue placeholder information, which disappears automatically when entered A 2. InputType is the type of input data, which requires a number, so allow the number to be entered -- -<EditText theAndroid:id="@+id/et_phone" -Android:layout_width="match_parent" -android:layout_height="wrap_content" -Android:hint="Please enter a number" +Android:inputtype=" Number"/> - +<!-- A 1It is recommended to use lines to control how much input content, if the height is written dead, different sizes of the phone font at The total number of characters that can be entered will change, and using lines no matter how big the word is, you can enter a fixed line. -- -<EditText -Android:id="@+id/et_content" -Android:layout_width="match_parent" -android:layout_height="wrap_content" inAndroid:hint="please input text message content" -android:lines="8" toandroid:gravity="Top"/> + -<Button theAndroid:layout_width="match_parent" *android:layout_height="wrap_content" $android:onclick="Send"Panax Notoginsengandroid:text="Send"/> - the</LinearLayout>
After the salt, is the business process, the focus, you can take the bench ah, we can directly call the Android send SMS API, only two lines of code, get the object to send text messages can
1 Package Com.example.sendmessage;2 3 import Android.os.Bundle;4 import android.support.v7.app.AppCompatActivity;5 import Android.telephony.SmsManager;6 import Android.view.View;7 import Android.widget.EditText;8 9 Public classMainactivity extends Appcompatactivity {Ten One @Override A protected voidonCreate (Bundle savedinstancestate) { - super.oncreate (savedinstancestate); - Setcontentview (r.layout.activity_main); the } - - Public voidSend (View v) { - //get user-entered numbers and SMS content +EditText Et_phone =(EditText) Findviewbyid (r.id.et_phone); -EditText et_content =(EditText) Findviewbyid (r.id.et_content); +String phone =Et_phone.gettext (). toString (); AString content =Et_content.gettext (). toString (); at - //Direct use of the API to send SMS -Smsmanager SM =Smsmanager.getdefault (); - - /** - * arg0: Target number in * ARG1: SMS center number, NULL, write dead to change the place no way to call - * arg2: Text message text to */ +Sm.sendtextmessage (Phone,NULL, Content,NULL,NULL); - } the * $}
(Sendtextmessage the second parameter, if you are interested, you can open your phone's dial-up interface, press the ' *#* #6436 #*#* ', select ' Mobile Information ', pull down can see smsc this place is empty, We passed a null in all programs)
Then we were excited to point out the running program, entered the phone and password, and then,,, a magical scene took place
Stop running, for what????? Don't blame me for digging a hole, let's read the wrong message first
This is very clear, do not send SMS permission, then we go to Androidmanifest.xml to add this permission
Add before <application>
<uses-permission android:name="android.permission.SEND_SMS"></ Uses-permission>
This sentence and then re-run
It's finally done, is it over??? Also nearly, there is another thing is that the operator is required to send a text message at most only 70 words, that question, life we send hundreds of words of sweet talk is how to send it? That's because the backstage program has split the text into multiple pieces.
In the same way, there will be a number of charges, then the operator must not be split, they are only responsible for collecting money, the split will be given to us,
1 //Direct use of the API to send SMS2Smsmanager SM =Smsmanager.getdefault ();3 4 //split the length of the letter5Arraylist<string> SMS =sm.dividemessage (content);6 for(Stringstring: SMS) {7 8 /**9 * arg0: Target numberTen * ARG1: SMS center number, NULL, write dead to change the place no way to call One * arg2: Text message text A */ -Sm.sendtextmessage (Phone,NULL,string,NULL,NULL); -}
And then it's over. Above
Android_ Simple SMS Transmitter