Reading text from SMS, and show it as text view - android

I am trying to create app, that will get the text from SMS, and use it in textview. So something like this, message is recived, i check if it is message I want, then i extract text, save it to string, and then show this string in textview. Any suggestions from where should i start, any examples plese ??

You can start here for handling received SMS.

First I would listen for SMS incoming, and on incoming SMS show a notification. Then if the user opens your app, update your display using this to get the data you want:
Uri allMessage = Uri.parse("content://sms/inbox");
ContentResolver cr = getContentResolver();
Cursor c = cr.query(allMessage, null, null, null, null);
//shows one message
c.moveToNext();
//uncomment to cycle thru ALL messages... This will take AWHILE
//while (c.moveToNext()) {
for(int i = 0; i != c.getColumnCount(); i++){
String columnName = c.getColumnName(i);
String columnValue = c.getString(i);
Log.v(TAG, "Col: " + columnName);
Log.v(TAG, "Val: " + columnValue);
}
//}
Play around with it a little. It Should have all of the data you need (distinguish SMSs by timestamp)

Related

Retrieve SMS sent after a specific time

I have a background service that listens to changes to Android SMS content provider. I store the last recorded id of the message that my Service recorded and using it, I want to find the number of messages that were sent when the service was Stopped/Killed.
So when the service is started again and another message is sent, I want to use the timestamp of the last recorded message. I use the below code to do this. My problem is that the cursor always returns 1 record irrespective of how many messages are sent when the service was killed before its restarted.
String COLUMN_NAME_DATE = "date"
String[] projection = new String[]{COLUMN_NAME_ID, COLUMN_NAME_DATE};
Cursor newMessagesCursor = mContext.getContentResolver().query(
Uri.parse(SMSManager.SMS_URI_SENT),
projection,
COLUMN_NAME_DATE + "> ? ",
new String[]{String.valueOf(lastMessageTimeStamp)}, null);
int newMessagesCount = (null != newMessagesCursor) ? newMessagesCursor.getCount() : 0;
Any pointers would be helpful. Let me know if there is any alternative approach as well.
Edit: Adding code to retrieve the lastMessageTimeStamp.
Cursor messageCursor = mContext.getContentResolver().query(
Uri.parse(SMSManager.SMS_URI_SENT),
projection,
COLUMN_NAME_ID + "= ? ",
new String[]{ lastMessageId}, null);
if(null != messageCursor && messageCursor.moveToFirst()) {
Long lastMessageTimeStamp = Long.parseLong(messageCursor.getString(messageCursor.getColumnIndex(COLUMN_NAME_DATE)));
}
Complete Source code for this class is located at:
https://github.com/midhunhk/message-counter/blob/unicorn/messageCounter/src/main/java/com/ae/apps/messagecounter/observers/SMSObserver.java
I was able to solve this issue by using a different approach.
It seems like when we query the table SMSManager.SMS_URI_SENT as in the question, it is only returning 1 row which is the last message that was sent.
Cursor newMessagesCursor = context.getContentResolver().query(
Uri.parse(SMSManager.SMS_URI_ALL),
MessagesTableConstants.SMS_TABLE_PROJECTION,
"person is null and " + MessagesTableConstants.COLUMN_NAME_DATE + "> ? ",
new String[]{String.valueOf(lastMessageTimeStamp)}, null);
I query for SMSManager.SMS_URI_ALL and check for person column as null which represents messages that are sent.

retrieve actual sent time of an received sms in android

