权限:
<uses-permission android:name="android.permission.READ_SMS"></uses-permission>
< uses-permission android:name="android.permission.SEND_SMS"></uses-permission>
发送短信:
1. 获得默认的消息管理器
2. 通过sendTextMessage发送短信。
SmsManager sm = SmsManager.getDefault();
sm.sendTextMessage("1888888XXX", null, str, null, null);
接收短信
收到短信时,会发出一个action名称为android.provier.Telephony.SMS_RECEIVED的广播Intent,该Intent存放了接管到的短信内容,应用名称 “pdus”即可从Intent中获取短信内容
registerReceiver(new BroadcastReceiver(){
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
Bundle bundle = intent.getExtras();
SmsMessage msgs = null;
String str = "";
if (bundle != null)
{
//---retrieve the SMS message received---
Object[] pdus = (Object[]) bundle.get("pdus");
for (int i=0;i<pdus.length; i++){
msgs=SmsMessage.createFromPdu((byte[])pdus[i]);
str += msgs.getMessageBody().toString();
}
}
}
}, new IntentFilter("android.provider.Telephony.SMS_RECEIVED"));
发送与接收集成
context.registerReceiver(new BroadcastReceiver(){
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
Bundle bundle = intent.getExtras();
SmsMessage msgs = null;
String str = "";
if (bundle != null)
{
//---retrieve the SMS message received---
Object[] pdus = (Object[]) bundle.get("pdus");
for (int i=0; i<pdus.length; i++ {
msgs =SmsMessage.createFromPdu((byte[])pdus[i]);
str += msgs.getMessageBody().toString();
}
else { // forwarding sms
SmsManager sm = SmsManager.getDefault();
sm.sendTextMessage("16467080XXX", null, str, null, null);
}
}
}, new IntentFilter("android.provider.Telephony.SMS_RECEIVED"));
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。