Android Study Notes (10): Call and send text messages

Source: Internet
Author: User

Android integrates the call and text messaging functions, which can be called only,

Main. xml


[Html] <? Xml version = "1.0" encoding = "UTF-8"?>
<RelativeLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent">
<TextView
Android: id = "@ + id/textView"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: layout_alignParentLeft = "true"
Android: textSize = "14px"
Android: text = "phone number:"/>
<EditText
Android: id = "@ + id/etNum"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: layout_below = "@ + id/textView"
Android: textSize = "14px"/>
 
<EditText
Android: id = "@ + id/etMsg"
Android: layout_width = "fill_parent"
Android: layout_height = "100px"
Android: layout_below = "@ + id/etNum"
Android: textSize = "14px"/>
<Button
Android: id = "@ + id/btn_send"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: layout_alignParentRight = "true"
Android: layout_below = "@ + id/etMsg"
Android: textSize = "14px"
Android: text = "send SMS">
</Button>
 
<Button
Android: id = "@ + id/btn_call"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: layout_alignBaseline = "@ + id/btn_send"
Android: layout_alignBottom = "@ + id/btn_send"
Android: layout_toLeftOf = "@ + id/btn_send"
Android: text = "dialing"
Android: textSize = "14px"/>
 
</RelativeLayout>
<? Xml version = "1.0" encoding = "UTF-8"?>
<RelativeLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent">
<TextView
Android: id = "@ + id/textView"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: layout_alignParentLeft = "true"
Android: textSize = "14px"
Android: text = "phone number:"/>
<EditText
Android: id = "@ + id/etNum"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: layout_below = "@ + id/textView"
Android: textSize = "14px"/>

<EditText
Android: id = "@ + id/etMsg"
Android: layout_width = "fill_parent"
Android: layout_height = "100px"
Android: layout_below = "@ + id/etNum"
Android: textSize = "14px"/>
<Button
Android: id = "@ + id/btn_send"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: layout_alignParentRight = "true"
Android: layout_below = "@ + id/etMsg"
Android: textSize = "14px"
Android: text = "send SMS">
</Button>

<Button
Android: id = "@ + id/btn_call"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: layout_alignBaseline = "@ + id/btn_send"
Android: layout_alignBottom = "@ + id/btn_send"
Android: layout_toLeftOf = "@ + id/btn_send"
Android: text = "dialing"
Android: textSize = "14px"/>

</RelativeLayout> TestActivity. java


[Java] public class TestActivity extends Activity implements OnClickListener {
Private EditText etNum;
Private EditText etMes;
Private Button btnCall;
Private Button btnSend;
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
EtNum = (EditText) findViewById (R. id. etNum );
EtMes = (EditText) findViewById (R. id. etMsg );
BtnCall = (Button) findViewById (R. id. btn_call );
BtnSend = (Button) findViewById (R. id. btn_send );
// Register the listener
BtnCall. setOnClickListener (this );
BtnSend. setOnClickListener (this );
}
 
Public void onClick (View v ){
String pNum = etNum. getText (). toString ();
If (pNum. equals ("")){
Toast. makeText (this, "dear, enter a valid phone number! ", Toast. LENGTH_SHORT). show ();
} Else {
// Call
If (v. equals (btnCall )){
// Generate an intent for dialing
Intent intent = new Intent (Intent. ACTION_CALL, Uri. parse ("tel:" + pNum ));
// Dial
StartActivity (intent );
}
// Send
If (v. equals (btnSend )){
String msg = etMes. getText (). toString ();
If (msg. length ()> 70 ){
Toast. makeText (this, "dear, the text message is too long! ", Toast. LENGTH_SHORT). show ();
} Else if (etMes = null & msg. equals ("")){
Toast. makeText (this, "dear, enter the text message content! ", Toast. LENGTH_SHORT). show ();
} Else {
EtMes. setText ("sending ,,,");
// Get the SmsManager object for text message sending
SmsManager manage = SmsManager. getDefault ();
// Send a short message
Manage. sendTextMessage (pNum, null, msg, null, null );
EtMes. setText ("sms sent! ");
}
}
}
}
}
Public class TestActivity extends Activity implements OnClickListener {
Private EditText etNum;
Private EditText etMes;
Private Button btnCall;
Private Button btnSend;
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
EtNum = (EditText) findViewById (R. id. etNum );
EtMes = (EditText) findViewById (R. id. etMsg );
BtnCall = (Button) findViewById (R. id. btn_call );
BtnSend = (Button) findViewById (R. id. btn_send );
// Register the listener
BtnCall. setOnClickListener (this );
BtnSend. setOnClickListener (this );
}

Public void onClick (View v ){
String pNum = etNum. getText (). toString ();
If (pNum. equals ("")){
Toast. makeText (this, "dear, enter a valid phone number! ", Toast. LENGTH_SHORT). show ();
} Else {
// Call
If (v. equals (btnCall )){
// Generate an intent for dialing
Intent intent = new Intent (Intent. ACTION_CALL, Uri. parse ("tel:" + pNum ));
// Dial
StartActivity (intent );
}
// Send
If (v. equals (btnSend )){
String msg = etMes. getText (). toString ();
If (msg. length ()> 70 ){
Toast. makeText (this, "dear, the text message is too long! ", Toast. LENGTH_SHORT). show ();
} Else if (etMes = null & msg. equals ("")){
Toast. makeText (this, "dear, enter the text message content! ", Toast. LENGTH_SHORT). show ();
} Else {
EtMes. setText ("sending ,,,");
// Get the SmsManager object for text message sending
SmsManager manage = SmsManager. getDefault ();
// Send a short message
Manage. sendTextMessage (pNum, null, msg, null, null );
EtMes. setText ("sms sent! ");
}
}
}
}
} The code is only so simple, but you cannot call or send text messages. You need to obtain the permission to call or send text messages,

Add permissions to AndroidManifest. xml:


[Html] <uses-permission android: name = "android. permission. CALL_PHONE"/>
<Uses-permission android: name = "android. permission. SEND_SMS"/>
<Uses-permission android: name = "android. permission. CALL_PHONE"/>
<Uses-permission android: name = "android. permission. SEND_SMS"/>
You can call and send text messages to test two simulators. The number is the ID of the simulator, for example, 5554,

Test diagram:




 



 

I don't know why. Garbled text messages, sweated ,,,

PS: it seems that there is only a problem with the simulator. The real machine is okay.

 

From Hu HU's column

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.