13

Is there a way to open up the Messaging Activity on android with a specific SMS?

Janusz
  • 187,060
  • 113
  • 301
  • 369
n179911
  • 19,547
  • 46
  • 120
  • 162

4 Answers4

9

threadId should be the id of the SMS/MMS thread you want to view

Intent defineIntent = new Intent(Intent.ACTION_VIEW); 
defineIntent.setData(Uri.parse("content://mms-sms/conversations/"+threadId));  
myActivity.startActivity(defineIntent);

This is the simplest way I found

Valay
  • 106
  • 1
  • 2
  • Try looking at the findThreadIdFromAddress() method here: http://code.google.com/p/android-smspopup/source/browse/trunk/SMSPopup/src/net/everythingandroid/smspopup/SmsPopupUtils.java – pm_labs Sep 17 '11 at 12:38
  • Try this updated link: https://code.google.com/p/android-smspopup/source/browse/SMSPopup/src/main/java/net/everythingandroid/smspopup/util/SmsPopupUtils.java – pm_labs Mar 03 '14 at 05:58
  • 1
    This method doesn't work in 2016 with the latest SDK – Oleksii K. Nov 01 '16 at 13:03
4

Try this

int req_thread_id;

Uri mSmsinboxQueryUri = Uri.parse("content://sms"));
Cursor cursor1 = getContentResolver().query(
                        mSmsinboxQueryUri,
                        new String[] { "_id", "thread_id", "address", "person", "date",
                                "body", "type" }, null, null, null);

startManagingCursor(cursor1);
if (cursor1.getCount() > 0)
{
while (cursor1.moveToNext())
{

int thread_id = cursor1.getInt(1);
String address; = cursor1.getString(cursor1
                            .getColumnIndex(columns[0]));
if("your desired no".equals(address)
 req_thread_id = thread_id;
}
}
Intent defineIntent = new Intent(Intent.ACTION_VIEW); 
defineIntent.setData(Uri.parse("content://mms-sms/conversations/"+req_thread_id));  
myActivity.startActivity(defineIntent);
Vibhuti
  • 702
  • 1
  • 7
  • 21
2

I dug this out of the source for the Messaging app (lines 311-315), so I'm pretty sure it'll work, but I don't have any experience with it.

// threadId should be the id of the sms/mms thread you want to view
long threadId = 0; 
Intent i = new Intent("com.android.mms");
i.setData(
        Uri.withAppendedPath(
                i.getData(), Long.toString(threadId)
        )
);
i.setAction(Intent.ACTION_VIEW);
Jeremy Logan
  • 47,151
  • 38
  • 123
  • 143
  • I think 'thread id' is different from 'sms id'? different sms from a same person (each has it own id) can have same thread id. – n179911 Sep 14 '09 at 17:48
0

This snippet is from a comment in the accepted answer. Posting the method here for posterity.

public static long findThreadIdFromAddress(Context context, String address) {
    if (address == null)
        return 0;

    String THREAD_RECIPIENT_QUERY = "recipient";

    Uri.Builder uriBuilder = THREAD_ID_CONTENT_URI.buildUpon();
    uriBuilder.appendQueryParameter(THREAD_RECIPIENT_QUERY, address);

    long threadId = 0;

    Cursor cursor = null;
    try {

        cursor = context.getContentResolver().query(
                uriBuilder.build(),
                new String[] { Contacts._ID },
                null, null, null);

        if (cursor != null && cursor.moveToFirst()) {
            threadId = cursor.getLong(0);
        }
    } finally {
        if (cursor != null) {
            cursor.close();
        }
    }
    return threadId;
}
pm_labs
  • 1,135
  • 12
  • 22