新しいメッセージで確認したい場合は、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());
}
}