我有两个具有两个不同数据短信广播的应用程序,我想在它们中接收数据短信。为此,我在每个应用程序中创建了广播
应用程序 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);