分类: 嵌入式
2012-12-18 11:27:01
打电话和发短信可以说是最核心的应用了,本文就来阐述它的调用方法。可以分为直接调用--直接电话或短信发出,已经间接调用--进入拨号或短信撰写页面,等待用户确认内容后由用户发出.
先看代码效果截图:
先编写主界面Activaty,创建类CallAndSms作为为默认启动页
[java] view plaincopy
1. package jtapp.callandsms;
2.
3. import java.util.List;
4.
5. import android.app.Activity;
6. import android.content.Intent;
7. import android.net.Uri;
8. import android.os.Bundle;
9. import android.telephony.SmsManager;
10. import android.view.View;
11. import android.view.View.OnClickListener;
12. import android.widget.Button;
13. import android.widget.Toast;
14.
15. public class CallAndSms extends Activity {
16. /** Called when the activity is first created. */
17. @Override
18. public void onCreate(Bundle savedInstanceState) {
19. super.onCreate(savedInstanceState);
20. setContentView(R.layout.main);
21.
22. setComponent();
23. }
24.
25. private void setComponent() {
26. Button bt1 = (Button) findViewById(R.id.Button01);
27. bt1.setOnClickListener(new OnClickListener() {
28. @Override
29. public void onClick(View v) {
30. Intent intent = new Intent(
31. Intent.ACTION_CALL, Uri.parse("tel:10010"));
32. startActivity(intent);
33. }
34. });
35. Button bt2 = (Button) findViewById(R.id.Button02);
36. bt2.setOnClickListener(new OnClickListener() {
37. @Override
38. public void onClick(View v) {
39. String smsContent = "102";
40. // note: SMS must be divided before being sent
41. SmsManager sms = SmsManager.getDefault();
42. List
43. for (String text : texts) {
44. sms.sendTextMessage("10010", null, text, null, null);
45. }
46. // note: not checked success or failure yet
47. Toast.makeText(
48. CallAndSms.this,
49. "短信已发送",
50. Toast.LENGTH_SHORT ).show();
51. }
52. });
53.
54. Button bt3 = (Button) findViewById(R.id.Button03);
55. bt3.setOnClickListener(new OnClickListener() {
56. @Override
57. public void onClick(View v) {
58. Intent intent = new Intent(
59. Intent.ACTION_DIAL, Uri.parse("tel:10010"));
60. startActivity(intent);
61. }
62. });
63. Button bt4 = (Button) findViewById(R.id.Button04);
64. bt4.setOnClickListener(new OnClickListener() {
65. @Override
66. public void onClick(View v) {
67. Uri uri = Uri.parse("smsto:10010");
68. Intent it = new Intent(Intent.ACTION_SENDTO, uri);
69. it.putExtra("sms_body", "102");
70. startActivity(it);
71. }
72. });
73. }
74. }
主界面ui定义 main.xml 代码:
[xhtml] view plaincopy
1. version="1.0" encoding="utf-8"?>
2.
3. android:orientation="vertical" android:layout_width="fill_parent"
4. android:layout_height="fill_parent" android:gravity="center">
5.
6. android:layout_height="wrap_content" android:text="Direct Method:"
7. android:gravity="center" />
8.
9. android:layout_width="wrap_content" android:layout_height="wrap_content">
10.
11. android:layout_width="wrap_content" android:layout_height="wrap_content">
12.
13. android:layout_width="wrap_content" android:layout_height="wrap_content" />
14.
15. android:layout_width="wrap_content" android:layout_height="wrap_content">
16.
17. android:layout_width="wrap_content" android:layout_height="wrap_content">
18.
用于监听短信到来的Broadcast消息的文件,
ReceiverSMS.java 代码:
[java] view plaincopy
1. package jtapp.callandsms;
2.
3. import android.content.BroadcastReceiver;
4. import android.content.Context;
5. import android.content.Intent;
6. import android.os.Bundle;
7. import android.telephony.SmsMessage;
8. import android.widget.Toast;
9.
10. public class ReceiverSMS extends BroadcastReceiver {
11.
12. @Override
13. public void onReceive(Context context, Intent intent) {
14. if (intent.getAction().equals(
15. "android.provider.Telephony.SMS_RECEIVED")) {
16. StringBuilder sb = new StringBuilder();
17. Bundle bundle = intent.getExtras();
18. if (bundle != null) {
19. Object[] pdus = (Object[]) bundle.get("pdus");
20. SmsMessage[] msgs = new SmsMessage[pdus.length];
21. for (int i = 0; i < pdus.length; i++) {
22. msgs[i] = SmsMessage
23. .createFromPdu((byte[]) pdus[i]);
24. }
25. for (SmsMessage s : msgs) {
26. sb.append("收到来自");
27. sb.append(s.getDisplayOriginatingAddress());
28. sb.append("的SMS, 内容:");
29. sb.append(s.getDisplayMessageBody());
30. }
31. Toast.makeText(
32. context,
33. "收到了短消息: " + sb.toString(),
34. Toast.LENGTH_LONG).show();
35. }
36.
37. }
38. }
39. }
AndroidManifest.xml中权限、activity和receiver的设定:
[java] view plaincopy
1.
2. 3. package="jtapp.callandsms" android:versionCode="1" android:versionName="1.0"> 4. 5. android:debuggable="true"> 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21.
补充,发Email 代码片段:
[java] view plaincopy
1. //Email
2. Button bt5 = (Button) findViewById(R.id.Button05);
3. bt5.setOnClickListener(new OnClickListener() {
4. @Override
5. public void onClick(View v) {
6. // Setup the recipient in a String array
7. String[] mailto = { "noam@gmail.com" };
8. // Create a new Intent to send messages
9. Intent sendIntent = new Intent(Intent.ACTION_SEND);
10. // Write the body of theEmail
11. String emailBody = "You're password is: ";
12. // Add attributes to the intent
13. //sendIntent.setType("text/plain"); // use this line for testing
14. // in the emulator
15. sendIntent.setType("message/rfc822"); // use this line for testing
16. // on the real phone
17. sendIntent.putExtra(Intent.EXTRA_EMAIL, mailto);
18. sendIntent.putExtra(Intent.EXTRA_SUBJECT, "Your Password");
19. sendIntent.putExtra(Intent.EXTRA_TEXT, emailBody);
20. startActivity(sendIntent);
21. }
22. });
本文来自 http://blog.csdn.net/xjanker2