Question

I am working on a android application which read the sms from content provider. The application works fine and read sms fine from content provider. But sometime (very rare) the 'address' column returns null for the sms message.

Here is sample code What I am using:

String whereClause = "_id > " + String.valueOf(Database.getLastSmsId(this));
        Cursor cursor = getContentResolver().query(smsUri, null, whereClause, null, null);
        if(cursor.moveToFirst()) {
            do {
                int id = cursor.getInt(cursor.getColumnIndex("_id"));
            String protocol = cursor.getString(cursor.getColumnIndex("protocol"));
            String body = cursor.getString(cursor.getColumnIndex("body"));

            String address = cursor.getString(cursor.getColumnIndex("address")); // <----- Here is the problem
            // address returns as null string

            String date = cursor.getString(cursor.getColumnIndex("date"));

                Log.d(Constants.TAG, "SMS event received. address="+address);
            } while(cursor.moveToNext());
    }

I am getting this issue on Motorola Driod Android v2.3.5. Please advise.

Ali

Was it helpful?

Solution

I found the problem. The android OS save the message as draft in SMS content provider, if start writing and then close the SMS app (due to incoming call or any other reason). This generate a new entry in SMS database with null value in 'address' column.

Salman

OTHER TIPS

This is a normal behavior. Found in the SmsMessage documentation:

Returns the originating address (sender) of this SMS message in String form or null if unavailable

If your app breaks because of a NPE later, you should insert a null check and set the address to an empty string.

An advice: A question should asked something and you should provide a stack trace if you have an error.

This can happen in case of masked number.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top