如果你想用新消息检查它,你需要创建 BroadcastReceiver
AndroidManifest.xml 声明:
<receiver android:name=".MySMSReceiver ">
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
广播接收器:
public class MySMSReceiver extends BroadcastReceiver {
@Override
public void onReceiver(Context context, Intent intent) {
Object[] pdus=(Object[])intent.getExtras().get("pdus");
String sender="";
StringBuilder text=new StringBuilder();
// get sender from first PDU
SmsMessage shortMessage=SmsMessage.createFromPdu((byte[]) pdus[0]);
sender=shortMessage.getOriginatingAddress();
for (int i=0;i<pdus.length;i++) {
shortMessage=SmsMessage.createFromPdu((byte[]) pdus[i]);
text.append(shortMessage.getDisplayMessageBody());
}
while (text.contains("WORD")) {
// DO SOMETHING
}
Log.d("SMSReceiver","SMS message sender: " + shortMessage.getOriginatingAddress());
Log.d("SMSReceiver","SMS message text: " + shortMessage.getDisplayMessageBody());
}
}