您的位置:首页 > 移动开发 > Android开发

Android应用开发笔记(1):调用打电话和发短信、收短信接口、发Email (Call, Dial, SMSManager, Broadcast, Email)

2011-06-24 13:42 671 查看
打电话和发短信可以说是最核心的应用了,本文就来阐述它的调用方法。可以分为直接调用--直接电话或短信发出,已经间接调用--进入拨号或短信撰写页面,等待用户确认内容后由用户发出.

先看代码效果截图:

先编写主界面Activaty,创建类CallAndSms作为为默认启动页
view plaincopy to clipboardprint?
01.package jtapp.callandsms;
02.
03.import java.util.List;
04.
05.import android.app.Activity;
06.import android.content.Intent;
07.import android.net.Uri;
08.import android.os.Bundle;
09.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<String> texts = sms.divideMessage(smsContent);
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.}
package jtapp.callandsms;

import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class CallAndSms extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

setComponent();
}

private void setComponent() {
Button bt1 = (Button) findViewById(R.id.Button01);
bt1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(
Intent.ACTION_CALL, Uri.parse("tel:10010"));
startActivity(intent);
}
});
Button bt2 = (Button) findViewById(R.id.Button02);
bt2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
String smsContent = "102";
// note: SMS must be divided before being sent
SmsManager sms = SmsManager.getDefault();
List<String> texts = sms.divideMessage(smsContent);
for (String text : texts) {
sms.sendTextMessage("10010", null, text, null, null);
}
// note: not checked success or failure yet
Toast.makeText(
CallAndSms.this,
"短信已发送",
Toast.LENGTH_SHORT ).show();
}
});

Button bt3 = (Button) findViewById(R.id.Button03);
bt3.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(
Intent.ACTION_DIAL, Uri.parse("tel:10010"));
startActivity(intent);
}
});
Button bt4 = (Button) findViewById(R.id.Button04);
bt4.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Uri uri = Uri.parse("smsto:10010");
Intent it = new Intent(Intent.ACTION_SENDTO, uri);
it.putExtra("sms_body", "102");
startActivity(it);
}
});
}
}

主界面ui定义 main.xml 代码:

view plaincopy to clipboardprint?
01.<?xml version="1.0" encoding="utf-8"?>
02.<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
03. android:orientation="vertical" android:layout_width="fill_parent"
04. android:layout_height="fill_parent" android:gravity="center">
05. <TextView android:layout_width="fill_parent"
06. android:layout_height="wrap_content" android:text="Direct Method:"
07. android:gravity="center" />
08. <Button android:text="拨打10010客服电话" android:id="@+id/Button01"
09. android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
10. <Button android:text="短信10010查余额" android:id="@+id/Button02"
11. android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
12. <TextView android:text="InDirect Method:" android:id="@+id/TextView01"
13. android:layout_width="wrap_content" android:layout_height="wrap_content" />
14. <Button android:text="拨打10010客服电话" android:id="@+id/Button03"
15. android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
16. <Button android:text="短信10010查余额" android:id="@+id/Button04"
17. android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
18.</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:gravity="center">
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content" android:text="Direct Method:"
android:gravity="center" />
<Button android:text="拨打10010客服电话" android:id="@+id/Button01"
android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
<Button android:text="短信10010查余额" android:id="@+id/Button02"
android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
<TextView android:text="InDirect Method:" android:id="@+id/TextView01"
android:layout_width="wrap_content" android:layout_height="wrap_content" />
<Button android:text="拨打10010客服电话" android:id="@+id/Button03"
android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
<Button android:text="短信10010查余额" android:id="@+id/Button04"
android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
</LinearLayout>

用于监听短信到来的Broadcast消息的文件,

ReceiverSMS.java 代码:

view plaincopy to clipboardprint?
01.package jtapp.callandsms;
02.
03.import android.content.BroadcastReceiver;
04.import android.content.Context;
05.import android.content.Intent;
06.import android.os.Bundle;
07.import android.telephony.SmsMessage;
08.import android.widget.Toast;
09.
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.}
package jtapp.callandsms;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsMessage;
import android.widget.Toast;

