In the following example, we use ContentObserver to monitor updates for the SMS provider. This observer is created and started before SMS Intent is launched, and verifies that the provider has changed the recipient address. The operation that creates the Observer must implement the SmsSendObserver.SmsSendListener interface to receive the callback.
The Observer constructor includes the timeout parameter (in milliseconds) to allow the Observer to be properly unregistered if the message is not sent after a reasonable time. Optionally, set the value to NO_TIMEOUT . However, the class, as it is written, is intended to be used "in one shot", and it will unregister and revoke the participants after the callback. The stop() method can be used to clear if no callback occurs. In any case, the instance is no longer used, and any reference to it must be null.
Example:
public class MainActivity extends Activity implements SmsSendObserver.SmsSendListener { ... private void sendMessage(String phoneNumber, String messageBody) {
SmsSendObserver Class:
public class SmsSendObserver extends ContentObserver { public static final int NO_TIMEOUT = -1; private static final Handler handler = new Handler(); private static final Uri uri = Uri.parse("content://sms/"); private static final String COLUMN_ADDRESS = "address"; private static final String COLUMN_TYPE = "type"; private static final String[] PROJECTION = { COLUMN_ADDRESS, COLUMN_TYPE }; private static final int MESSAGE_TYPE_SENT = 2; private Context context = null; private ContentResolver resolver = null; private String phoneNumber = null; private long timeout = NO_TIMEOUT; private boolean wasSent = false; private boolean timedOut = false; public SmsSendObserver(Context context, String phoneNumber, long timeout) { super(handler); if (context instanceof SmsSendListener) { this.context = context; this.resolver = context.getContentResolver(); this.phoneNumber = phoneNumber; this.timeout = timeout; } else { throw new IllegalArgumentException( "Context must implement SmsSendListener interface"); } } private Runnable runOut = new Runnable() { @Override public void run() { if (!wasSent) { timedOut = true; callBack(); } } }; public void start() { if (resolver != null) { resolver.registerContentObserver(uri, true, this); if (timeout > NO_TIMEOUT) { handler.postDelayed(runOut, timeout); } } else { throw new IllegalStateException( "Current SmsSendObserver instance is invalid"); } } public void stop() { if (resolver != null) { resolver.unregisterContentObserver(this); resolver = null; context = null; } } private void callBack() { ((SmsSendListener) context).onSmsSendEvent(wasSent); stop(); } @Override public void onChange(boolean selfChange) { if (wasSent || timedOut) return; Cursor cursor = null; try { cursor = resolver.query(uri, PROJECTION, null, null, null); if (cursor != null && cursor.moveToFirst()) { final String address = cursor.getString(cursor.getColumnIndex(COLUMN_ADDRESS)); final int type = cursor.getInt(cursor.getColumnIndex(COLUMN_TYPE)); if (PhoneNumberUtils.compare(address, phoneNumber) && type == MESSAGE_TYPE_SENT) { wasSent = true; callBack(); } } } finally { if (cursor != null) { cursor.close(); } } } public interface SmsSendListener {
source share