简体   繁体   中英

How to resolve fetching SMS fail due to cursor index issues?

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:

在此处输入图像描述

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM