0

I've got an app that sends a text message as a response after receiving a text message. (Auto Respond) When SMS is enabled in hangouts, my app wasn't sending its messages. I fixed that by doing this:

<intent-filter android:priority="500">
    <action android:name="android.provider.Telephony.SMS_RECEIVED"/>
</intent-filter>

After sending the message, my app also writes that sent message to the user's SMS log (inbox/outbox displayed by messaging apps.)

But now that my SMS receiver is higher priority than Hangouts, the sent message is written to the user's SMS log AFTER the received message when it should be the other way around. So it shows like this:

Response Message
Received Message - this is what triggered the response

But it should be:

Received Message - triggers response
Response Message

Is there a way for me to wait for the received message to be written before writing the response message? It works fine when SMS is disabled in Hangouts. But since Hangouts is now writing that message instead of the default SMS receiver, it messes things up like crazy.

EDIT: Thanks to Keith's response, this is the code that worked for me:

context.getContentResolver().registerContentObserver(
Uri.parse("content://sms"), 
true, 
smsObserver);

And this class:

private class SMSObserver extends ContentObserver
{
    public SMSObserver()
    {
        super(null);
    }

    @Override
    public boolean deliverSelfNotifications() {
        return true;
    }

    @Override
    public void onChange(boolean selfChange) {
        super.onChange(selfChange);
        if(!selfChange)
            //sendResponse
        context.getContentResolver().unregisterContentObserver(this);
    }

    @Override
    public void onChange(boolean selfChange, Uri uri) {
        super.onChange(selfChange, uri);
        if(!selfChange)
            //sendResponse
        context.getContentResolver().unregisterContentObserver(this);
    }
}

I'm not sure if the self change part is necessary, but it works so I'm not changing it.

Randy
  • 1,068
  • 2
  • 14
  • 32

1 Answers1

1

Try a ContentObserver on the SMS database to listen for when Hangouts writes to the SMS content provider. This approach should be compatible with 4.4/Hangouts as well as earlier versions; you'd just wait until something is written to write your sent message.

Tested two different versions on Android 4.3, Galaxy Nexus:

context.getContentResolver().registerContentObserver(Uri.parse("content://sms"), true, myContentObserver);

or

cursor = context.getContentResolver().query(Uri.parse("content://sms/inbox"),
            new String[] { SMS_ID, SMS_ADDRESS, SMS_READ },
            "read = 0",
            null,
            null);
cursor.registerContentObserver(myContentObserver);

But I couldn't use the non-Cursor version with sms/inbox for some reason. The downside of the Cursor-based version is that it seems to need to stay open so then you have to be sure to close it later.

Also, neither version is being called when the read status changes.

Keith
  • 580
  • 1
  • 4
  • 11
  • The first one worked. I had tried a similar solution earlier, after seeing your initial response about the ContentObserver, and that was actually the next thing that I was going to try. Thank you for your help! – Randy Dec 22 '13 at 04:03
  • If it's not working for you, note that mine has `priority="500"` in the manifest. This is necessary to have a higher priority than Hangouts. Otherwise the message never even gets to your app. – Randy Dec 22 '13 at 05:45
  • Yeah, I've got the priority set high. Not sure if it's helpful, but [GO SMS sets their priority to Integer.MAX_VALUE](http://stackoverflow.com/questions/18075549/sms-broadcastreceiver-not-called-when-go-sms-install) – Keith Dec 22 '13 at 21:10