androidsmsandroid-cursor

Get SMS Details From its ID Android


I want to get Details of SMS(Number, Text Body, Time of coming); And i only know the id of sms. Can i query to "content://sms" with this id and get details?

At the moment i can make a loop and query for every message and get details. But that is not efficient when you have to get single sms details 10 times from 1000 sms.....


Solution

  • I figured it out myself:

    Uri myMessage = Uri.parse("content://sms/");
    ContentResolver cr = getContentResolver();
    Cursor c = cr.query(myMessage, new String[] { "_id", "address", "date", "body","read" },"_id = "+smsID, null, null);
    
    c.moveToFirst();
    
    String Number = c.getString(c.getColumnIndexOrThrow("address")).toString();
    
    String ReadStatus = c.getString(c.getColumnIndex("read"));
    String Body = c.getString(c.getColumnIndexOrThrow("body")).toString();
                
    c.close();
    

    I was missing the condition and moving the cursor to first.