Android 尽管有优先级,短信广播接收器仍未触发

Android Sms Broadcast Receiver not firing despite priority

我读过很多类似的问题,这些问题的答案通常都是关于优先级的。我不认为我的情况是这样的原因是我 phone 上的其他 SMS 阅读器(如自动化应用程序)可以很好地接收广播。我想 post 我目前正在做的事情的过程,并三重确保我的代码没有做错导致失败的事情。感谢您提供的任何提示!

注意: 我尝试过设置最高整数优先级,优先级 99、100、0 或 none。它们都不起作用。

AndroidManifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.hennessylabs.appname" >

    <uses-permission android:name="android.permission.CALL_PHONE" />
    <uses-permission android:name="android.permission.READ_CONTACTS" />
    <uses-permission android:name="android.permission.SEND_SMS" />
    <uses-permission android:name="android.permission.READ_SMS" />

    <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"
            android:label="@string/app_name"
            android:theme="@style/AppTheme.NoActionBar" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <receiver android:name="com.hennessylabs.drivercompanion.ProcessTextMessage" android:exported="true">
        <intent-filter android:priority="999" android:permission="android.permission.BROADCAST_SMS">
            <action android:name="android.provider.Telephony.SMS_RECEIVED"></action>
        </intent-filter>
    </receiver>
    </application>


</manifest>

广播接收器:

package com.hennessylabs.appname;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.provider.Telephony;
import android.telephony.SmsManager;
import android.telephony.SmsMessage;
import android.util.Log;
import android.widget.Toast;

/**
 * Created by kyleC on 11/15/2015.
 */
public class ProcessTextMessage extends BroadcastReceiver {
    private static final String SMS_RECEIVED = "android.provider.Telephony.SMS_RECEIVED";
    final SmsManager sms = SmsManager.getDefault();
    @Override
    public void onReceive(Context context, Intent intent) {
        Toast.makeText(context, "Entered onReceive", Toast.LENGTH_LONG).show();
        // Retrieves a map of extended data from the intent.
        final Bundle bundle = intent.getExtras();

        try {

            if (bundle != null) {

                final Object[] pdusObj = (Object[]) bundle.get("pdus");

                for (int i = 0; i < pdusObj.length; i++) {

                    SmsMessage currentMessage = SmsMessage.createFromPdu((byte[]) pdusObj[i]);
                    String phoneNumber = currentMessage.getDisplayOriginatingAddress();

                    String senderNum = phoneNumber;
                    String message = currentMessage.getDisplayMessageBody();

                    Log.i("SmsReceiver", "senderNum: "+ senderNum + "; message: " + message);


                    // Show alert
                    int duration = Toast.LENGTH_LONG;
                    Toast toast = Toast.makeText(context, "senderNum: " + senderNum + ", message: " + message, duration);
                    toast.show();

                } // end for loop
            } // bundle is null

        } catch (Exception e) {
            Log.e("SmsReceiver", "Exception smsReceiver" +e);

        }
    }
}

预期结果:

预期的结果是,当短信到达时,屏幕首先会显示一个进入OnReceive方法的Toast。然后它会记录和 Toast 短信的内容。

实际结果:

预期的结果没有发生。事实上,即使连接到 USB 并处于调试模式,它似乎也根本不会输入 class。所以也许我的清单设置有误?

您的收件人清单声明中遗漏了一些内容。您必须声明 exported=true 并且您必须要求 BROADCAST_SMS 许可。查看工作示例:

    <receiver android:name=".sms.IncomingSmsReceiver" android:exported="true">
        <intent-filter android:priority="999" android:permission="android.permission.BROADCAST_SMS">
            <action android:name="android.provider.Telephony.SMS_RECEIVED"></action>
        </intent-filter>
    </receiver>

那么,您的应用 运行 所在设备的版本是什么?

从Android 4.4开始,只有默认的短信应用程序可以接收SMS_DELIVER_ACTION广播。

更多信息:http://android-developers.blogspot.sg/2013/10/getting-your-sms-apps-ready-for-kitkat.html

如果其他一切都正确,您似乎只是缺少 RECEIVE_SMS 权限。

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