Android短信接收监听、自动回复短信操作例子
程序员文章站
2022-06-04 12:16:49
定义广播接收器的action:复制代码 代码如下:private static final string tag ="smsservice";/*** 信息发送状态广播*/...
定义广播接收器的action:
复制代码 代码如下:
private static final string tag ="smsservice";
/**
* 信息发送状态广播
*/
private static final string action_sms_send = "com.smsservice.send";
/**
* 信息接收状态广播
*/
private static final string action_sms_delivery = "com.smsservice.delivery";
/**
* 信息接收广播
*/
private static final string action_sms_receiver = "android.provider.telephony.sms_received";
定义并注册广播接收器,可以动态注册,也可以静态注册:
复制代码 代码如下:
class smsreceiver extends broadcastreceiver {
@override
public void onreceive(context context, intent intent) {
// todo auto-generated method stub
int resultcode = getresultcode();
if (intent.getaction().equals(action_sms_receiver)){
log.i(tag, "smsreceiver->onreceive");
smsmessage sms = null;
bundle bundle = intent.getextras();//获取intent中的内容
if (bundle != null) {
object[] pdus = (object[]) bundle.get("pdus");//获取bundle里面的内容
for (object obj : pdus) {
//下面两行将短信内容取出加入到message中
sms = smsmessage.createfrompdu((byte[]) obj);
}
}
mhandler.obtainmessage(msg_sms_receive,sms).sendtotarget();
//接收信息发送状态
}else if (intent.getaction().equals(action_sms_send)){
switch (resultcode) {
case activity.result_ok:
log.i(tag, "短信发送成功");
break;
case smsmanager.result_error_generic_failure:
log.i(tag, "短信发送失败:generic_failure");
break;
case smsmanager.result_error_no_service:
log.i(tag, "短信发送失败:no_service");
break;
case smsmanager.result_error_null_pdu:
log.i(tag, "短信发送失败:null_pdu");
break;
case smsmanager.result_error_radio_off:
log.i(tag, "短信发送失败:radio_off");
break;
}
//接收信息接收状态
}else if (intent.getaction().equals(action_sms_delivery)){
switch (resultcode) {
case activity.result_ok:
log.i(tag, "短信接收成功");
break;
case smsmanager.result_error_generic_failure:
log.i(tag, "短信接收失败:generic_failure");
break;
case smsmanager.result_error_no_service:
log.i(tag, "短信接收失败:no_service");
break;
case smsmanager.result_error_null_pdu:
log.i(tag, "短信接收失败:null_pdu");
break;
case smsmanager.result_error_radio_off:
log.i(tag, "短信接收失败:radio_off");
break;
}
}
}
}
这样就实现了短信自动回复功能,当然也可以实现接收到短信自动回拨电话。这里提供一个短信接收函数:
复制代码 代码如下:
public void doreceivesms(intent intent) {
object[] pdus = (object[]) intent.getextras().get("pdus");
smsmessage [] messages = new smsmessage[pdus.length];
for(int i=0; i<pdus.length; i++){
byte[]pdu = (byte[]) pdus[i];
//从pud中创建一个消息
messages[i] = smsmessage.createfrompdu(pdu);
}
for(smsmessage msg: messages){
//获取短信的内容
string content = msg.getmessagebody();
//获取发送的人
string sender = msg.getoriginatingaddress();
//获取短信的时间
long timer = msg.gettimestampmillis();
//将毫秒数字转换成日期格式
date date = new date(timer);
simpledateformat sdf = new simpledateformat("yyyy-mm-dd hh-mm-ss");
string time = sdf.format(date);
string smscontent = time + ": " + sender + ": " + content;
//调用发送短信的方法
sendsmsmessage("5556", smscontent);
}
}
短信发送:
复制代码 代码如下:
public void sendsmsmessage(string phonenumber, string content){
smsmanager smsmanager = smsmanager.getdefault();
//判断短信内容的长度,如果长度大于70就会出错,所以这步很重要
if(content.length() >= 70){
list<string> list = smsmanager.dividemessage(content);
for(string mmsg: list){
smsmanager.sendtextmessage(phonenumber, null, mmsg, null, null);
}
}else{
smsmanager.sendtextmessage(phonenumber, null, content, null, null);
}
}