2

I am fed up of this issue, don't know actually where is problem, Below is my code :

private void readSMS() throws IOException {
    // TODO Auto-generated method stub
    Log.d("Read SMS","Called");
    ContentResolver cr = context.getContentResolver();
    Uri uri = Uri.parse("content://sms/inbox");
    StringBuilder smsBackup = new StringBuilder(); 
    Cursor messagesCursor = cr.query(uri, new String[] { "_id","address","body","person"}, null,null, null);
    smsBackup.append("SMS Back UP (Total Message(s)::"+messagesCursor.getCount()+") \n\n");

    String name = null;
    if(messagesCursor.getCount() > 0){
        while(messagesCursor.moveToNext()){
            name = null;
            name = getName(messagesCursor.getString(messagesCursor.getColumnIndex("address")));

            if(name==null)
                name = "Sender : " + messagesCursor.getString(messagesCursor.getColumnIndex("address"));

            smsBackup.append("Sender : "+name +"\n"+  "Message : "+messagesCursor.getString(messagesCursor.getColumnIndex("body")) + "\n\n");
        }
    }

    Log.d("InSMS Lenght","::"+smsBackup.toString().length());
}

here is log cat message : W/CursorWrapperInner(8375): Cursor finalized without prior close()

Caution Continues
  • 743
  • 2
  • 10
  • 25

2 Answers2

0

You should close the Cursor object once you're done working with it.

Jong
  • 9,045
  • 3
  • 34
  • 66
  • You mean above the Log.d("InSMS Lenght","::"+smsBackup.toString().length()); I have done that also forgot print here, still thats not working dear. – Caution Continues Mar 01 '13 at 05:19
0

This may be old but here is the reason why.

Don't throw the exception here:

readSMS() throws IOException

instead wrap in a a try catch block like so:

try{
    ContentResolver cr = context.getContentResolver();
    Uri uri = Uri.parse("content://sms/inbox");
    StringBuilder smsBackup = new StringBuilder(); 
    Cursor messagesCursor = cr.query(uri, new String[] { "_id","address","body","person"}, null,null, null);
    smsBackup.append("SMS Back UP (Total Message(s)::"+messagesCursor.getCount()+") \n\n");

    String name = null;
    if(messagesCursor.getCount() > 0){
        while(messagesCursor.moveToNext()){
            name = null;
            name = getName(messagesCursor.getString(messagesCursor.getColumnIndex("address")));

            if(name==null)
                name = "Sender : " + messagesCursor.getString(messagesCursor.getColumnIndex("address"));

            smsBackup.append("Sender : "+name +"\n"+  "Message : "+messagesCursor.getString(messagesCursor.getColumnIndex("body")) + "\n\n");
        }
    }
 messagesCursor.close();
}catch(IOException e){
//handle here, if not log it
}finally{
//can also close here if you want, need to wrap in another try block and check for null
}

Seems like the problem is the ioexception is being caught before the close() could be called. The method is then thrown at that point and is never called. I could be wrong, I just took a quick glance at it. I hope this helps.

wseme
  • 805
  • 6
  • 14