Enabling SMS support in Hangouts 2.0 breaks the BroadcastReceiver of SMS_RECEIVED in my app

不问归期 提交于 2019-11-27 13:31:57
Flow

Fixed it.

The first problem was that, as you can see in revision 2 of my question, I put the priority attribute within the action element, when it actually belongs into the intent-filter element. So the priority didn't work.

While still targeting API 19, I did some experiences with Hangouts SMS enabled and different priorities.

  • No priority set → BroadcastReceiver doesn't receive SMS_RECEIVED intent
  • Priority 500 → BroadcastReceiver does receive SMS_RECEIVED intent
  • Priority 9991 → BroadcastReceiver does receive SMS_RECEIVED intent

So it seems that you need to have a minimum value for priority in order to get the intent with Hangouts SMS enabled. I didn't bother to bisect the lowest possible value. ;) I am going with 999 as I don't see any reason to get lower because my app does just some quick checks on the received sms and does not further process it. But it should really make a difference, as the broadcast is non-abortable.

1The maximum value

According to the most recent Google Hangouts Manifest, they have an AbortSmsReceiver set with a priority of "3" - so it seems that any app that wants to receive the SMS_RECEIVED broadcast on API 18 or lower should use a priority higher than 3:

<receiver android:name="com.google.android.apps.babel.sms.AbortSmsReceiver" android:permission="android.permission.BROADCAST_SMS" android:enabled="false">
    <intent-filter android:priority="3">
        <action android:name="android.provider.Telephony.SMS_RECEIVED" />
    </intent-filter>
</receiver>

You can use a build target of API 19. Apps on a device running API 19 (KitKat) will not be able to abort the broadcast as in prior API's. This prevents apps from performing a premature abort.

I assume they included this abort to prevent stock messaging apps from posting duplicate notifications. Stock messaging apps should process at priority 0 prior to KitKat - but any application that does not set a priority is processed at 0 also. IntentFilter objects are created with a default priority value of "0":

public IntentFilter() {
        mPriority = 0;
        mActions = new ArrayList<String>();
}
Ricardo

I can still get the broadcast fine. Just installed the new hangouts and enabled SMS. In my case, I am just reading the SMS contents and that continues to work. What I have done, though, was to set the priority of the intent-filter to 999 following a previous thread here:

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

Maybe that is playing a role?

UPDATE: Just read that you changed your target to SDK level 19. In that case, I read that you have to change how your app behaves (can't find the link now) following API 19 guidelines. Change your target back to 18 and it should work just fine.

I'm having the same issue. I'm targeting sdk 17, and I'm still unable to get the broadcast if Hangouts is enabled to handle SMS. Who knows how many applications Hangouts just broke in the market.

I'll try tweaking the priority and see if that helps.

[edit] Yup, the priority fixed it for me on target sdk 17. thanks!

When you register receiver, set priority of filter to INTEGER.MAX_VALUE. Now abortBroadcast() will work;

receiver = new HightPrioritySmsReceiver();
IntentFilter filter = new IntentFilter("android.provider.Telephony.SMS_RECEIVED");
filter.setPriority(Integer.MAX_VALUE);
registerReceiver(receiver, filter);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!