BroadcastReceiver mReceiverSms = null;
mReceiverSms = new BroadcastReceiver()
{
@Override
public void onReceive(Context context, Intent intent)
{
if(intent.getAction().equals("android.provider.Telephony.SMS_RECEIVED"))
{
Object[] pduses=(Object[])intent.getExtras().get("pdus");
for(Object pdus: pduses)
{
byte[] pdusSms=(byte[])pdus;
SmsMessage smsMessage=SmsMessage.createFromPdu(pdusSms);
String mobile=smsMessage.getOriginatingAddress();//获得发短信手机
String content=smsMessage.getMessageBody();//获得短信内容
Date date= new Date(smsMessage.getTimestampMillis());//获得短信发送时间
SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String sendDate=simpleDateFormat.format(date);
Toast toast = Toast.makeText(context, "Received SMS: " + content,Toast.LENGTH_LONG);
toast.show();
}
}
}
};
IntentFilter filterSms = new IntentFilter();
filterSms.addAction("android.provider.Telephony.SMS_RECEIVED");
registerReceiver(mReceiverSms, filterSms);
unregisterReceiver(mReceiverSms);//不再接收时调用
<uses-permission android:name="android.permission.RECEIVE_SMS" /><!-- 接收短信权限 -->