直接上onReceive()方法中的代碼,清單文件中的action的配置就不再給出
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if(action.equals("android.provider.Telephony.SMS_RECEIVED")) {
//獲取短信內容,有可能一次發來多條短信
Object[] objects = (Object[]) intent.getExtras().get("pdus");
for (Object obj : objects) {
//獲取短信對象
SmsMessage msg = SmsMessage.createFromPdu((byte[]) obj);
//獲取短信內容
String msgBody = msg.getDisplayMessageBody();
//獲取發信人的電話號碼
String originatingAddress = msg.getOriginatingAddress();
Log.i("wk","短信內容==>"+msgBody+" 來自==>"+originatingAddress);
}
}
}