How to search word in SMS inbox within the application? - android

I am new to Android. I want to know how to search by word in SMS inbox within the application. Can anyone help me. Please give me the source code if you could.

** Use Content Resolver ("content://sms/inbox") to read SMS which are in inbox.**
Cursor cursor = getContentResolver().query(Uri.parse("content://sms/inbox"), null, null, null, null);
cursor.moveToFirst();
do{
String msgData = "";
for(int idx=0;idx<cursor.getColumnCount();idx++)
{
msgData += " " + cursor.getColumnName(idx) + ":" + cursor.getString(idx);
}
if(msgData.contains(yourWord))
;// do something
}while(cursor.moveToNext());

This seems to have been answered several times already here and there for pointer for instance.
Please do search for existing questions before creating new ones.

first you have to query the inbox content provider for sms
Uri uriSMSURI = Uri.parse("content://sms/inbox");
Cursor cur = getContentResolver().query(uriSMSURI,new String[] { "_id", "thread_id", "address", "person", "date", "body" }, null, null,null);
Then search iterate and get sms body
if(cur.getCount()>0) {
while(cur.moveToNext()) {
String smsBody = cur.getString(5);
Log.v("body",smsBody);
if(smsBody.contains("matching string")) {
}
also refer this link
how can i read inbox in android mobile?

Related

In Sqlite, LIKE function is not working or return nothing

I use below code to get number from database from given number, for that I fired below query to get number but it does not return number. Is there wrong something in my query? please help me to resolved this issue.
I need to use LIKE function because some number stored with special char and whitespace.
contactNumber = contactNumber.replaceAll("[-+.^:,]", "");
contactNumber="%"+contactNumber+"%";
String strEmail = "";
String name = "";
Cursor cursor = ctx.getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
ContactsContract.CommonDataKinds.Phone.NUMBER + " LIKE ?", new String[]{contactNumber}, null);
This is the way to use LIKE when using ContentProviders:
getContentResolver().query(Phone.CONTENT_URI, null, Phone.NUMBER + " LIKE ?", new String[] { "%" + contactNumber + "%" }, null);
However, the best way to search by phone number is to use the dedicated PhoneLookup API:
Uri uri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, Uri.encode(phoneNumber));
getContentResolver().query(uri, new String[]{PhoneLookup.CONTACT_ID, PhoneLookup.DISPLAY_NAME}, null, null, null);
It's both optimised to return a result faster, and handles phone numbers with or without country-codes.

How to access miscall and new message state in android?

I wanna make an android app that runs in background and check if there is a miscall or new massage and if it is true do something.How can I run my app in background (it does not close when I click back button) and how to access miscall and new message state I am an amateur in android.
Thanks!
For not seen sms messages there is a column in content://sms that you can check in order to determine if a message has been seen.
Column name is "seen".
example of calling it and checking:
ContentResolver mContectResolver = context.getContentResolver();
Uri uri = Uri.parse("content://sms/");
String[] mProjection = {
"_id",
"address",
"person",
"date",
"body",
"protocol",
"seen"
};
Cursor cursor = mContectResolver.query(uri, mProjection, null, null, null);
cursor.moveToFirst();
for(int i = 0 ; i<cursor.getCount() ; i++){
if(cursor.getString(cursor.getColumnIndexOrThrow("seen")).equalsIgnoreCase("1")){
//Message has not been seen
}
cursor.moveToNext();
}
cursor.close();

Determine if a phone number has sms messages in android

Is there a way to tell if a phone number has an sms(s) in android ??
I want to know if there is a way to tell using the sms content porvider I know its not documented and not recommanded to use but is there a way to tell ??
what i have tried .
this statement will get all the inbox sms(s)
cursor = getContentResolver().query(
Uri.parse("content://sms/inbox"), null, null, null,
null);
I cant use the where clause like this
cursor = getContentResolver().query(
Uri.parse("content://sms/inbox"), "address =?", new String[]{"001435436654747"}, null,
null);
because the number may be stored without the global code the "001" or such .
is there a work around for this ??
Hi you can use below code to do this , I use this in my application to listen incoming sms to reply automaticaly.
Uri myMessage = Uri.parse("content://sms/");
ContentResolver cr = getContentResolver();
Cursor c = cr.query(myMessage, new String[] {
"_id", "address", "date", "body",
"read" }, null, null, null);
System.out.println(Number);
while (c.moveToNext()) {
Number = c.getString(c.getColumnIndexOrThrow("address")).toString();
if (Number.equals("001435436654747")) {
// DO YOUR STUFF
}
}

Android sms reading by number

I am working on a sms application. in my App I have to list all the convrsation .SO i use the following Uri.
content://mms-sms/conversations/
Its working fine.And the following is my code snippet.
Uri uri = Uri.parse("content://mms-sms/conversations/");
Cursor c= getContentResolver().query(uri, null, null ,null,null);
startManagingCursor(c);
if(c.moveToFirst()){
for(int i=0;i<c.getCount();i++){
body[i]= c.getString(c.getColumnIndexOrThrow("body")).toString();
number[i]=c.getString(c.getColumnIndexOrThrow("address")).toString();
c.moveToNext();
}
}
c.close();
for (int i = 0; i < body.length; i++) {
System.out.println("body ="+body[i]);
System.out.println("number ="+number[i]);
}
The number prints each conversations number and body prints the each conversations last message. But i want to get each conversations whole message and also help me to conversation for a specific number.
From Conversationscursor, You can get thread_id. Once you got thread_id, continue query from sms provider to return all message which has the same thread_id
Example : thread_id = 2
Uri SMS_INBOX = Uri.parse("content://sms");
Cursor c = getContentResolver().query(SMS_INBOX, null, "thread_id" + " = "
+ "2", null,
"date" + " ASC");
while (c.moveToNext()){
Log.v("BODY", c.getString(11).toString()); // 11 is the index of body with project URI
}

How do I change contact numbers to contact names?

Is there a way to display the contact names instead of the contact numbers?
This method returns the phone numbers within an inbox, but I need the names instead:
UPDATE: code changed from the original.
NOTE: I'm trying to change the contact numbers to display their corresponding contact names in the INBOX. Im not trying to get a list of all available contacts names.
public ArrayList<String> fetchInboxNumbers() {
ArrayList<String> sms = new ArrayList<String>();
Uri uriSms = Uri.parse("content://sms/inbox");
Cursor cursor = getContentResolver().query(uriSms,
new String[] { "_id", "address", "date", "body" }, null, null,
null);
cursor.moveToFirst();
while (cursor.moveToNext()) {
String address = cursor.getString(1); // Displays phone number
sms.add(address);
}
return sms;
} // END FETCHINBOX
Is there a way to display the contact names instead of the contact
numbers?
Following Snippet will help you.
String whereName = ContactsContract.Data.MIMETYPE + " = ?";
String[] whereNameParams = new String[] { ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE };
Cursor nameCur = contentResolver.query(ContactsContract.Data.CONTENT_URI, null, whereName, whereNameParams, ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME);
while (nameCur.moveToNext()) {
String given = nameCur.getString(nameCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME));
String family = nameCur.getString(nameCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME));
String display = nameCur.getString(nameCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME));
}
nameCur.close();
I will like to add to #Vipul Shah answer.
wouldnt be better to add a projection with only the columns that you need to retreive?
new String [] { ContactsContract.CommonDataKinds.Phone._ID,
ContactsContract.CommonDataKinds.Phone.NUMBER,
ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME }
Regards

Categories

Resources