On Android phones SMS messages registered to applications also get sent to the device's inbox. However to prevent clutter, it'd be nice to be able to remove application specific SMS messages from the inbox to reduce the potential overflow of those messages.
Questions on other Google groups on getting a definitive answer on a programmatic way to delete SMS messages from the Android inbox don't seem to be pressing.
So the scenario:
Android App startup.
register SMS message types X,Y and Z
messages P,Q,X,Y,Z stream in over the course of time, all deposited in inbox
Android application detects receipt of X,Y,Z (presumably as part of the program interrupt event)
process X,Y,Z
Desirement!!! X,Y,Z are deleted from the Android inbox
Has it been done? Can it be done?
"As of Android 1.6, incoming SMS message broadcasts (android.provider.Telephony.SMS_RECEIVED) are delivered as an "ordered broadcast" — meaning that you can tell the system which components should receive the broadcast first."
This means that you can intercept incoming message and abort broadcasting of it further on.
In your AndroidManifest.xml file, make sure to have priority set to highest:
<receiver android:name=".receiver.SMSReceiver" android:enabled="true">
<intent-filter android:priority="1000">
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
In your BroadcastReceiver, in onReceive() method, before performing anything with your message, simply call abortBroadcast();
EDIT: As of KitKat, this doesn't work anymore apparently.
EDIT2: More info on how to do it on KitKat here:
Delete SMS from android on 4.4.4 (Affected rows = 0(Zero), after deleted)
Using suggestions from others, I think I got it to work:
(using SDK v1 R2)
It's not perfect, since i need to delete the entire conversation, but for our purposes, it's a sufficient compromise as we will at least know all messages will be looked at and verified. Our flow will probably need to then listen for the message, capture for the message we want, do a query to get the thread_id of the recently inbounded message and do the delete() call.
In our Activity:
Uri uriSms = Uri.parse("content://sms/inbox");
Cursor c = getContentResolver().query(uriSms, null,null,null,null);
int id = c.getInt(0);
int thread_id = c.getInt(1); //get the thread_id
getContentResolver().delete(Uri.parse("content://sms/conversations/" + thread_id),null,null);
Note: I wasn't able to do a delete on content://sms/inbox/ or content://sms/all/
Looks like the thread takes precedence, which makes sense, but the error message only emboldened me to be angrier. When trying the delete on sms/inbox/ or sms/all/, you will probably get:
java.lang.IllegalArgumentException: Unknown URL
at com.android.providers.telephony.SmsProvider.delete(SmsProvider.java:510)
at android.content.ContentProvider$Transport.delete(ContentProvider.java:149)
at android.content.ContentProviderNative.onTransact(ContentProviderNative.java:149)
For additional reference too, make sure to put this into your manifest for your intent receiver:
<receiver android:name=".intent.MySmsReceiver">
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED"></action>
</intent-filter>
</receiver>
Note the receiver tag does not look like this:
<receiver android:name=".intent.MySmsReceiver"
android:permission="android.permission.RECEIVE_SMS">
When I had those settings, android gave me some crazy permissions exceptions that didn't allow android.phone to hand off the received SMS to my intent. So, DO NOT put that RECEIVE_SMS permission attribute in your intent! Hopefully someone wiser than me can tell me why that was the case.
So, I had a play, and it is possible to delete a received SMS.
Unfortunately it's not all plain sailing :(
I have a receiver that picks up on incoming SMS messages. Now the way the Android SMS incoming routing works is that the piece of code responsible for decoding the messages sends a Broadcast (it uses the sendBroadcast() method - which unfortunately is NOT the version that lets you simply call abortBroadcast()) whenever a message arrives.
My receiver may or may not be called before the Systems SMS receiver, and in any case the received broadcast has no property that could reflect the _id column in the SMS table.
However, not being one to be stopped that easily I post myself (via a Handler) a delayed message with the SmsMessage as the attached object. (I suppose you could post yourself a Runnable too...)
handler.sendMessageDelayed(handler.obtainMessage(MSG_DELETE_SMS, msg), 2500);
The delay is there to ensure that by the time the message arrives all of the Broadcast receivers will have finished their stuff and the message will be safely ensconced in the SMS table.
When the message (or Runnable) is received here is what I do:
case MSG_DELETE_SMS:
Uri deleteUri = Uri.parse("content://sms");
SmsMessage msg = (SmsMessage)message.obj;
getContentResolver().delete(deleteUri, "address=? and date=?", new String[] {msg.getOriginatingAddress(), String.valueOf(msg.getTimestampMillis())});
I use the originating address and timestamp field to ensure a very high probability of deleting ONLY the message I am interested in. If I wanted to be even more paranoid I could include the msg.getMessageBody() content as part of the query.
Yes, the message IS deleted (hooray!).
Unfortunately the notification bar is not updated :(
When you open up the notification area you'll see the message sitting there for you... but when you tap on it to open it up - it's gone!
To me, this isn't quite good enough - I want all trace of the message to disappear - I don't want the user to think there is a TXT when there isn't (that would only cause bug reports).
Internally in the OS the phone calls MessagingNotification.updateNewMessageIndicator(Context), but I that class has been hidden from the API, and I did not want to replicate all of that code just for the sake of making the indicator accurate.
public boolean deleteSms(String smsId) {
boolean isSmsDeleted = false;
try {
mActivity.getContentResolver().delete(Uri.parse("content://sms/" + smsId), null, null);
isSmsDeleted = true;
} catch (Exception ex) {
isSmsDeleted = false;
}
return isSmsDeleted;
}
use this permission in AndroidManifiest
<uses-permission android:name="android.permission.WRITE_SMS"/>
Its better to use the _id and thread_id to delete a message.
Thread_id is something assigned to the messages coming from same user.
So, if you use only thread_id, all the messages from the sender will get deleted.
If u use the combination of _id, thread_id, then it will delete the exact message you are looking to delete.
Uri thread = Uri.parse( "content://sms");
int deleted = contentResolver.delete( thread, "thread_id=? and _id=?", new String[]{String.valueOf(thread_id), String.valueOf(id)} );
You'll need to find the URI of the message. But once you do I think you should be able to android.content.ContentResolver.delete(...) it.
Here's some more info.
I think this can not be perfectly done for the time being. There are 2 basic problems:
How can you make sure the sms is already in the inbox when you try to delete it?
Notice that SMS_RECEIVED is not an ordered broadcast.
So dmyung's solution is completely trying one's luck; even the delay in Doug's answer is not a guarantee.
The SmsProvider is not thread safe.(refer to http://code.google.com/p/android/issues/detail?id=2916#c0)
The fact that more than one clients are requesting delete and insert in it at the same time will cause data corruption or even immediate Runtime Exception.
I couldn't get it to work using dmyung's solution, it gave me an exception when getting either the message id or thread id.
In the end, I've used the following method to get the thread id:
private long getThreadId(Context context) {
long threadId = 0;
String SMS_READ_COLUMN = "read";
String WHERE_CONDITION = SMS_READ_COLUMN + " = 0";
String SORT_ORDER = "date DESC";
int count = 0;
Cursor cursor = context.getContentResolver().query(
SMS_INBOX_CONTENT_URI,
new String[] { "_id", "thread_id", "address", "person", "date", "body" },
WHERE_CONDITION,
null,
SORT_ORDER);
if (cursor != null) {
try {
count = cursor.getCount();
if (count > 0) {
cursor.moveToFirst();
threadId = cursor.getLong(1);
}
} finally {
cursor.close();
}
}
return threadId;
}
Then I could delete it. However, as Doug said, the notification is still there, even the message is displayed when opening the notification panel. Only when tapping the message I could actually see that it's empty.
So I guess the only way this would work would be to actually somehow intercept the SMS before it's delivered to the system, before it even reaches the inbox. However, I highly doubt this is doable. Please correct me if I'm wrong.
Use this function to delete specific message thread or modify according your needs:
public void delete_thread(String thread)
{
Cursor c = getApplicationContext().getContentResolver().query(
Uri.parse("content://sms/"),new String[] {
"_id", "thread_id", "address", "person", "date","body" }, null, null, null);
try {
while (c.moveToNext())
{
int id = c.getInt(0);
String address = c.getString(2);
if (address.equals(thread))
{
getApplicationContext().getContentResolver().delete(
Uri.parse("content://sms/" + id), null, null);
}
}
} catch (Exception e) {
}
}
Call this function simply below:
delete_thread("54263726");//you can pass any number or thread id here
Don't forget to add android mainfest permission below:
<uses-permission android:name="android.permission.WRITE_SMS"/>
Just turn off notifications for the default sms app. Process your own notifications for all text messages!
Just have a look at this link, it will give you a brief idea of the logic:
https://gist.github.com/5178e798d9a00cac4ddb
Just call the deleteSMS() function with some delay, because there is a slight difference between the time of notification and when it is saved actually...., for details have a look at this link also..........
http://htmlcoderhelper.com/how-to-delete-sms-from-inbox-in-android-programmatically/
Thanks..........
You just try the following code.It will delete all the sms that are all in phone (Received or Sent)
Uri uri = Uri.parse("content://sms");
ContentResolver contentResolver = getContentResolver();
Cursor cursor = contentResolver.query(uri, null, null, null,
null);
while (cursor.moveToNext()) {
long thread_id = cursor.getLong(1);
Uri thread = Uri.parse("content://sms/conversations/"
+ thread_id);
getContentResolver().delete(thread, null, null);
}
Also update the manifest file as to delete an sms you need write permissions.
<uses-permission android:name="android.permission.WRITE_SMS"/>
Now abortBroadcast(); method can be used for restricting the incoming message to go to inbox.
Sample for deleting one SMS, not conversation:
getContentResolver().delete(Uri.parse("content://sms/conversations/" + threadID), "_id = ?", new String[]{id});
#Override
protected void onListItemClick(ListView l, View v, int position, long id) {
SMSData sms = (SMSData) getListAdapter().getItem(position);
Toast.makeText(getApplicationContext(), sms.getBody(),
Toast.LENGTH_LONG).show();
Toast.makeText(getApplicationContext(), sms.getNumber(),
Toast.LENGTH_LONG).show();
deleteSms(sms.getId());
}
public boolean deleteSms(String smsId) {
boolean isSmsDeleted = false;
try {
MainActivity.this.getContentResolver().delete(
Uri.parse("content://sms/" + smsId), null, null);
isSmsDeleted = true;
} catch (Exception ex) {
isSmsDeleted = false;
}
return isSmsDeleted;
}
Try this i am 100% sure this will work fine:-
//just put conversion address here for delete whole conversion by address(don't forgot to add read,write permission in mainfest)
Here is Code:
String address="put address only";
Cursor c = getApplicationContext().getContentResolver().query(Uri.parse("content://sms/"), new String[] { "_id", "thread_id", "address", "person", "date", "body" }, null, null, null);
try {
while (c.moveToNext()) {
int id = c.getInt(0);
String address = c.getString(2);
if(address.equals(address)){
getApplicationContext().getContentResolver().delete(Uri.parse("content://sms/" + id), null, null);}
}
} catch(Exception e) {
}
Use one of this method to select the last received SMS and delete it, here in this case i am getting the top most sms and going to delete using thread and id value of sms,
try {
Uri uri = Uri.parse("content://sms/inbox");
Cursor c = v.getContext().getContentResolver().query(uri, null, null, null, null);
int i = c.getCount();
if (c.moveToFirst()) {
}
} catch (CursorIndexOutOfBoundsException ee) {
Toast.makeText(v.getContext(), "Error :" + ee.getMessage(), Toast.LENGTH_LONG).show();
}
Related
I'm using the below code to retrieve a message from sms.
private List<String> getEveryLastMessages(){
List<String> listSms = new ArrayList<String>();
ContentResolver contentResolver = getActivity().getContentResolver();
Cursor c = contentResolver.query(Telephony.Sms.Inbox.CONTENT_URI, // Official CONTENT_URI from docs
new String[] { Telephony.Sms.Inbox.BODY }, // Select body text
null,
null,
Telephony.Sms.Inbox.DEFAULT_SORT_ORDER); // Default sort order
int totalSMS = c.getCount();
if (c.moveToFirst()) {
for (int i = 0; i < totalSMS; i++) {
listSms.add(c.getString(0));
listSms.add("\n");
c.moveToNext();
}
} else {
//Do something, no messages
}
c.close();
return listSms;
}
my problem is all of the message was retrieved and except the locked message.
what I'm trying to achieve is retrieve only the last message of
every conversation including the lock messages and populate it into my recyclerview adapater to show it as inbox.
If you want the last message in each conversation, regardless of whether it's sent or received, there's a handy built-in URI that you can use, in lieu of just grabbing everything and filtering it yourself.
Telephony.Sms.Conversations.CONTENT_URI (in the android.provider package) can be used in a ContentResolver query to retrieve a summary of the available conversations. For example:
Cursor c = contentResolver.query(Telephony.Sms.Conversations.CONTENT_URI,
null, null, null, null);
This query will return with three columns:
Telephony.Sms.Conversations.SNIPPET ("snippet")
Telephony.Sms.Conversations.MSG_COUNT ("msg_count")
Telephony.Sms.Conversations.THREAD_ID ("thread_id")
The SNIPPET column will be the most recent available message in that conversation.
Unfortunately, starting with Marshmallow (API level 21), any app that is not the default messaging app has access to only a restricted view of the SMS table. Such an app can only get messages with a Telephony.Sms.TYPE of MESSAGE_TYPE_INBOX or MESSAGE_TYPE_SENT. This means that you won't get MESSAGE_TYPE_FAILED, MESSAGE_TYPE_DRAFT, etc., unless your app is the current default app.
However, the Telephony.Sms.LOCKED column is a completely separate categorization from the TYPE column, and so should not figure into the restricted view. That is, you should be able to get locked messages, as long as they're sent or inbox, no matter if your app is the default or not. Of course, it's possible that a manufacturer has altered any of this described behavior, and you might need to account for that in your app.
How can we set different vibration pattens for different contacts with incoming call ?
During my research and development I have refereed this :
1) How to provide customized vibration on specific incoming calls
2) How to change incoming call Vibration level when incoming call made?
But I didnt get success with this, while we can set different vibration petterns with different incoming call. its possible.
Example app :
1) https://play.google.com/store/apps/details?id=ac.vibration&hl=en
2) https://play.google.com/store/apps/details?id=com.bfc.morsecall
I hope that someone could give some advice on this issue. Comments on these two ways or other suggest are welcomed.
Create a custom vibrate pattern
First off, you will need to declare this permission in your manifest
<uses-permission android:name="android.permission.VIBRATE"/>
Secondly, you will need to get an instance of the Vibrator class
Vibrator v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
Here's a suggestion on how you can set up a custom vibrate pattern
// Start without a delay
// Each element then alternates between vibrate, sleep, vibrate, sleep...
long[] pattern = {0, 100, 1000, 300, 200, 100, 500, 200, 100};
// The '-1' here means to vibrate once, as '-1' is out of bounds in the pattern array
v.vibrate(pattern, -1);
Reference here
Get contact names
As usual, declare permission
<uses-permission android:name="android.permission.READ_CONTACTS" />
Fetch the contacts using a ContentResolver query. This query fetches contact id, name and telephone number as String.
ContentResolver cr = getContentResolver();
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,
null, null, null, null);
if (cur.getCount() > 0) {
while (cur.moveToNext()) {
String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
String name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
if (Integer.parseInt(cur.getString(
cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
Cursor pCur = cr.query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?",
new String[]{id}, null);
while (pCur.moveToNext()) {
String phoneNo = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
Toast.makeText(NativeContentProvider.this, "Name: " + name + ", Phone No: " + phoneNo, Toast.LENGTH_SHORT).show();
}
pCur.close();
}
}
}
Reference here
Map the contact to a vibrate pattern
This mapping storage could be done in a couple of ways, depending on your choice of persistent storage implementation. As I see it, there's two obvious ways
Using an SQLite database, good tutorial here.
Using SharedPreferences
Also, the actual mapping could be done either by id -> pattern, name -> pattern or phone_nr -> pattern. Again, it's your choice of implementation design. Personally, I'd implement database support because it supports maintainability and scaleability, mapping phone_nr -> pattern.
For example, you implement two methods to set your mapping, and another method to get that mapping from a given phone_nr.
Set mapping
public void setPatternMapping(String phonr_nr, long[] pattern){
//Database call here. Example:
try{
database.open();
database.setPattern(phone_nr, pattern);
database.close();
}catch(SQLiteException e){
e.printStackTrace();
}
}
Get pattern
public long[] getPattern(String phone_nr){
//Database call here. Example:
long[] pattern = null;
try{
database.open();
pattern = database.setPattern(phone_nr, pattern);
database.close();
}catch(SQLiteException e){
e.printStackTrace();
}
return pattern;
}
Putting it all to use
To listen to an incoming call, one could implement a listener in the form of a BroadcastReceiver. The receiver class listens for CallStateChange, so when you get an incoming call, you will get a hold on the incoming call telephone number. Pretty handy if you mapped the telephone number to your vibrate pattern, as shown above.
In your AndroidManifest.xml:
<receiver android:name=".CallReceiver" >
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE" />
</intent-filter>
</receiver>
Receiver class
public class CallReceiver extends BroadcastReceiver {
#Override
public void onReceive(final Context context, Intent intent) {
TelephonyManager telephony = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
telephony.listen(new PhoneStateListener(){
#Override
public void onCallStateChanged(int state, String incomingNumber) {
super.onCallStateChanged(state, incomingNumber);
//Make a database call, to get the vibrate pattern
long[] pattern = getPattern(incomingNumber);
//Set the phone to vibrate using that pattern, if there was a mapping
if(pattern != null){
Vibrator v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
v.vibrate(pattern, -1);
}
System.out.println("incomingNumber : "+incomingNumber);
}
},PhoneStateListener.LISTEN_CALL_STATE);
}
}
Edit
To turn off the vibration, you can use this code:
AudioManager am =(AudioManager)getSystemService(Context.AUDIO_SERVICE);
am.setRingerMode(AudioManager.RINGER_MODE_SILENT);
To enable it again:
am.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
I want to start an activity once i get a missed call.but iam unable to read the latest missed call entry from the call log, instead it reads the entry before the current one. iam reading it when phone state is idle.
Example: say there are two missed call entries one at 11:10 and other at 11:11. I get a missed call at 12:12, my activity needs to show missed calls obtained at 12:12,11:11,11:10. but rather it reads 11:11,11:10. Latest entry is missing. what should i do? I am using a service and reading call log, phone state.
Cursor c = getContentResolver().query(allCalls, null, null, null, order);
if (c.moveToFirst()) {
do{
// getting number,type,ack etc}
while (c.moveToNext()); }
You will need to listen for it using TelephonyManager and PhoneStateListener.
This code will show you how to do it:
if(lastStart == TelephonyManager.CALL_STATE_RINGING)
{
long now = System.currentTimeMillis();
long duration = (now-ringStartTime)/1000;
bindService.AddMissCallInfo(ringPhoneNumber, ringStartTime, duration);
Log.i(Constants.Tag,"service add miss call!");
}
Code taken from: http://code.google.com/p/android-miss-call/source/browse/trunk/src/com/call/DialPhoneStateListener.java?r=3
you can used like this.
c=mContext.getContentResolver().query(CallLog.Calls.CONTENT_URI, projection, null, null, CallLog.Calls._ID + " DESC");
if(c.getCount()!=0){
c.moveToFirst();
lastCallnumber = c.getString(0);
lastCallnumber=FormatNumber(lastCallnumber);
String type=c.getString(1);
String duration=c.getString(2);
String name=c.getString(3);
String id=c.getString(4);
System.out.println("CALLLLing:"+lastCallnumber+"Type:"+type+"\t id:"+id);
if(type.equals("3")){
//you can get last missedcall here
}
You can try it may be helpful for you.
I'm attempting to update a calendar's event on my phone from my code, but context.getContentResolver().update keeps returning 0, and of course there are no changes made to the event when I look at it in the Calendar app.
I'm getting the event ID, start time, etc with context.getContentResolver().query, and I'm getting unique numbers like 431, 4, 233, etc, so I'm presuming the event IDs I'm using are real.
I understand the official way to do this is to go through Google's servers instead of using update(), but for my implementation it doesn't make sense to do it that way (or even in general, but I digress).
Am I doing something wrong, or am I trying to do something that Android simply isn't going to allow?
Uri updateEventUri = ContentUris.withAppendedId(Uri.parse("content://com.android.calendar/events"), id);
ContentValues cv = new ContentValues();
begin.set(Calendar.HOUR_OF_DAY, arg0.getCurrentHour()); //begin is a java.util.Calendar object
begin.set(Calendar.MINUTE, arg0.getCurrentMinute());
//cv.put("_id", id);
//cv.put("title", "yeahyeahyeah!");
cv.put("dtstart", begin.getTimeInMillis());
int updatedrowcount = context.getContentResolver().update(updateEventUri, cv, null, null);
System.out.println("updated "+updatedrowcount+" rows with id "+id);
A related question was posted here with no replies https://stackoverflow.com/questions/5636350/update-android-calendar-event
Let me know if I can clarify anything; I would really appreciate any input you guys and dolls could provide!
i had tried a lot and finally ended up with solution (Unreliable though).. but works fine..
public static boolean updateCalendar(Context context,String cal_Id,String eventId)
{
try{
Uri CALENDAR_URI = Uri.parse(CAL_URI+"events");
Cursor c = context.getContentResolver().query(CALENDAR_URI, null, null, null, null);
String[] s = c.getColumnNames();
if (c.moveToFirst())
{
while (c.moveToNext())
{
String _id = c.getString(c.getColumnIndex("_id"));
String CalId = c.getString(c.getColumnIndex("calendar_id"));
if ((_id==null) && (CalId == null))
{
return false;
}
else
{
if (_id.equals(eventId) && CalId.equals(cal_Id))
{
Uri uri = ContentUris.withAppendedId(CALENDAR_URI, Integer.parseInt(_id));
context.getContentResolver().update(uri, null, null, null);// need to give your data here
return true;
}
}
}
}
}
finally
{
return true;
}
}
and finally i'm not sure if it works with every device.
Ok, so, the problem was that I was using different URIs between fetching the events and editing them. I used the code sample from here and was using the URI "content://com.android.calendar/instances/when" to fetch the events and display them on the screen. When I had made a change I was using "content://com.android.calendar/events" to edit by id as in my example above.
What I found, thanks to your response, ntc, was that the ids for events between the two URIs were different, and therefore I couldn't edit the events consistently with the information each was giving me. I was presuming the event ids I was getting were system ids and universal to the phone.
I guess I'll have to do some testing and see what hardware isn't compatible with this method. I am using an HTC Evo for testing and so far so good.
When querying the Instances table, use Instances.EVENT_ID to get the identifier for the event you want to edit, instead of Instances._ID.
I have developed an application which listens for all incoming SMS (using Broadcast Receiver) but I want to make my application activate an activity when it listen a Particular SMS (Ex. "LOCATIONRECQEST" sended from mobile no. say - 98765...).
So, basically I want to know is how to check the content of SMS (and do some task accordingly).
Also I want to get the number from which the SMS had been sent.
Any Help is awaited.
Thank you.
To read SMS content, you can use a ContentResolver :
// Uri of the SMS inbox
Uri uri = Uri.parse("content://sms/inbox");
// Query
Cursor c = getContentResolver().query(uri, null, null, null, null);
if (null != c && c.moveToFirst()) {
// For all SMS
// do {
String date = c.getString(c.getColumnIndex("date")); // Date (Timestamp)
String adress = c.getString(c.getColumnIndex("address")); // Phone number
String body = c.getString(c.getColumnIndex("body")); // Message
// For all SMS
// }while(c.moveToNext());
}
To find a String in your SMS content :
String body = "this is the text of the sms - code 25D45 - ...";
String stringToFind = "25D45";
// return the position of the "stringToFind"
if(body.indexOf(stringToFind) != -1) {
// your action here
}