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

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.

Related

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();

Android/Database. Any idea why disappears from phone number the first character, the "+"?

I used Log, when i get contacts number and there is everything right, until I read them from database and puts in a list, then I loose the "+"character from numbers.
public void getNumbers(String box) {
Uri uri = Uri.parse("content://sms/" + box);
Cursor c = getContentResolver().query(uri, null, null, null, null);
startManagingCursor(c);
// Read the sms data and store it in the list
if (c.moveToFirst()) {
for (int i = 0; i < c.getCount(); i++) {
SMSData sms = new SMSData();
sms.setNumber(c.getString(c.getColumnIndexOrThrow("address"))
.toString());
String nr = sms.getNumber();
Log.d("I got this:", nr);
db.getSMSDataBySearch(nr);
c.moveToNext();
}
}
c.close();
}
And the SQLite code:
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery("INSERT INTO version3 (number) SELECT "+value+" WHERE NOT EXISTS "
+ "(SELECT 1 FROM version3 WHERE number = "+value+")", null);
The SMS content provider does not store the formatting, only the numeric data.
If you want it formatted, there are tools or you can build it:
Java phone number format API

Using new Telephony content provider to read SMS

According to the 4.4 SMS APIs, the new version provides functionality to:
allow apps to read and write SMS and MMS messages on the device
I can't find any information on this functionality, nor any samples in the new SDK. This is what I have so far for reading new incoming messages.
However, I would like to read existing messages stored on the deivce:
// Can I only listen for incoming SMS, or can I read existing stored SMS?
SmsMessage[] smsList = Telephony.Sms.Intents.getMessagesFromIntent(intent);
for(SmsMessage sms : smsList) {
Log.d("Test", sms.getMessageBody())
}
Please note: I know about using the SMS Content Provider, however this method is unsupported. According to the linked APIs, I should be able to do this in a supported way.
It looks like you would be able to use this class to get it working. The package is Telephony.Sms.Conversations.
Although the following code uses the content provider method, this is now an official API added in API Level 19 (KitKat) for reading the SMS messages.
public List<String> getAllSmsFromProvider() {
List<String> lstSms = new ArrayList<String>();
ContentResolver cr = mActivity.getContentResolver();
Cursor c = cr.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++) {
lstSms.add(c.getString(0));
c.moveToNext();
}
} else {
throw new RuntimeException("You have no SMS in Inbox");
}
c.close();
return lstSms;
}
I did it like the following. Create SMSObject:
public class SMSObject {
private String _id;
private String _address;
private String _msg;
private String _readState; // "0" for have not read sms and "1" for have
// read sms
private String _time;
private String _folderName;
//+ getter and setter methods and
#Override
public String toString() {
return "SMSObject [_id=" + _id + ", _address=" + _address + ", _msg="
+ _msg + ", _readState=" + _readState + ", _time=" + _time
+ ", _folderName=" + _folderName + "]";
}
And here a simple function, which simply logs all current SMS-Objects
private void readSMS() {
List<SMSObject> lstSms = new ArrayList<SMSObject>();
SMSObject objSms = new SMSObject();
Uri message = Uri.parse("content://sms/");
ContentResolver cr = this.getContentResolver();
Cursor c = cr.query(message, null, null, null, null);
// this.startManagingCursor(c);
int totalSMS = c.getCount();
Log.d("SMS Count->", "" + totalSMS);
if (c.moveToFirst()) {
for (int i = 0; i < totalSMS; i++) {
objSms = new SMSObject();
objSms.setId(c.getString(c.getColumnIndexOrThrow("_id")));
objSms.setAddress(c.getString(c
.getColumnIndexOrThrow("address")));
objSms.setMsg(c.getString(c.getColumnIndexOrThrow("body")));
objSms.setReadState(c.getString(c.getColumnIndex("read")));
objSms.setTime(c.getString(c.getColumnIndexOrThrow("date")));
if (c.getString(c.getColumnIndexOrThrow("type")).contains("1")) {
objSms.setFolderName("inbox");
} else {
objSms.setFolderName("sent");
}
lstSms.add(objSms);
Log.d("SMS at " + i, objSms.toString());
c.moveToNext();
}
}
// else {
// throw new RuntimeException("You have no SMS");
// }
c.close();
// return lstSms;
}
I found this some days ago, can't remember from what site;
You can only restore messages if the user has chosen to make the app the default sms app. This may or may not answer your question fully. I haven't tried this yet
Query the current default SMS app's package name and save it.
String defaultSmsApp = Telephony.Sms.getDefaultSmsPackage(context);
Request the user change the default SMS app to your app in order to restore SMS messages (you must be the default SMS app in order to write to the SMS Provider).
Intent intent = new Intent(context, Sms.Intents.ACTION_CHANGE_DEFAULT);
intent.putExtra(Sms.Intents.EXTRA_PACKAGE_NAME, context.getPackageName());
startActivity(intent);

Android getting sms conversation with name or address

I'm developing an SMS program and I want to get conversations.
I wrote the code below and it works fine, but I wonder if it could be more efficient
This is for geting conversation threads
Uri SMS_INBOX = Uri.parse("content://sms/conversations/");
Cursor c = getContentResolver().query(SMS_INBOX, null, null, null, "date desc");
startManagingCursor(c);
String[] count = new String[c.getCount()];
String[] snippet = new String[c.getCount()];
String[] thread_id = new String[c.getCount()];
c.moveToFirst();
for (int i = 0; i < c.getCount(); i++) {
count[i] = c.getString(c.getColumnIndexOrThrow("msg_count"))
.toString();
thread_id[i] = c.getString(c.getColumnIndexOrThrow("thread_id"))
.toString();
snippet[i] = c.getString(c.getColumnIndexOrThrow("snippet"))
.toString();
//Toast.makeText(getApplicationContext(), count[i] + " - " + thread_id[i]+" - "+snippet[i] , Toast.LENGTH_LONG).show();
c.moveToNext();
}
c.close();
for getting addresses according to conversation thread
for(int ad = 0; ad < thread_id.length ; ad++)
{
Uri uri = Uri.parse("content://sms/inbox");
String where = "thread_id="+thread_id[ad];
Cursor mycursor= getContentResolver().query(uri, null, where ,null,null);
startManagingCursor(mycursor);
String[] number = new String[mycursor.getCount()];
if(mycursor.moveToFirst()){
for(int i=0;i<mycursor.getCount();i++){
number[i]=mycursor.getString(mycursor.getColumnIndexOrThrow("address")).toString();
mycursor.moveToNext();
}
}
mycursor.close();
and finally checking the adresses (if in contact list) and adding to a list
for(int i =0 ; i < numaralar.length ;i++)
{
String a = numaralar[i].substring(0,1);
if(!a.equals("+")){ kisiismi = numaralar[i]; }
ContentResolver localContentResolver = getApplicationContext().getContentResolver();
Cursor contactLookupCursor =
localContentResolver.query(
Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI,
Uri.encode(numaralar[i])),
new String[] {PhoneLookup.DISPLAY_NAME, PhoneLookup._ID},
null,
null,
null);
try {
while(contactLookupCursor.moveToNext()){
String contactName = contactLookupCursor.getString(contactLookupCursor.getColumnIndexOrThrow(PhoneLookup.DISPLAY_NAME));
kisiismi = contactName;
}
}catch (Exception e) {
kisiismi = numaralar[i].toString();
}
finally {
//Toast.makeText(getApplicationContext(), ad + kisiismi + " " + count[ad], Toast.LENGTH_LONG).show();
myArr.add(kisiismi);
contactLookupCursor.close();
}
}
Are there any way to make this process easier?
Since it is a simple SQLite query and the Contact provider can be accessed in a similar way, you should try to get the job done in SQL by GROUPING via number, timestamp and maybe thrad_id and then matching the results to a query to the contact provider (also via SQLite)
The documentation holds a pretty good description of all available columns.
https://developer.android.com/reference/android/provider/ContactsContract.PhoneLookupColumns.html
From KitKat onwards, we have a specific Uri for this: content://mms-sms/messages/byphone. But before that, this is what we can come up with:
Set<Integer> conversationIds = new HashSet<Integer>();
String numbers = "+xxxxxxxxxx,+yyyyyyyyyy";
final String[] PROJECTION = { Sms._ID, Sms.THREAD_ID };
final String SELECTION = Sms.ADDRESS + " IN (" + numbers.replaceAll("[^,]+", "?") + ")";
final String[] selectionArgs = numbers.split(",");
Cursor cursor = context.getContentResolver().query(Sms.CONTENT_URI, PROJECTION, SELECTION, selectionArgs, null);
int threadColumn = cursor.getColumnIndexOrThrow(Sms.THREAD_ID);
while (cursor.moveToNext())
conversationIds.add(cursor.getInt(threadColumn));
cursor.close();
return conversationIds;
There is no easy way to do the same with MMS messages because they don't keep their address in the database, you have to query each and every one separately. If you need to do it repeatedly, a viable solution is to cache MMS-to-phone number relations in a database of your own.
You can then use the identifiers accumulated in conversationIds to query the individual conversations. Note that if you want to merge different coversations belonging to the same contact, you can reuse the same query selection pattern above with passing in all ids as IN (?,...,?) at once.

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
}

Categories

Resources