Search code examples
javaandroidsqlsmsandroid-appwidget

android AppWidget: SMS query works on emulator, but not on a real device


I have built an appWidget which update it's unread sms count for specific contact when a new sms comes in, for that I've registered a contentObserver to monitor incoming sms.

The mySMSObserver class calls the method below for getting the number of unread messages for specific contact, by his ID.

So far so good, the problem is that the query below works fine on the emulator, but when I try that on my android device it allways return 0 unread messages (as shown by Toast).

private static String updateUnreadSMS(String contactID, Context context) {

ContentResolver cr=context.getContentResolver();
//final Uri SMS_INBOX=(Uri.parse("content://mms-sms/conversations"));
//final Uri SMS_INBOX=(Uri.parse("content://mms/inbox"));
final Uri SMS_INBOX=Uri.parse("content://sms/inbox");

Cursor c = cr.query(SMS_INBOX, null, "read = 0 AND person=?", new String[]{contactID}, null);

// get number of unread sms messages
int unreadMessagesCount = c.getCount();

Log.d("DEBUG", "UnreadSMS: "+unreadMessagesCount);
Toast.makeText(context, "updateUnreadSMS "+unreadMessagesCount, Toast.LENGTH_LONG).show();

c.deactivate();
return String.valueOf(unreadMessagesCount); 

}

Is there different query's needed for different devices?

how do I write the same query for "content://mms/inbox"?, Because "person=?" is an illegal field for that query.

Will be glad for your help and advice for solving this problem :)


Solution

  • Accessing the databases directly might not be the best idea. As locations and data structure might change with different Android versions (It did so with the Calendar storage from 7 => 8). Nevertheless check if your device location and database structure is the same.

    For MMS you might take a peek at PduParser used to parse MMS messages and other classes in this folder.