How to open up a specific SMS in android

前端 未结 4 570
死守一世寂寞
死守一世寂寞 2021-01-04 09:03

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

4条回答
  •  感情败类
    2021-01-04 09:35

    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;
    }
    

提交回复
热议问题