Android Learning Lesson II-SMS Transmitter

Source: Internet
Author: User

 PackageCom.example.message;Importandroid.app.Activity;ImportAndroid.os.Bundle;ImportAndroid.telephony.SmsManager;Importandroid.text.TextUtils;ImportAndroid.view.View;ImportAndroid.view.View.OnClickListener;ImportAndroid.widget.Button;ImportAndroid.widget.EditText;ImportAndroid.widget.Toast; Public classMainactivityextendsActivityImplementsOnclicklistener {PrivateEditText Et_number; PrivateEditText Et_message; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);            Setcontentview (R.layout.activity_main); Button Send=(Button) Findviewbyid (r.id.send); Et_number= (EditText) mainactivity. This. Findviewbyid (R.id.et_number); Et_message= (EditText) mainactivity. This. Findviewbyid (R.id.et_message); Send.setonclicklistener ( This); } @Override Public voidOnClick (View v) {Switch(V.getid ()) { CaseR.id.send:sendmessage ();  Break; }        }        Private voidSendMessage () {String number=Et_number.gettext (). toString (). Trim (); String message=Et_message.gettext (). toString (). Trim (); if(textutils.isempty (number) | |textutils.isempty (message)) {Toast.maketext ( This, "phone number or SMS content cannot be empty", Toast.length_short). Show (); return; }Else{Smsmanager Smsmanager=Smsmanager.getdefault (); Smsmanager.sendtextmessage (number,NULL, Message,NULL,NULL); }                }}

Learning the first lesson, the second lesson is easy to imitate the first lesson, the only difficulty is

  Smsmanager Smsmanager =smsmanager.getdefault ();                  null);
These two lines of code to achieve is the text message sent.
It is also important to note that the first lesson needs to add permissions, that is, the right to call is found in the Androidmanifest.xml permissions, find Call_phone
With the haircut SMS permission is send_sms

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.