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
}
}
Related
I'm getting all phone's SMS with this URI - Telephony.Sms.Inbox.CONTENT_URI
TextBasedSmsColumns contains all columns for a projection.
Cursor is built like this:
cursor = getContentResolver().query(Telephony.Sms.Inbox.CONTENT_URI,
new String[] { BaseColumns._ID, TextBasedSmsColumns.ADDRESS },
null, null, null);
There is a TextBasedSmsColumns.ADDRESS column that contains a phone number OR a contact name. So how can I always get the phone number for the SMS? Should I use another approach to get all SMS?
This is how I got.
Uri uriSMSURI = Uri.parse("content://sms/inbox");
Cursor cur = getContentResolver().query(uriSMSURI, null, null, null, null);
if (cur != null) {
while (cur.moveToNext()) {
String body = cur.getString(cur.getColumnIndexOrThrow("body"));
//Here you will get all PhoneNumbers
String phoneNumber = cur.getString(cur.getColumnIndexOrThrow("address"));
}
cur.close();
}
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();
How to Differentiate MMS and SMS via MMS/SMS listeners in Android? Because I got SMS counts as in MMS counts?
I am calculating Total MMS as using following code.
Uri mmsInboxUri = Uri.parse("content://mms/inbox");
Uri mmsSentUri = Uri.parse("content://mms/sent");
Cursor mmsInboxCursor = getContentResolver().
query(mmsInboxUri ,new String[] {"_id"}, null, null);
Cursor mmsSentCursor = getContentResolver().
query(mmsSentUri ,new String[] {"_id"}, null, null);
int mmsCount = mmsInboxCursor.getCount() + mmsSentCursor.getCount();
You should check the column ct_t. If it's a MMS it should be equal to application/vnd.vap_multipart.related.
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?
I want to display a list of the contact names from the SMS inbox and outbox (like in the native messaging app). I have come up with the following code:
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
Uri messagesUri = Uri.parse("content://sms/");
Cursor cursor = getContentResolver().query(messagesUri,new String[] { "_id", "thread_id", "address", "person", "date", "body", "type" }, null, null, null);
startManagingCursor(cursor);
String[] columns = new String[] { "address", "person", "date", "body", "type" };
String sms = "";
if (cursor.getCount() > 0) {
while (cursor.moveToNext()){
String address = cursor.getString(cursor.getColumnIndex(columns[0]));
sms += address + " ";
String contact=address;
Uri uri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, Uri.encode(address));
Cursor cs= getContentResolver().query(uri, new String[]{PhoneLookup.DISPLAY_NAME},PhoneLookup.NUMBER+"='"+address+"'",null,null);
startManagingCursor(cs);
if(cs.getCount()>0)
{
cs.moveToFirst();
contact=cs.getString(cs.getColumnIndex(PhoneLookup.DISPLAY_NAME));
}
listItems.add(contact);
}
}
adapter=new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1,
listItems);
setListAdapter(adapter);
}
This works when I run the application in an emulator, but when I try to run it on a phone I get a NullPointerException. If I double-click on the error message in the LogCat the following line gets highlighted:
if(cs.getCount()>0)
What is the problem here?
From the documentation for ContentResolver.query(...)
A Cursor object, which is positioned before the first entry, or null
You must always check for null when using a ContentResolver to query for data.
Also from the same documentation.
Use question mark parameter markers such as 'phone=?' instead of explicit values in the selection parameter, so that queries that differ only by those values will be recognized as the same for caching purposes.
So instead of:
Cursor cs= getContentResolver().query(uri, new String[ {PhoneLookup.DISPLAY_NAME},PhoneLookup.NUMBER+"='"+address+"'",null,null);
Do:
Cursor cs= getContentResolver().query(uri, new String[]{PhoneLookup.DISPLAY_NAME},PhoneLookup.NUMBER+"=?",new String[]{address},null);
My first guess would be that cs is null. As to why it is null, I suppose that the carrier/manufacturer could have changed how this is implemented on your device. Without knowing too much about how these systems work, it looks like you are querying against some kind of database and then using a cursor to iterate over the data. If the representation has been changed, the cursor will break. My suggestion would be to see if there is some other solution that is more Google/API driven (as opposed to reading the raw data).