定义广播接收器的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);
}
}else{
smsManager.sendTextMessage(phoneNumber,content,null);
}
}
以上是内存溢出为你收集整理的Android短信接收监听、自动回复短信 *** 作例子全部内容,希望文章能够帮你解决Android短信接收监听、自动回复短信 *** 作例子所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)