发送短信和打电话的方法

发送短信的方法

有两种方法可以实现发送短信,

其一是使用 intent-startActivity

   U RI 数据格式为" smsto:num ", 调用的 action Intent.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, null , null );

                }                

                // 发送结果提示

                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为模拟器手机号码。

 

http://www.cnblogs.com/feisky/archive/2010/06/10/1755914.html


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值