Android 之 ACTION_CALL 拨打电话 与 发送短信

来源:互联网 发布:知乎 我的收藏 编辑:程序博客网 时间:2024/05/17 09:30

要拨打电话,关键有2点:

1.   声明<uses-premission  android:name="android.permission.CALL_PHONE"> 权限

2.  通过action.CALL动作,触发系统拨打电话事件

在本示例中,假设电话号码格式为   区号3位 + 号码8位,实际开发可根据需求进行调整

具体代码见 my_call_phone工程

---------------------

短信功能是手机的一项重要功能,注意一下:GSM的规范为70个Unicode16文字为 1 则短信

关键程序是通过SmsManager对象的sendTextMessage()方法来完成,其中sendTextMessage(String destinationAddress,String scAddress,

String text,PendingIntent  sentIntent,PendingIntent deliveryIntent)方法,5个参数依次为: 收件人地址,发送地址,正文,发送服务,送达服务

其中收件人,正文2个参数不可为空.

流程:  在这个2个参数同时通过的前提下,通过PendingIntent.getBroadcast()的方法自定义PendingIntent并进行Broadcasting,紧接着创建一个SmsManager类的对象,

由smsManager使用sendTextMessage()方法传入参数,即完成发送短信

具体代码见 my_sendMessage 工程, 接收示意图如下:

 

 在这个程序中使用发送短信的方式,只使用sendTextMessage()方法,它只是三种传送短信的方法之一,完整的三种方法如下表如示

方法名称
使用时机
sendDataMessage(String destingationAddress,String scAddress,short destinactionPort,byte[] data,PendingIntent  sentIntent,PendingIntent  deliveryIntent)
参数依次为: 收件人,发件人,收件人端口号,信息,发送服务,接收服务
发送Data格式的SMS传送到特定程序的Port
sendMultipartTextMessage(String destinationAddress,String scAddress,ArrayList<String> parts, ArrayList<PendingIntent> sentIntents,ArrayList<PendingIntent> deliveryIntents )
参数依次为: 收件人,发件人, 有序的信息列表,一组发送服务,一组接收服务
发送多条文字短信
sendTextMessage(String desinationAddress,String scAddrsss,String text,PendingIntent sentIntent,PendingIntent deliveryIntent)
参数依次为: 收件人,发件人,信息,发送服务,接收服务
发送文字短信

在SmsManager类 还有一个公有方法public ArrayList<String> divideMessage(String  text),当短信的字数超过上限时,

自动拆分成数则短信,其中参数text 为原始短信内容,

而divideMessage()方法的返回值为ArrayList 类型,再通过sendMultipartTextMessage()发送

 

原创粉丝点击