问题
If 3 unread SMS messages are present in native(phone), and user has read one of the SMS. How to identify which SMS got read(from native) in our application.
Currently I am using ContentObserver and onChange() method is called when user read SMS from native(phone).
I am able to get all unread SMS messages:
Cursor cursor = this.getContentResolver().query(Uri.parse("content://sms/"), null, "read = 0", null, null);
How to identify which SMS got read from native(phone). Any help or guidance will be well appreciated.
In Nexus 4(Android 4.3) - API 18), If notification(in device status bar) for two unread SMS(belongs two different sender) in Android Native Sms Client, user clicks on that notification, onChange(context, uri) is called in my aaplication, inside it I have tried to get id of the read SMS:
String id = uri.getLastPathSegment();
For above mentioned case, "inbox" as value of "id" came. But for reading any of the SMS by user from in Android Native Sms Client, onChange(context, uri) method is not invoked.
In Samsung Galaxy S3(Android 4.1.2 - API 16), onChange(context, uri) is not at all invoked in either of the case (i.e. if user read the SMS from Native or user clicks on the notification(in device status bar) for two unread SMS(belongs two different sender) in Android Native Sms Client.
(In API 16, when new SMS received in native, onChange(context, uri) is called and value of "id" is equal to the unique ID for that SMS - Tested on Nexus 4(Android 4.3))
Note: Here, "from native" means "from the device's default SMS client".
回答1:
To determine definitely which message has changed from unread to read, you will likely have to track the state of all unread messages in your own data structure and compare during each onChange detected by your Content Observer to determine which/if any messages changed read states. You may able to improve performance by including additional projection and selection arguments in the Content Resolver queries to narrow the data set to only the fields of interest.
Hopefully, the upcoming changes to the SMS APIs that Google is promising in Android 4.4 will make the use of hidden APIs and Content Providers unnecessary for these sorts of purposes moving forward.
You may also be able to determine the SMS that was read (or at least the thread) by looking for the markAsRead debug message that is generated by the com.android.mms.data.Conversation class used by the native Messaging app:
D/Mms (1645): [192] markAsRead: update read/seen for thread uri: content://mms-sms/conversations/4
However, that may not always be reliable and would not work on Android 4.1+ devices unless your app has system privileges due to the restrictions placed on the READ_LOGS permission in API Level 16.
来源:https://stackoverflow.com/questions/19333961/how-to-get-notify-which-sms-got-read-from-nativephone-if-multiple-unread-sms-a