public class ReceiverSMS extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(
"android.provider.Telephony.SMS_RECEIVED")) {
StringBuilder sb = new StringBuilder();
Bundle bundle = intent.getExtras();
if (bundle != null) {
Object[] pdus = (Object[]) bundle.get("pdus");
SmsMessage[] msgs = new SmsMessage[pdus.length];
for (int i = 0; i < pdus.length; i++) {
msgs[i] = SmsMessage
.createFromPdu((byte[]) pdus[i]);
}
for (SmsMessage s : msgs) {
sb.append("收到来自");
sb.append(s.getDisplayOriginatingAddress());
sb.append("的SMS, 内容:");
sb.append(s.getDisplayMessageBody());
}
Toast.makeText(
context,
"收到了短消息: " + sb.toString(),
Toast.LENGTH_LONG).show();
}

}
}
}

AndroidManifest.xml中权限、activity和receiver的设定:

view plaincopy to clipboardprint?
01.<?xml version="1.0" encoding="utf-8"?>
02.<manifest xmlns:android="http://schemas.android.com/apk/res/android"
03. package="jtapp.callandsms" android:versionCode="1" android:versionName="1.0">
04. <application android:icon="@drawable/icon" android:label="@string/app_name"
05. android:debuggable="true">
06. <activity android:name=".CallAndSms" android:label="@string/app_name">
07. <intent-filter>
08. <action android:name="android.intent.action.MAIN" />
09. <category android:name="android.intent.category.LAUNCHER" />
10. </intent-filter>
11. </activity>
12. <receiver android:name=".ReceiverSMS" android:enabled="true">
13. <intent-filter>
14. <action android:name="android.provider.Telephony.SMS_RECEIVED" />
15. </intent-filter>
16. </receiver>
17. </application>
18. <uses-permission android:name="android.permission.CALL_PHONE"></uses-permission>
19. <uses-permission android:name="android.permission.SEND_SMS"></uses-permission>
20. <uses-permission android:name="android.permission.RECEIVE_SMS"></uses-permission>
21.</manifest>
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="jtapp.callandsms" android:versionCode="1" android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name"
android:debuggable="true">
<activity android:name=".CallAndSms" android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name=".ReceiverSMS" android:enabled="true">
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
</application>
<uses-permission android:name="android.permission.CALL_PHONE"></uses-permission>
<uses-permission android:name="android.permission.SEND_SMS"></uses-permission>
<uses-permission android:name="android.permission.RECEIVE_SMS"></uses-permission>
</manifest>

补充,发Email 代码片段:

view plaincopy to clipboardprint?
01.//Email
02.Button bt5 = (Button) findViewById(R.id.Button05);
03.bt5.setOnClickListener(new OnClickListener() {
04. @Override
05. public void onClick(View v) {
06. // Setup the recipient in a String array
07. String[] mailto = { "noam@gmail.com" };
08. // Create a new Intent to send messages
09. 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.});
//Email
Button bt5 = (Button) findViewById(R.id.Button05);
bt5.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// Setup the recipient in a String array
String[] mailto = { "noam@gmail.com" };
// Create a new Intent to send messages
Intent sendIntent = new Intent(Intent.ACTION_SEND);
// Write the body of theEmail
String emailBody = "You're password is: ";
// Add attributes to the intent
//sendIntent.setType("text/plain"); // use this line for testing
// in the emulator
sendIntent.setType("message/rfc822"); // use this line for testing
// on the real phone
sendIntent.putExtra(Intent.EXTRA_EMAIL, mailto);
sendIntent.putExtra(Intent.EXTRA_SUBJECT, "Your Password");
sendIntent.putExtra(Intent.EXTRA_TEXT, emailBody);
startActivity(sendIntent);
}
});

扩展阅读:

Android下调用收发短信邮件等

http://sean.huanglijiang.com/article.asp?id=218

android 短信发送全过程

http://apps.hi.baidu.com/share/detail/15096826
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