参考 http://blog.csdn.net/mad1989/article/details/22426415/
1. 创建一个广播接受者类
public class SMSReceiver extends BroadcastReceiver{ @Override public void onReceive(Context context, Intent intent) { Log.e("John", "SMSReceiver" + "收到短信的广播"); // 根据接收到新消息广播,然后执行响应的操作 Bundle bundle = intent.getExtras(); if(bundle != null){ Object[] smsObjs = (Object[]) bundle.get("pdus"); String messageBody = null; for(Object sms : smsObjs){ SmsMessage msg = SmsMessage.createFromPdu((byte[]) sms); messageBody = msg.getMessageBody(); }
}
}
}
2. 注册这个接受者
<receiver android:name=".receiver.SMSReceiver"> <intent-filter android:priority="1000"> <action android:name="android.provider.Telephony.SMS_RECEIVED"/> </intent-filter> </receiver>
3. 添加短信的读写权限
<uses-permission android:name="android.permission.RECEIVE_SMS" /> <uses-permission android:name="android.permission.READ_SMS" />