发送消息和打电话的方法

有两种方法可以实现发送短信,其一是使用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);        
 // Intent intent = new Intent(Intent.ACTION_VIEW);
// intent.setType("vnd.android-dir/mms-sms");
// intent.putExtra("sms_body", body);
 
其二是使用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为模拟器手机号码。
转自:http://www.cnblogs.com/feisky/archive/2010/06/10/1755914.html
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章