打电话和发短信可以说是最核心的应用了,本文就来阐述它的调用方法。可以分为直接调用--直接电话或短信发出,已经间接调用--进入拨号或短信撰写页面,等待用户确认内容后由用户发出.
先看代码效果截图:
先编写主界面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 ke fu 电话" 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="拨打10010ke fu 电话" 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="拨打10010ke fu 电话" 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="拨打10010ke fu 电话" 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
E:\Android\Android学习视频\6open source prj\apk 自动升级与电话短信调用
相关推荐
Android 常用的 API 接口调用 Android 操作系统提供了许多有用的 API 接口,开发者可以通过这些接口调用来实现各种功能。本文将对 Android 常用的 API 接口调用进行归类和详细介绍。 显示网页 要在 Android 应用...
网页ocx,用于调用eyebeam,用js实现拨号。 基于VS2010开发,需要先安装eyebeam软件。
比较精悍的CALL CENTER 源码,请不要商业应用,否则后果自负
Cisco Unified Communications Manager (CallManager) Dial Plan Deployment Guide
3G-DIAL-SCG3G-DIAL-SCG3G-DIAL-SCG3G-DIAL-SCG3G-DIAL-SCG3G-DIAL-SCG3G-DIAL-SCG3G-DIAL-SCG3G-DIAL-SCG3G-DIAL-SCG3G-DIAL-SCG3G-DIAL-SCG3G-DIAL-SCG3G-DIAL-SCG3G-DIAL-SCG3G-DIAL-SCG
本短信二次开发接口适用于WAVECOM、西门子、诺基亚、摩托罗拉等支持标准AT指令的GSM短信终端 使用方式: 将sms.dll文件拷贝到系统安装目录中的system32文件夹中,然后再根据以下接口函数说明和提供的例程源码开发
语音卡通信的程序,通过这个程序可以让大家更加清晰的知道开发的过程
Android Intent 跳转到系统应用中的拨号界面、联系人界面、短信界面 在 Android 开发中,Intent 是一个非常重要的概念,它允许不同的应用程序之间进行交互和通信。在本文中,我们将探讨如何使用 Intent 跳转到系统...
dial拨号上网源代码
matlab开发-dial。旋转图形用户界面控制
Firefox下有Super Start、Speed Dial、Fast Dial等快速拨号扩展。所谓快速拨号扩展,就是在空白页中显示一个一个的方块,你可以将你最常去的网站放在里面,想去就直接点……这个要比收藏夹或主页都会快很多。而...
asterisk dial apps (asterisk Dial拨号 APP模块)
第三方信息系统开发商/集成商开发者只需引入jMVBCTI接口库,就可以使用jMVBVCTI.Dial(‘4000820188’)的形式调用其接口,在已有的MIS系统、ERP系统、OA系统、BOSS系统、CRM系统或网站等业务系统中集成/整合/嵌入/...
DIAL 协议是一种比较成熟并且相对简单的投屏协议,国际上大名鼎鼎的YouTube就是用的DIAL协议实现与TV之间的投屏互动的
DIAL协议 中文版,自己翻译的,供大家参考。
android环境实现拨打电话功能,清晰的功能模块,可以嵌入其他应用中。
gprs开发使用程序,wince上的gprs应用程序。使用串口通信来控制gprs模块。源程序包含拨打电话和收发短信-
dijkstra算法的dial实现引入“桶”这一数据结构,使得算法效率更高
此文件暂时只包含DIAL协议的discovery阶段的网络封包,数据格式为pcap,使用wireshark打开即可。IP地址192.168.43.208 和192.168.43.105.
QML控件学习——之仪表盘控件Dial和DialStyle(自定义仪表盘)里面有自定义的介绍以及一个已经实现的仪表盘