In the original SMS app of android i can check the details of a received sms and it wil show me the time and date it was sent by the sender and the time i received this message.
I am interested in the time difference between these 2
I can get the time i received the sms via this code. how can i extend this so that i also get the time it was sent?
Uri uri = Uri.parse("content://sms");
Cursor cursor = getContentResolver().query(uri, null, null, null, null);
if (cursor.moveToFirst()) {
for (int i = 0; i < cursor.getCount(); i++) {
String body = cursor.getString(cursor.getColumnIndexOrThrow("body"))
.toString();
String number = cursor.getString(cursor.getColumnIndexOrThrow("address"))
.toString();
String date = cursor.getString(cursor.getColumnIndexOrThrow("date"))
.toString();
Date smsDayTime = new Date(Long.valueOf(date));
String type = cursor.getString(cursor.getColumnIndexOrThrow("type"))
.toString();

How to find contents URI for SMS?

Is there a way to figure out a contents URI for SMS data for a specific android device?
For example, I was able to locate the DB containing all messages following this.
SMS DB is located at ./dbdata/databases/com.android.providers.telephony/mmssms.db
However, I want to know the contents URI rather than the actual location (e.g. contents://sms). Is this possible? If not, how do I gather information about stored SMS messages from different devices that have different and unknown content URIs for SMS?
For example, these are the contents URI for some vendors:
LG: content://com.lge.messageprovider/msg/inbox
SAMSUNG: content://com.sec.mms.provider/message
SAMSUNG Galaxy A: content://com.btb.sec.mms.provider/message
General: content://sms/inbox
Why don't you use uri parser of Contentresolver, especially that you know content string.
Try this (in connection with Cursor):
Cursor cursor = getContentResolver().query(Uri.parse("content://sms/inbox"), null, null, null, null);
cursor.moveTofirst();
//read all messages in your inbox
do
{
String message = "";
//read all data from all available columns for each message
for (int i = 0; i < cursor.getColumnCount(); i++)
{
message += " " + cursor.getComulnName(i) + ": " + cursor.getString(i);
//here do wathever you want with your message string
}
}
while(cursor.moveToNext());
cursor.close();
dont forget do add READ_SMS permission to your AndroidManifest.xml

How to get the number of already read messages in android?

In my app,i want to give the user info about the number of already read and unread messages from inbox.I have searched and found out that there is no such content://sms/read is present in android api.I have tried this code till now:
Uri number_of_sms_read = Uri.parse("content://sms/read");
Cursor c = ShowTheMessages.this.getContentResolver().query(number_of_sms_read, null,null, null, null);
c.moveToFirst();
NumberOfSmsReadInInbox = c.getCount();
Can you please tell me is it possible to get the count of read and unread messages in android?
Thanks in advance.
I suppose you can take a look at this question which is the reverse of yours: Get number of unread sms
It provides the WHERE condition "read = 0" to the query, so you might want to supply a "read = 1".
final Uri SMS_INBOX = Uri.parse("content://sms/inbox");
Cursor c = getContentResolver().query(SMS_INBOX, null, "read = 1", null, null);
int unreadMessagesCount = c.getCount();
c.deactivate();

Android - Querying the SMS ContentProvider?

I currently register a content observer on the following URI "content://sms/" to listen out for incoming and outgoing messages being sent.
This seems to work ok and I have also tried deleting from the sms database but I can only delete an entire thread from the following URI "content://sms/conversations/"
Here is the code I use for that
String url = "content://sms/";
Uri uri = Uri.parse(url);
getContentResolver().registerContentObserver(uri, true, new MyContentObserver(handler));
}
class MyContentObserver extends ContentObserver {
public MyContentObserver(Handler handler) {
super(handler);
}
#Override public boolean deliverSelfNotifications() {
return false;
}
#Override public void onChange(boolean arg0) {
super.onChange(arg0);
Log.v("SMS", "Notification on SMS observer");
Message msg = new Message();
msg.obj = "xxxxxxxxxx";
handler.sendMessage(msg);
Uri uriSMSURI = Uri.parse("content://sms/");
Cursor cur = getContentResolver().query(uriSMSURI, null, null,
null, null);
cur.moveToNext();
String protocol = cur.getString(cur.getColumnIndex("protocol"));
if(protocol == null){
Log.d("SMS", "SMS SEND");
int threadId = cur.getInt(cur.getColumnIndex("thread_id"));
Log.d("SMS", "SMS SEND ID = " + threadId);
Cursor c = getContentResolver().query(Uri.parse("content://sms/outbox/" + threadId), null, null,
null, null);
c.moveToNext();
int p = cur.getInt(cur.getColumnIndex("person"));
Log.d("SMS", "SMS SEND person= " + p);
//getContentResolver().delete(Uri.parse("content://sms/conversations/" + threadId), null, null);
}
else{
Log.d("SMS", "SMS RECIEVE");
int threadIdIn = cur.getInt(cur.getColumnIndex("thread_id"));
getContentResolver().delete(Uri.parse("content://sms/conversations/" + threadIdIn), null, null);
}
}
}
However I want to be able to get the recipricant and the message text from the SMS Content Provider, can anyone tell me how to do this?
And also how to delete one message instead of an entire thread?
This was already discussed.
To read SMS from the Content provider check:
- android-1-5-reading-sms-messages
Check this threads:
delete-sms-in-android-1-5
how-to-delete-sms-from-inbox-in-android-programmatically
can-we-delete-an-sms-in-android-before-it-reaches-the-inbox
About your comment saying that you are deleting a whole thread instead of a single sms:
Have you tried out this code?
The address column contains the telephone number of the sms sender.
Use cursor.getString(cursor.getColumnIndex("address")) to pull the telephone number as a String. Pop a cursor on content://sms and sort it in date descending order to get the most recent message. You will have to wait for the new message to enter the table otherwise you will pull information from the wrong message. In an incomingSMS broadcastReceiver use a while loop, in a thread, polling for the cursor.getCount() to change. Then after the while loop cursor.moveToFirst will be the new message.
For example:
Cursor cur = getContentResolver().query(Uri.parseUri(content://sms), null, null, null, null);
int count = cur.getCount();
while (cur.getCount() == count)
{
Thread.sleep(1000);
cur = getContentResolver().query(Uri.parseUri(content://sms), null, null, null, null);
}
Then get the address of the sms sender:
cur = getContentResolver().query(Uri.parseUri(content://sms), null, null, null, "date DESC");
cur.MoveToFirst();
String telephoneNumber = cur.getString(cur.getColumnIndex("address");
This while loop will pause the thread until the new message arrives. You can also use a contentObserver, but this while loop is simple and does not require registration, unregistration, and a separate class.
Frankly I think its faster to pull the address and the body of the message directly from the pdu of the incoming intent. This way you dont have to wait for the message to enter the table to get the address and the body. The Android class SmsMessage has a variety of useful methods.

Categories

Resources