The example of this article for everyone to share the Android SMS function of the implementation method for your reference, the specific contents are as follows
First configure a layout:
<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http:// Schemas.android.com/tools "android:layout_width=" match_parent "android:layout_height=" Match_parent "Android: paddingbottom= "@dimen/activity_vertical_margin" android:paddingleft= "@dimen/activity_horizontal_margin" Android: paddingright= "@dimen/activity_horizontal_margin" android:paddingtop= "@dimen/activity_vertical_margin" tools: Context= ". Mainactivity "android:orientation=" vertical "> <edittext android:id=" @+id/et_phone "Android:layout_width" = "Match_parent" android:layout_height= "wrap_content" android:inputtype= "Phone" android:hint= "Please enter your opponent's number"/> < EditText android:id= "@+id/et_content" android:layout_width= "match_parent" android:layout_height= "Wrap_content" and
roid:lines= "5" android:hint= "Please enter SMS Content" android:gravity= "top"/> <button android:layout_width= "Wrap_content"
android:layout_height= "wrap_content" android:text= "Send" android:onclick= "Send"/> </LinearLayout>
Then write the code to text in the activity:
package com.ydl.smssender;
Import java.util.ArrayList; Omit Guide Pack public class Mainactivity extends activity {@Override protected void onCreate (Bundle savedinstancestate)
{super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_main);
public void Send (View v) {//Get user input number and 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 ();
String content = Et_content.gettext (). toString ();
1. Get SMS manager Smsmanager SM = Smsmanager.getdefault (); 2. Cutting SMS, the long message into a number of small text messages arraylist<string> SMSs = sm.dividemessage (content);//an ArrayList of strings that comprise the original message//3.for loops all text messages in the collection for (String String:smss) {sm.sendtextmessage (phone,
NULL, string, NULL, NULL);//send a text based SMS. }
}
}
Texting is required for system privileges:
Copy Code code as follows:
<uses-permission android:name= "Android.permission.SEND_SMS"/>
Effect:
Opened two simulator, the realization of the text messaging function.
The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.