Android系统广播实现短信监听

public class MySMS extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        if("android.provider.Telephony.SMS_RECEIVED".contains(intent.getAction())){
            Log.i("test","收到信息了");
            Bundle bundle=intent.getExtras();
            Object object[]= (Object[]) bundle.get("pdus");
            SmsMessage[] message=new SmsMessage[object.length];
            for (int i = 0; i < object.length; i++) {
                message[i]=SmsMessage.createFromPdu((byte[])object[i]);
            }
            for (SmsMessage smsMessage : message) {
                String address=smsMessage.getOriginatingAddress();
                String body=smsMessage.getDisplayMessageBody();
                Log.i("test",address+":"+body);
            }
        }
    }
}


进行配置:

 
     
    


你可能感兴趣的:(Android系统广播实现短信监听)