Android 5. callphone and sendsms

Source: Internet
Author: User

Simple implementation of the call and text message functions of the two Simulators

Main Interface:



 


 



 

Layout file:


01 simple layout file:

02

03 <? Xml version = "1.0" encoding = "UTF-8"?>

04 <LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"

05 android: layout_width = "fill_parent"

06 android: layout_height = "fill_parent"

07 android: orientation = "vertical">

08

09 <TextView

10 android: layout_width = "fill_parent"

11 android: layout_height = "wrap_content"

12 android: inputType = "phone"

13 android: numeric = "decimal"

14 android: text = "dialing"/>

15

16 <EditText

17 android: id = "@ + id/phoneText"

18 android: layout_width = "250dip"

19 android: layout_height = "wrap_content"

20 android: hint = "Enter the phone number ..."

21 android: phoneNumber = "true"/>

22

23 <Button

24 android: id = "@ + id/callButton"

25 android: layout_width = "wrap_content"

26 android: layout_height = "wrap_content"

27 android: text = "dial"/>

28

29 <TextView

30 android: layout_width = "fill_parent"

31 android: layout_height = "wrap_content"

32 android: text = "send SMS"/>

33

34 <EditText

35 android: id = "@ + id/smsText"

36 android: layout_width = "250dip"

37 android: layout_height = "120dip"

38 android: hint = "Enter the text message..."/>

39

40 <Button

41 android: id = "@ + id/smsButton"

42 android: layout_width = "wrap_content"

43 android: layout_height = "wrap_content"

44 android: text = "send SMS"/>

45

46 </LinearLayout>

 

Main program code:


01 package smh. demo;

02

03 import android. app. Activity;

04 import android. app. PendingIntent;

05 import android. content. Intent;

06 import android.net. Uri;

07 import android. OS. Bundle;

08 import android. telephony. SmsManager;

09 import android. view. View;

10 import android. view. View. OnClickListener;

11 import android. widget. Button;

12 import android. widget. EditText;

13 import android. widget. Toast;

14

15 public class IntentDemoActivity extends Activity {

16/** Called when the activity is first created .*/

17 private EditText phoneNumberText;

18

19 @ Override

20 public void onCreate (Bundle savedInstanceState ){

21 super. onCreate (savedInstanceState );

22 setContentView (R. layout. main );

23

24 Button callButton = (Button) findViewById (R. id. callButton );

25 Button smsButton = (Button) findViewById (R. id. smsButton );

26

27 phoneNumberText = (EditText) findViewById (R. id. phoneText );

28

29 // call

30 callButton. setOnClickListener (new OnClickListener (){

31

32 @ Override

33 public void onClick (View arg0 ){

34 String phoneNumber = phoneNumberText. getText (). toString ();

35

36 if (! "". Equals (phoneNumber )){

37 Intent intent = new Intent (Intent. ACTION_CALL, Uri

38. parse ("tel:" + phoneNumber ));

39 startActivity (intent );

40} else {

41 Toast. makeText (IntentDemoActivity. this, "Sorry, the phone number cannot be blank! ",

42 Toast. LENGTH_LONG). show ();

43}

44}

45 });

46

47 // send SMS

48 smsButton. setOnClickListener (new OnClickListener (){

49

50 @ Override

51 public void onClick (View arg0 ){

52 EditText smsNumberText = (EditText) findViewById (R. id. smsText );

53 String smsMessage = smsNumberText. getText (). toString ();

54 String phoneNumber = phoneNumberText. getText (). toString ();

55

56 if (! "". Equals (phoneNumber )){

57 SmsManager sms = SmsManager. getDefault ();

58 PendingIntent mPI = PendingIntent. getBroadcast (

59 IntentDemoActivity. this, 0, new Intent (), 0 );

60 sms. sendTextMessage (phoneNumber, null, smsMessage, mPI,

61 null );

62

63 Toast. makeText (IntentDemoActivity. this, "sent successfully ",

64 Toast. LENGTH_LONG). show ();

65} else {

66 Toast. makeText (IntentDemoActivity. this, "Sorry, the phone number cannot be blank! ",

67 Toast. LENGTH_LONG). show ();

68}

69}

70 });

71

72}

73}

 

 

AndroidManifest. xml


01 <? Xml version = "1.0" encoding = "UTF-8"?>

02 <manifest xmlns: android = "http://schemas.android.com/apk/res/android"

03 package = "smh. demo"

04 android: versionCode = "1"

05 android: versionName = "1.0" type = "codeph" text = "/codeph">

06

07 <uses-sdk android: minSdkVersion = "8"/>

08

09 <application

10 android: icon = "@ drawable/ic_launcher"

11 android: label = "@ string/app_name">

12 <activity

13 android: name = ". IntentDemoActivity"

14 android: label = "@ string/app_name">

15 <intent-filter>

16 <action android: name = "android. intent. action. MAIN"/>

17

18 <category android: name = "android. intent. category. LAUNCHER"/>

19 </intent-filter>

20 </activity>

21 </application>

22

23 <uses-permission android: name = "android. permission. CALL_PHONE"/>

24 <uses-permission android: name = "android. permission. SEND_SMS"/>

25

26 </manifest>
 


From myter7
 

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.