Android Application Development Notes (1): Call and send SMS, receive SMS interface, send email (call, dial, smsmanager, broadcast, email)

Source: Internet
Author: User

This article is from http://blog.csdn.net/xjanker2,!

 

Calling and sending text messages can be said to be the core application. This article will describe its calling method. It can be divided into direct call-direct call or text message sending, or indirect call-to enter the dialing or text message Writing Page, waiting for the user to confirm the content before the user sends out.

First look at the code effect:

First, write activaty on the main interface and create the callandsms class as the default startup page.Package jtapp. callandsms; </p> <p> import java. util. list; </p> <p> import android. app. activity; <br/> import android. content. intent; <br/> import android.net. uri; <br/> import android. OS. bundle; <br/> import android. telephony. smsManager; <br/> import android. view. view; <br/> import android. view. view. onClickListener; <br/> import android. widget. button; <br/> import android. widget. toast; </p> <p> public class CallAndSms extends Activity {<br/>/** Called when the activity is first created. */<br/> @ Override <br/> public void onCreate (Bundle savedInstanceState) {<br/> super. onCreate (savedInstanceState); <br/> setContentView (R. layout. main); </p> <p> setComponent (); <br/>}</p> <p> private void setComponent () {<br/> Button bt1 = (Button) findViewById (R. id. button01); <br/> bt1.setOnClickListener (new OnClickListener () {<br/> @ Override <br/> public void onClick (View v) {<br/> Intent intent = new Intent (<br/> Intent. ACTION_CALL, Uri. parse ("tel: 10010"); <br/> startActivity (intent); <br/>}< br/> }); <br/> Button bt2 = (Button) findViewById (R. id. button02); <br/> bt2.setOnClickListener (new OnClickListener () {<br/> @ Override <br/> public void onClick (View v) {<br/> String smsContent = "102"; <br/> // note: SMS must be divided before being sent <br/> SmsManager sms = SmsManager. getDefault (); <br/> List <String> texts = sms. divideMessage (smsContent); <br/> for (String text: texts) {<br/> sms. sendTextMessage ("10010", null, text, null, null); <br/>}< br/> // note: not checked success or failure yet <br/> Toast. makeText (<br/> CallAndSms. this, <br/> "sms sent", <br/> Toast. LENGTH_SHORT ). show (); <br/>}< br/>}); </p> <p> Button bt3 = (Button) findViewById (R. id. button03); <br/> bt3.setOnClickListener (new OnClickListener () {<br/> @ Override <br/> public void onClick (View v) {<br/> Intent intent = new Intent (<br/> Intent. ACTION_DIAL, Uri. parse ("tel: 10010"); <br/> startActivity (intent); <br/>}< br/> }); <br/> Button bt4 = (Button) findViewById (R. id. button04); <br/> bt4.setOnClickListener (new OnClickListener () {<br/> @ Override <br/> public void onClick (View v) {<br/> Uri uri = Uri. parse ("smsto: 10010"); <br/> Intent it = new Intent (Intent. ACTION_SENDTO, uri); <br/> it. putExtra ("sms_body", "102"); <br/> startActivity (it); <br/>}< br/> }); <br/>}< br/>}

The main UI defines the main. XML code:

<? Xml version = "1.0" encoding = "UTF-8"?> <Br/> <LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android" <br/> android: orientation = "vertical" android: layout_width = "fill_parent" <br/> android: layout_height = "fill_parent" android: gravity = "center"> <br/> <TextView android: layout_width = "fill_parent" <br/> android: layout_height = "wrap_content" android: text = "Direct Method:" <br/> android: gravity = "center"/> <br/> <Button android: text = "Call 10010 Customer Service" android: id = "@ + id/Button01" <br/> android: layout_width = "wrap_content" android: layout_height = "wrap_content"> </Button> <br/> <Button android: text = "text message 10010 balance check" android: id = "@ + id/Button02" <br/> android: layout_width = "wrap_content" android: layout_height = "wrap_content"> </Button> <br/> <TextView android: text = "InDirect Method:" android: id = "@ + id/TextView01" <br/> android: layout_width = "wrap_content" android: layout_height = "wrap_content"/> <br/> <Button android: text = "Call 10010 Customer Service" android: id = "@ + id/Button03" <br/> android: layout_width = "wrap_content" android: layout_height = "wrap_content"> </Button> <br/> <Button android: text = "SMS 10010 balance check" android: id = "@ + id/Button04" <br/> android: layout_width = "wrap_content" android: layout_height = "wrap_content"> </Button> <br/> </LinearLayout>

The file used to listen to the broadcast message sent by the SMS,

Receiversms. Java code:

Package jtapp. callandsms; </P> <p> Import android. content. broadcastreceiver; <br/> Import android. content. context; <br/> Import android. content. intent; <br/> Import android. OS. bundle; <br/> Import android. telephony. smsmessage; <br/> Import android. widget. toast; </P> <p> public class receiversms extends broadcastreceiver {</P> <p> @ override <br/> Public void onreceive (context, intent) {<br/> If (in Tent. getaction (). equals (<br/> "android. provider. telephony. sms_received ") {<br/> stringbuilder sb = new stringbuilder (); <br/> bundle = intent. getextras (); <br/> If (bundle! = NULL) {<br/> object [] PDUS = (object []) bundle. get ("PDUS"); <br/> smsmessage [] msgs = new smsmessage [PDUS. length]; <br/> for (INT I = 0; I <PDUS. length; I ++) {<br/> msgs [I] = smsmessage <br/>. createfrompdu (byte []) PDUS [I]); <br/>}< br/> for (smsmessage S: MSGs) {<br/> Sb. append ("received from"); <br/> Sb. append (S. getdisplayoriginatingaddress (); <br/> Sb. append ("SMS, content:"); <br/> Sb. append (S. getdisplaymessagebody (); <br/>}< br/> toast. maketext (<br/> context, <br/> "received short message:" + sb. tostring (), <br/> toast. length_long ). show (); <br/>}</P> <p >}< br/>

In androidmanifest. XML, the permission, activity, and handler settings are as follows:

<? XML version = "1.0" encoding = "UTF-8"?> <Br/> <manifest xmlns: Android = "http://schemas.android.com/apk/res/android" <br/> package = "jtapp. callandsms "Android: versioncode =" 1 "Android: versionname =" 1.0 "> <br/> <application Android: icon =" @ drawable/icon "Android: label = "@ string/app_name" <br/> Android: debuggable = "true"> <br/> <activity Android: Name = ". callandsms "Android: Label =" @ string/app_name "> <br/> <intent-filter> <br/> <action Android: Name =" android. intent. action. main "/> <br/> <category Android: Name =" android. intent. category. launcher "/> <br/> </intent-filter> <br/> </activity> <br/> <Cycler Android: Name = ". receiversms "Android: enabled =" true "> <br/> <intent-filter> <br/> <action Android: Name =" android. provider. telephony. sms_received "/> <br/> </intent-filter> <br/> </javaser> <br/> </Application> <br/> <uses-Permission Android: name = "android. permission. call_phone "> </uses-Permission> <br/> <uses-Permission Android: Name =" android. permission. send_sms "> </uses-Permission> <br/> <uses-Permission Android: Name =" android. permission. receive_sms "> </uses-Permission> <br/> </manifest>

 

 

Add, send email code snippets:

// Email <br/> Button bt5 = (Button) findViewById (R. id. button05); <br/> bt5.setOnClickListener (new OnClickListener () {<br/> @ Override <br/> public void onClick (View v) {<br/> // Setup the recipient in a String array <br/> String [] mailto = {"noam@gmail.com "}; <br/> // Create a new Intent to send messages <br/> Intent sendIntent = new Intent (Intent. ACTION_SEND); <br/> // Write the body of theEmail <br/> String emailBody = "You're password is :"; <br/> // Add attributes to the intent <br/> // sendIntent. setType ("text/plain"); // use this line for testing <br/> // in the emulator <br/> sendIntent. setType ("message/rfc822"); // use this line for testing <br/> // on the real phone <br/> sendIntent. putExtra (Intent. EXTRA_EMAIL, mailto); <br/> sendIntent. putExtra (Intent. EXTRA_SUBJECT, "Your Password"); <br/> sendIntent. putExtra (Intent. EXTRA_TEXT, emailBody); <br/> startActivity (sendIntent); <br/>}< br/> });

 

Additional reading:

Send and receive text messages in Android

Http://sean.huanglijiang.com/article.asp? Id = 218

Android SMS sending Process

Http://apps.hi.baidu.com/share/detail/15096826

 

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.