retrieve actual sent time of an received sms in android - 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();

Related

Android-how to delete specific message from sent item or don't show sent message when using following code..?

this is my sample code for sending message i want to use not show this message in sent item.
String strPhone = "XXXXXXXXXXX";
String strMessage = "Lorem\nIpsum";
SmsManager sms = SmsManager.getDefault();
sms.sendTextMessage(strPhone, null, strMessage, null, null);
Toast.makeText(this, "Sent.", Toast.LENGTH_SHORT).show();
i want to delete this sended message from sent items or now show in sent item.
in all version.
please help me to solve out.
Query your conversations after sending the message. Then fetch that particular message by comparing the message contents using any unique keyword in your sent message.I have tried this code in my phone running Marshmallow.
Uri uriSMS = Uri.parse("content://sms/conversations");
Cursor cur = getContentResolver().query(uriSMS, null, null, null, null);
if(cur != null && cur.moveToNext()) {
do {
String smsContent = cur.getString(2);
if(smsContent.contains("your sms keyword")) {
String pid = cur.getString(0); // Get id;
String uri = "content://sms/" + pid;
getContentResolver().delete(Uri.parse(uri), null, null);
}
} while (cur.moveToNext());
}

Reading last sms from a particular sender

Im building a app that is reading last sms from spec number (vb# 8888). Code is working great. I only have one prob. When i get a new sms from other number (vb# 7777)my code stops reading sms from (v#8888). if i delete the new sms from (7777) than my code strats working again. I'm using this to update a string in my app. Can someone help me
This is my code
Uri mSmsinboxQueryUri = Uri.parse("content://sms/inbox");
Cursor cursor1 = getContentResolver().query(mSmsinboxQueryUri, null, null, null, null);
String[] columns = new String[]{"address", "body"};
if(cursor1.moveToFirst()) {
String address = cursor1.getString(cursor1.getColumnIndex(columns[0]));
if address.equalsIgnoreCase("+597*******")) {
body = cursor1.getString(cursor1.getColumnIndex(columns[3]));
Koers = (TextView) findViewById(R.id.Koersdiedag);
Koers.setText(body);//body
Your code is just reading the most recent message in the inbox, whomever it's from. If you want the most recent message from that particular number, you can adjust your query to match the number, and limit the results to one record.
Uri mSmsinboxQueryUri = Uri.parse("content://sms/inbox");
String[] projection = {"address", "body"};
String phoneNumber = "+597*******";
Cursor cursor1 = getContentResolver().query(mSmsinboxQueryUri,
projection,
"address = ?",
new String[] {phoneNumber},
"date DESC LIMIT 1");
if (cursor1 != null && cursor1.moveToFirst()) {
body = cursor1.getString(cursor1.getColumnIndex("body"));
Koers = (TextView) findViewById(R.id.Koersdiedag);
Koers.setText(body);
}

what is the sms drafts box uri in android?

i want read all sms in draft box of android i write an app that can read all sms in inbox sent box
but i dont know uri path of draft box in android
i should replace uri path of draft box with inbox to read all sms that are in drafts
but what is it?
sms objSms = new sms();
Uri message = Uri.parse("content://sms/sent");
ContentResolver cr = context.getContentResolver();
Cursor c = cr.query(message, null, null, null, null);
int totalSMS = c.getCount();
Log.i("READDDDDDDDD INBOX",totalSMS+"***************************" );
if (c.moveToFirst()) {
for (int i = 0; i < totalSMS; i++) {
objSms = new sms();
objSms.setPhone(c.getString(c
.getColumnIndexOrThrow("address")));
objSms.setMessage(c.getString(c.getColumnIndexOrThrow("body")));
objSms.setDate_time(c.getString(c.getColumnIndexOrThrow("date")));
ars.add(objSms);
c.moveToNext();
}
}
c.close();
return ars;
SMS draft URI is
content://sms/drafts/
Uri message = Uri.parse("content://sms/drafts/");
The whole list of SMS uris are:
Inbox = "content://sms/inbox"
Failed = "content://sms/failed"
Queued = "content://sms/queued"
Sent = "content://sms/sent"
Draft = "content://sms/draft"
Outbox = "content://sms/outbox"
Undelivered = "content://sms/undelivered"
All = "content://sms/all"
Conversations = "content://sms/conversations"
"content://mms-sms/conversations"
Some are documented and rest are not/
public static final Uri CONTENT_URI =
Uri.parse("content://sms/draft");
for more help check this
http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/4.2.2_r1/android/provider/Telephony.java

Getting phone number of each sms via content://sms/

I've wrote the following code, to get the whole conversation between the user and a number:
Uri SMS_INBOX = Uri.parse("content://sms/");
String selection = "thread_id = " + thread_id;
final String[] projection = new String[] { "*" };
Cursor c = getContentResolver().query(SMS_INBOX, projection, selection,null, "date");
startManagingCursor(c);
String[] body = new String[c.getCount()];
String[] address = new String[c.getCount()];
if (c.moveToFirst()) {
for (int j = 0; j < c.getColumnCount(); j++)
Log.w("ColumnName", c.getColumnName(j));
for (int i = 0; i < c.getCount(); i++) {
body[i] = c.getString(c.getColumnIndexOrThrow("body")).toString();
address[i] = c.getString(c.getColumnIndexOrThrow("address")).toString();
Log.d("address-" + i, address[i]);
Log.d("body-" + i, body[i]);
String subject = c.getString(c.getColumnIndexOrThrow("_id")).toString();
Log.d("_id-" + i, subject);
String thread = c.getString(c.getColumnIndexOrThrow("thread_id")).toString();
Log.d("thread_id-" + i, subject);
Log.d("----", "----");
c.moveToNext();
}
}
Via this code, i get all the messages in a conversation. The problem is, I can't figure out which number is sending which message. If i get the column "address" it returns the same number all the time (actually it returns the other person's number only), so I can't keep record of whether the message I just got through this code was sent by the user or the other number.
The column will always gives second persons number only.If you want to differentiate sent message and received message you have to use column 'type'.
body[i] = c.getString(c.getColumnIndexOrThrow("body")).toString();
if(c.getString(c.getColumnIndex("type")).equalsIgnoreCase("1")){
// sms received
msg_state[i]=1;
}
else {
//sms sent
msg_state[i]=0;
}
No You can easily identify the sent sms and received sms.

Reading text from SMS, and show it as text view

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)

Categories

Resources