I searched the forums a lot to determine how many SMS messages I have that have not been read. The code seems to work well for received messages, but as soon as I actually read the message, onChange ContentObserver is not called. Thus, with an incoming SMS message, onChange is dialed, and I get the correct number, but after reading the message it is not called . The Samsung Note device is running Android 2.3.6. Any help would be appreciated.
public class UnreadSMSContentObserver extends ContentObserver { private static final String myTAG="PhoneInfoSMS"; private static final Uri SMS_INBOX = Uri.parse("content://sms/inbox"); private ContentResolver mContentResolver; private Handler mHandler; public int unreadSMS; private static final String TAG = "PhoneInfoSMS"; public UnreadSMSContentObserver(ContentResolver cr, Handler h) { super(null); mContentResolver = cr; mHandler = h; } public int getUnreadSMS() { if (mContentResolver != null) { try { Cursor c = mContentResolver.query(SMS_INBOX, null, "read = 0", null, null); if (c != null) { unreadSMS = c.getCount(); c.deactivate(); Log.d(TAG, unreadSMS + " unread SMS messages"); } } catch (Exception ex) { Log.e("ERROR: " + ex.toString(), ""); } } return unreadSMS; } @Override public void onChange(boolean selfChange) { Log.d(myTAG, "onChange"); if (mContentResolver != null) { getUnreadSMS(); mHandler.obtainMessage(PhoneInfoServer.CONTENTO_INFOCHANGED, PhoneInfoServer.CONTENTO_US, unreadSMS).sendToTarget(); Log.d(myTAG, "done"); } } }
In the manifest file, I have the following permissions:
"android.permission.READ_CONTACTS" "android.permission.READ_SMS" "android.permission.GET_ACCOUNTS" "android.permission.BATTERY_STATS" "android.permission.BLUETOOTH" "android.permission.BLUETOOTH_ADMIN" "android.permission.BROADCAST_SMS" "android.permission.RECEIVE_SMS" "android.permission.BROADCAST_SMS" "android.permission.RECEIVE_MMS"
How I set it for the calling service: in the onCreate of the service, I do:
mContentResolver = this.getContentResolver(); mUnreadSMSContentObserver = new UnreadSMSContentObserver(mContentResolver, mContentObserversHandler);
in the onStartCommand service that I am running:
mContentResolver.registerContentObserver(Uri.parse("content://sms/"), true, mUnreadSMSContentObserver); mContentResolver.registerContentObserver(Uri.parse("content://sms/inbox/"), true, mUnreadSMSContentObserver);
I tried only the content: // sms / and only the content: // sms / inbox / and both ... did not solve the problem.
PS A similar method for missed calls works great!
The app + service transmits battery information, missed calls and unread SMS messages via Bluetooth to the Arduino device, which displays it on the LED screen. What for? Two reasons: firstly, this is my first Android program made for fun. Secondly, I do not carry the phone with me at home all the time, and this is usually in my home office. If I didn’t hear him calling, I can still see the LED display telling me that I called or sent SMS when passing through.