【问题标题】:SMS Broadcast Receiver not working when application is not running应用程序未运行时,SMS 广播接收器不工作
【发布时间】:2016-01-25 18:51:02
【问题描述】:

我在清单中注册了一个广播接收器,如下所示:

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN"/>

            <category android:name="android.intent.category.LAUNCHER"/>
        </intent-filter>
    </activity>

    <receiver android:name=".SMSReceiver" android:permission="android.permission.RECEIVE_SMS" android:exported="true">
        <intent-filter android:priority="9999">
            <action android:name="android.provider.telephony.SMS_RECEIVED" />
        </intent-filter>
    </receiver>
</application>

我的 SMSReceiver 类是这样的:

public class SMSReceiver extends BroadcastReceiver
{
    @Override
    public void onReceive(Context context, Intent intent)
    {
        if     (intent.getAction().equalsIgnoreCase("android.provider.Telephony.SMS_RECEIVED")) {
...

但是 onReceive 方法中的代码永远不会被调用。我试过在接收器中没有 android:exported 和 android:permission 标签,但没有任何效果。

【问题讨论】:

  • 您是否在安装后至少启动了一次MainActivity 以使应用退出停止状态?另外,从&lt;receiver&gt; 元素中删除android:permission="android.permission.RECEIVE_SMS"
  • 是的,我做到了。然后我用 android 任务管理器杀死应用程序
  • 那么这很可能是强行停止您的应用程序,并将其恢复为停止状态。在您再次启动应用程序之前,您的接收器将无法再次工作。
  • 实际上,我刚刚注意到,您想将该权限更改为 BROADCAST_SMS,因为您没有指定该权限。
  • telephony 需要在&lt;receiver&gt;&lt;action&gt; 中大写。即"android.provider.Telephony.SMS_RECEIVED"。抱歉,我早就注意到了,但我认为您的标题意味着它在应用运行时确实工作。

标签: android broadcastreceiver


【解决方案1】:

添加RECEIVE_SMS 权限以及READ_SMS

ActivityCompat.requestPermissions(this, 
            new String[]{Manifest.permission.RECEIVE_SMS},
            MY_PERMISSIONS_REQUEST_SMS_RECEIVE);

这应该可以。

【讨论】:

    【解决方案2】:

    从 android 4.4 开始,您必须实现一些功能才能将您的短信应用程序更改为默认短信应用程序和 getSms/MMS。并且只有默认应用会收到短信/彩信的通知。

    这个答案可以帮助你做到这一点:link

    【讨论】:

    • 这是不正确的。从 4.4 开始,SMS_RECEIVED 动作仍然是广播的,并且不能中止。为该广播注册的任何应用都可以接收它。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-04-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多