当前位置: 代码迷 >> Android >> 初学者学Android开发系列之:发送短信拨打电话
  详细解决方案

初学者学Android开发系列之:发送短信拨打电话

热度:95   发布时间:2016-05-01 16:58:17.0
菜鸟学Android开发系列之:发送短信拨打电话

在android中有两种方式发送短信:

第一种:

使用intent-startActivityURI数据格式为"smsto:num",调用的actionIntent.ACTION_SENDTO

Intent tent = new Intent();tent.setAction(Intent.ACTION_SENDTO);tent.setData(Uri.parse("smsto:5554"));tent.putExtra("sms_body", "android 你好!");startActivity(tent);

第二种:

使用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();//获取SmsManagerSmsManager 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-permissionandroid: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

Intent intent = new Intent();intent.setAction(Intent.ACTION_CALL);intent.setData(Uri.parse("tel:18982113450"));startActivity(intent);

?打电话权限的设置:

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

?扩展学习:

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

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 18982113450

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

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

sms send 18982113450 hello

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

  相关解决方案