5

我有两个具有两个不同数据短信广播的应用程序,我想在它们中接收数据短信。为此,我在每个应用程序中创建了广播

应用程序 1 中的接收器一:

 <receiver android:name=".SMSReceiver" >
            <intent-filter android:priority="10" >
                <action android:name="android.intent.action.DATA_SMS_RECEIVED" />
                <data android:scheme="sms" />
                <data android:port="5555" />
            </intent-filter>
  </receiver>

应用程序二中的接收器二:

  <receiver android:name=".SMSReceiver1" >
            <intent-filter android:priority="10" >
                <action android:name="android.intent.action.DATA_SMS_RECEIVED" />
                <data android:scheme="sms" />
                <data android:port="8901" />
            </intent-filter>
   </receiver>

现在的情况是这样的,仅在第一个安装的应用程序中收到短信,我尝试过使用相同的端口、类名和不同的。但是问题没有解决,请问怎么解决??这是我的广播接收器类

public class SMSReceiver extends BroadcastReceiver {



    @Override
    public void onReceive(Context context, Intent intent) {
        Bundle myBundle = intent.getExtras();

        SmsMessage[] messages = null;
        String strMessage = "";
        byte[] data = null;

        if (myBundle != null) {

            Object[] pdus = (Object[]) myBundle.get("pdus");
            messages = new SmsMessage[pdus.length];

            for (int i = 0; i < messages.length; i++) {
                messages[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
                phoneNo = messages[i].getOriginatingAddress();
                try {
                    data = messages[i].getUserData();

                } catch (Exception e) {

                }
                if (data != null) {
                    for (int index = 0; index < data.length; ++index) {
                        strMessage += Character.toString((char) data[index]);
                    }
                }

            }

            message = strMessage;


            System.out.println("message received: " + message);

    }
}

和我发送消息的代码

SmsManager smsManager = SmsManager.getDefault();
            smsManager.sendDataMessage(myNumber, null,(short)8901, sms.getBytes(), null, null);
4

2 回答 2

1

需要仔细检查的几件事:

a) 你确定你android.permission.RECEIVE_SMS在两个应用程序中都添加了权限吗?

b) 你能从两个清单中删除这部分以确保意图过滤不会混乱吗?

<data android:scheme="sms" />
<data android:port="5555" />

c) 如果 b) 有效,您可能有兴趣在您的接收器代码中进行过滤。

于 2013-01-15T23:17:49.813 回答
1

要从不同的端口接收短信,请在您的 AndroidManifest.xml 中设置此接收器

<receiver android:name=".SMSBcastReceiver">
    <intent-filter android:priority="10"> 
    <action android:name="android.intent.action.DATA_SMS_RECEIVED" /> 
<data android:scheme="sms" /> 
        <data android:port="13013" /> 
</intent-filter>
</receiver>

然后在你的 SMSBcastReceiver 类中设置它

public void onReceive(final Context context, Intent intent) {

Bundle bundle = intent.getExtras();

Object messages[] = (Object[]) bundle.get("pdus");
SmsMessage smsMessage[] = new SmsMessage[messages.length];

for (int n = 0; n < messages.length; n++) 
{
smsMessage[n] = SmsMessage.createFromPdu((byte[]) messages[n]);
}

smsOrigin = smsMessage[0].getDisplayOriginatingAddress();
smsData = smsMessage[0].getUserData();

for(int index=0; index<smsData.length; ++index)
{
smsBody += Character.toString((char)smsData[index]);
}
}
于 2013-01-17T05:46:34.400 回答