简体   繁体   English

SMS广播接收器在KitKat中不起作用

[英]SMS Broadcast Receiver not working in KitKat

My app needs to receive SMS. 我的应用程序需要接收短信。 So far it seems to work on real devices and emulators from Gingerbread to JellyBean. 到目前为止,它似乎可以在从Gingerbread到JellyBean的 真实设备 和仿真器上运行。 I'm now testing it in a KitKat emulator, and the Broadcast Receiver is not fired. 我现在正在KitKat模拟器中对其进行测试,并且不会触发Broadcast Receiver。

I'm registering a the receiver in the manifest using the classic sms action: 我正在使用经典的短信操作在清单中注册一个接收器:

    <receiver android:name=".receivers.MyReceiver">
        <intent-filter>
            <action android:name="android.provider.Telephony.SMS_RECEIVED" />
        </intent-filter>
    </receiver>

I've also added the SMS permission in the manifest: 我还在清单中添加了SMS许可:

<uses-permission android:name="android.permission.RECEIVE_SMS"/>

I've read the blog post on SMS changes in KitKat, where they say this intent filter should still work: 我已经阅读了有关KitKat中SMS更改的博客文章 ,他们说这个Intent过滤器应该仍然有效:

Any filters for the SMS_RECEIVED_ACTION broadcast in existing apps will continue to work the same on Android 4.4, but only as an observer of new messages, because unless your app also receives the SMS_DELIVER_ACTION broadcast, you cannot write to the SMS Provider on Android 4.4. 现有应用中广播的SMS_RECEIVED_ACTION的任何过滤器在Android 4.4上将继续起作用,但仅作为新消息的观察者,因为除非您的应用还接收到SMS_DELIVER_ACTION广播,否则您将无法在Android 4.4上写入SMS Provider。

Which is fine because I just need to listen for new messages, and I don't need my app to be the default SMS app nor modify the provider (This is a very simple app with no activities, only a receiver and a service that is called from the receiver). 很好,因为我只需要侦听新消息,并且不需要我的应用程序成为默认的SMS应用程序,也不需要修改提供程序(这是一个非常简单的应用程序,没有任何活动,只有一个接收者,并且一个服务从接收方呼叫)。

The emulator is very laggy, so I think it might be a problem with the emulator only. 该仿真器很慢,所以我认为这可能只是仿真器的问题。 Too bad I don't have any KitKat device to test it. 太糟糕了,我没有任何KitKat设备可以对其进行测试。 For now I'm testing in KitKat using the DDMS telephony tab. 现在,我正在使用DDMS电话选项卡在KitKat中进行测试。 I've added logging to the receiver but I don't see anything in logcat. 我已将日志记录添加到接收器,但在logcat中什么都看不到。 There are no exceptions nor any other message that could indicate problems. 没有例外,也没有任何其他消息可以表明问题。 It looks the receiver is not being notified, but on the other hand I've double-checked and the app is installed. 看起来接收器没有收到通知,但是另一方面,我已经仔细检查并安装了该应用程序。

What could be going wrong? 可能出什么问题了?

As @Seshu Vinai has pointed in the comments, the problem turns out to be the security restriction introduced in Android 3.1 . 正如@Seshu Vinai在评论中指出的那样,问题原来是Android 3.1中引入安全限制 I thought it was not the case since i had tested it before on JellyBean devices and emulators and the app seemed to work. 我认为情况并非如此,因为我之前已经在JellyBean设备和仿真器上对其进行过测试,并且该应用程序似乎可以运行。 But it did so only because I had previously launched an activity fron some unit tests, and thus the app was activated. 但这之所以如此,是因为我之前已经通过一些单元测试启动了一项活动,因此该应用程序已被激活。

You can find more info in the following questions: 您可以在以下问题中找到更多信息:

How to start a Service when .apk is Installed for the first time 首次安装.apk时如何启动服务
Android BroadcastReceiver won't work Android BroadcastReceiver无法正常工作
Android BroadcastReceiver not working after install 安装后,Android BroadcastReceiver无法正常工作

The broadcast receiver will not be called unless your app is the default app in KITKAT. 除非您的应用程序是KITKAT中的默认应用程序,否则不会调用广播接收器。 You will be able get the contents directly, but you will not be able to listen to new messages. 您将可以直接获取内容,但是将无法收听新消息。 I had the same problem. 我有同样的问题。 I got to know after making my App as default. 将我的应用程序设置为默认值后,我才知道。

Try making your app the default. 尝试将您的应用设置为默认应用。 You will notice the difference. 您会注意到差异。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM