-1

I am learning how to develop Apps on Android. I have a button that onClick, it should read my SMS and check the body of each SMS, if one SMS contains "WORD" then DO something.

It should be achieved using this, right?

 String body = sms.getMessageBody().toString();



                while (body.contains("WORD"))
                   DO SOMETHING

Thanks in advance.

EDIT

Is there any way to do this without hitting the button, progamatically every hour for example?

user2667879
  • 45
  • 2
  • 8

3 Answers3

1

if you want ot check it with new message you need to create BroadcastReceiver

AndroidManifest.xml Declaration :

<receiver android:name=".MySMSReceiver "> 
<intent-filter> 
        <action android:name="android.provider.Telephony.SMS_RECEIVED" /> 
</intent-filter> 

BroadcastReceiver :

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());
 }
}
SilentKiller
  • 6,944
  • 6
  • 40
  • 75
  • Hello, THIS is what I have, what should I put into? thank you! – user2667879 Aug 12 '13 at 18:37
  • `last = (Button) findViewById(R.id.last); last.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View arg0) { String body = sms.getMessageBody().toString(); while (body.contains("WORD")) DO SOMETHING } });` – user2667879 Aug 12 '13 at 18:38
1

instead of while you need to use an if statement...
if (body.contains("WORD"){ DO SOMETHING }

AndroidCB
  • 230
  • 4
  • 11
0

You can use AlarmManager to schedule some task to be run at some point in the future

See https://stackoverflow.com/a/8801990/1051147 for example

Community
  • 1
  • 1
Arun C
  • 9,035
  • 2
  • 28
  • 42