0

I'm trying to make my own SMS app but I'm having trouble with fetching SMS. I'm testing the app on AVD and my SMS folder is as shown below.

Method responsible for fetching existing data:

public void refreshSmsInbox() {

        ContentResolver contentResolver = getContentResolver();
        Cursor smsInboxCursor = contentResolver.query(Uri.parse("content://sms/inbox"), null, null, null, null);
        int indexBody = smsInboxCursor.getColumnIndex("body");
        int indexAddress = smsInboxCursor.getColumnIndex("address");
        if (indexBody < 0 || !smsInboxCursor.moveToFirst()) return; //FAILING HERE

        do {
            ffrom.add(smsInboxCursor.getString(indexAddress));
            ccontent.add(smsInboxCursor.getString(indexBody));
            //String str = "SMS From: " + smsInboxCursor.getString(indexAddress)  + "\n" + smsInboxCursor.getString(indexBody) + "\n";
            //arrayAdapter.add(str);
        } while (smsInboxCursor.moveToNext());

The "//FAILING HERE" commented line is the culprit. I don't know what is the exact issue. I have no idea what to make of the condition checks. smsInboxCursor is not null, I checked that.

Please help me understand the reason why my code isn't working as expected.

SMS app in AVD:

enter image description here

SonOfEl
  • 175
  • 1
  • 10
  • Errors / stack traces are designed to explain what's going wrong. This in sharp contrast to Stack Overflow, which is designed to explain how to do things. You've used the wrong tool. Look at your stacktrace, don't ask here. If you're having a hard time understanding the exception and error you get, then by all means. Naturally, you'd have to include the entire error message, including the line numbers (include the code it points at, make sure SO readers know which line number in the stack trace matches with which line of code you pasted). – rzwitserloot Dec 18 '21 at 17:10
  • @rzwitserloot my stack trace included no error so I was unsure about how to debug it. But now I realized that mine was a logical error and the code worked as it should have without any error. I have self-answered the question. Cheers! – SonOfEl Dec 19 '21 at 10:51

1 Answers1

0

I was trying to access even sent SMS using "content://sms/inbox". That was my mistake.

"content://sms/" solved my problem - it fetches all SMS.

SonOfEl
  • 175
  • 1
  • 10