发送短信和打电话的方法

简介:

发送短信的方法

有两种方法可以实现发送短信,其一是使用intent-startActivityURI数据格式为"smsto:num",调用的actionIntent.ACTION_SENDTO

Uri uri = Uri.parse("smsto:5554");

Intent it = new Intent(Intent.ACTION_SENDTO, uri);

it.putExtra("sms_body""好。。");

startActivity(it);        

 

 

其二是使用SmsManager:

EditText num=(EditText)findViewById(R.id.num);

                EditText content=(EditText)findViewById(R.id.content);

                String mobile=num.getText().toString();

                String smstext=content.getText().toString();

                //获取SmsManager

                SmsManager sms=SmsManager.getDefault();

                //如果内容大于70字,则拆分为多条

                List<String> texts=sms.divideMessage(smstext);

                //逐条发送短信

                for(String text:texts)

                {

                    sms.sendTextMessage(mobile, null, text, nullnull);

                }                

                //发送结果提示

                Toast.makeText(SendSMS.this"发送成功", Toast.LENGTH_LONG).show();

二者的不同在于前者只是调用了发送界面,需要按下Send按钮短信才发送出去,而后者则是直接发送出去。

发送SMS权限的设置:

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

关于SmsManager

SDK中的介绍:Manages SMS operations such as sending data, text, and pdu SMS messages. Get this object by calling the static method SmsManager.getDefault().

方法:

public void sendTextMessage (String destinationAddress, String scAddress, String text, PendingIntent sentIntent, PendingIntent deliveryIntent)

 

destinationAddress: 收件人地址

scAddress: 短信中心号码,null为默认中心号码

sentIntent: 当消息发出时,成功或者失败的信息报告通过PendingIntent来广播。如果该参数为空,则发信程序会被所有位置程序检查一遍,这样会导致发送时间延长。

deliveryIntent: 当消息发送到收件人时,该PendingIntent会被广播。pdu数据在状态报告的extended data ("pdu")中。

如果收件人或者信息为空则抛出 IllegalArgumentException 。

public ArrayList<String> divideMessage (String text)

将大于70字的短信分割为多条。

参数:text    the original message. Must not be null.

返回:an ArrayList of strings that, in order, comprise the original message

sendDataMessage 参数与上类似,只是用于发送Data。

sendMultipartTextMessage发送多条短信,发送内容必须是用divideMessage分割好了的。

 

打电话的方法

打电话的方法类似,所不用的是URI格式为"tel:num",而调用的action为Intent.ACTION_CALL

EditText edit=(EditText)findViewById(R.id.DialEdit);

String num=edit.getText().toString();

if((num!=null)&&(!"".equals(num.trim())))

{

Intent intent=new Intent(Intent.ACTION_CALL,Uri.parse("tel:"+num));

                    startActivity(intent);

}

打电话权限的设置:

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

 

 

 

向模拟器发短信打电话的方法

1.启动android emulator,查看标题栏找出端口。一般是android emulator (5554),其中5554就是端口。

 

2.打开命令行,输入telnet localhost 5554。程序将会连接到android console,返回

Android Console: type 'help' for a list of commands

OK

模拟电话打入gsm <call|accept|busy|cancel|data|hold|list|voice|status>

 

输入gsm call <模拟打进的电话号码>。如:

gsm call 15555218135

模拟短信发送sms send <senderPhoneNumber> <textmessage>

输入sms send <模拟发送短信的电话> <内容>。如:

sms send 15555218135 hello

其中,15555218135为模拟器手机号码。

本文转自feisky博客园博客,原文链接:http://www.cnblogs.com/feisky/archive/2010/06/10/1755914.html,如需转载请自行联系原作者



相关文章
|
1月前
|
移动开发 JavaScript
H5唤起手机打电话(拨号)和发短信功能
H5唤起手机打电话(拨号)和发短信功能
47 0
|
7月前
|
移动开发
手机h5页面唤起打电话、发短信功能
手机h5页面唤起打电话、发短信功能
|
Java 云计算
JAVA实现《阿里云发送短信验证码以及短信通知》
JAVA实现《阿里云发送短信验证码以及短信通知》
831 0
|
C# Windows 数据安全/隐私保护
|
安全 数据安全/隐私保护 Android开发
消息服务发送短信,手机接收不到短信解决思路
阿里云平台,消息服务发送注册码,用户接收不到注册码
6146 0

热门文章

最新文章