Android Hands-on tutorial The fourth article simple realization SMS transmitter _android

Source: Internet
Author: User
Tags gettext xmlns

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.