One way to implement SMS in Android

Source: Internet
Author: User

Sendsmsactivity.java:

Package Com.test.smsmangerdemo.sendsmsactivity;import Android.support.v7.app.appcompatactivity;import Android.os.bundle;import Android.content.intent;import Android.app.pendingintent;import Android.telephony.smsmanager;import Android.view.view;import Android.widget.button;import Android.widget.EditText Import android.widget.toast;/** * Send SMS Instance */public class Sendsmsactivity extends Appcompatactivity {EditText phone, con    Tent    Button send;        @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);        Setcontentview (r.layout.activity_send_sms);        Get Smsmanger Manager final Smsmanager Smsmanager = Smsmanager.getdefault ();        Initialize control phone = (EditText) Findviewbyid (R.id.et_phone);        Content = (EditText) Findviewbyid (r.id.et_content);        Send = (Button) Findviewbyid (r.id.btn_send); Send.setonclicklistener (New View.onclicklistener () {@Override public void OnClick (View v) {               Create a Android.app.PendingIntent object pendingintent pi = pendingintent.getactivity (sendsmsacti                Vity.this, 0, New Intent (), 0);                        Send SMS Smsmanager.sendtextmessage (Phone.gettext (). ToString (), NULL, Content.gettext (). ToString (),                PI, NULL);            Prompt SMS Send complete toast.maketext (sendsmsactivity.this, "SMS Send Complete", Toast.length_short). Show ();    }        }); }}

Androidmainfest.xml:

<?xml version= "1.0" encoding= "Utf-8"? ><manifest xmlns:android= "http://schemas.android.com/apk/res/ Android "    package=" com.test.smsmangerdemo.sendsmsactivity ">    <uses-permission android:name=" Android.permission.SEND_SMS "/>    <application        android:allowbackup=" true "        android:icon=" @mipmap /ic_launcher "        android:label=" @string/app_name "        android:supportsrtl=" true "        android:theme=" @style/ Apptheme ">        <activity android:name=". Sendsmsactivity ">            <intent-filter>                <action android:name=" Android.intent.action.MAIN "/>                <category android:name= "Android.intent.category.LAUNCHER"/>            </intent-filter>        </ Activity>    </application></manifest>

Activity_send_sms.xml:

<?xml version= "1.0" encoding= "Utf-8"?> <linearlayoutxmlns: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:orientation= "vertical" tools:context= "    Com.test.smsmangerdemo.sendsmsactivity.SendSmsActivity "><linearlayout android:layout_width=" Match_parent " android:layout_height= "wrap_content" android:orientation= "horizontal" > <textview android:layout_width= " Wrap_content "android:layout_height=" Wrap_content "android:text=" Recipient "/> <edittext android:id = "@+id/et_phone" android:layout_width= "match_parent" android:layout_height= "Wrap_content" Android:lay out_marginleft= "10DP"/></linearlayout><linearlayout android:layout_width= "Match_parent" Android:  layout_height= "Wrap_content" android:layout_margintop= "10DP" android:orientation= "Horizontal" > <TextView      Android:layout_width= "Wrap_content" android:layout_height= "wrap_content" android:text= "Send Content"/> & Lt EditText android:id= "@+id/et_content" android:layout_width= "match_parent" android:layout_height= "wrap _content "android:layout_marginleft=" 10DP "android:gravity=" Top "android:lines=" 5 "Android:tex t= "Hello"/></linearlayout><button android:layout_gravity= "center_horizontal" android:layout_width= "wrap _content "android:layout_height=" wrap_content "android:text=" send "android:id=" @+id/btn_send "/></LinearLa" Yout>

Implementation results:

One way to implement SMS in Android

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.