【问题标题】:Android Broadcast receiver not receving income SMSAndroid广播接收器不接收传入的短信
【发布时间】:2013-08-02 09:08:33
【问题描述】:

我写了一个接收收入短信的短信接收器,一切看起来都很好,但事实并非如此 有效,接收方没有收到短信收入。这是代码和清单内容。 我记得我在android 2.3 中编写了相同的应用程序,但运行良好,但此代码在android 4.x 中运行,无法正常运行。问题是什么 ?是否取决于android 4.x 的安全问题?

清单:

<receiver android:name="SmsReceiver">
        <intent-filter android:priority="100">
            <action android:name="android.provider.telephony.SMS_RECEIVED"/>
        </intent-filter>
    </receiver>

Java 代码:

 public class SmsReceiver extends BroadcastReceiver {

private static final String SMS_RECEIVED = "android.provider.Telephony.SMS_RECEIVED";

@Override
public void onReceive(Context context, Intent intent) {
    String action = intent.getAction();
    Logger.i("INCOMMING SMS...");
    if (action == SMS_RECEIVED) {
        Bundle bundle = intent.getExtras();
        if (bundle != null) {
            Object[] pdus = (Object[]) bundle.get("pdus");
            final SmsMessage[] messages = new SmsMessage[pdus.length];
            for (int i = 0; i < pdus.length; i++) {
                messages[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
            }
            if (messages.length > -1) {
                String sendr = messages[0].getOriginatingAddress();
                Logger.i(sendr);
            }
        }
    }
}


}

【问题讨论】:

标签: android broadcastreceiver


【解决方案1】:

我遇到了同样的问题,我已经通过向清单注册接收器添加另外两个操作来修复它,所以它看起来像这样:

<receiver android:name=".SmsReceiver">
    <intent-filter android:priority="100">
    <action android:name="android.intent.action.PHONE_STATE"/>
        <action android:name="android.provider.telephony.SMS_RECEIVED"/>
        <action android:name="com.your.package.android.action.broadcast"/>
    </intent-filter>
  </receiver>

还添加权限:

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

对于字符串的比较,不要使用equals运算符,而是使用equals方法。(注意。equalsIgnoreCase()应该更适合你。)

所以它会像:

if(SMS_RECEIVED.equalsIgnoreCase(action))
{
//continue
}

希望对您有所帮助。

【讨论】:

    【解决方案2】:

    我遇到了和你一样的问题,在网上查了很多资料,我找到了问题所在。您的应用程序未启动,并且 Emulator Control 未发送选项 --include-stopped-packages ,因为 adb 命令可以执行此操作。这是 Android 3.x 中引入的一项功能。

    因此,您的接收器永远不会收到广播,因为您的应用程序没有启动。 要首先启动它,请从您的系统中打开一个控制台并输入以下命令:

    *adb -e shell am broadcast -a android.provider.Telephony.SMS_RECEIVED --include-stopped-packages*
    

    您的 BroadcastReceiver 收到一条空短信。

    执行此命令后,您的进程将显示在“设备”视图中,您可以从模拟器控件发送 SMS。

    【讨论】:

      猜你喜欢
      • 2022-11-28
      • 2011-05-06
      • 2022-01-08
      • 1970-01-01
      • 1970-01-01
      • 2012-11-14
      • 1970-01-01
      相关资源
      最近更新 更多