我有两个应用程序,有两个不同的数据短信广播,我想在它们两个接收数据短信。为此,我在每个应用程序中创建了广播
应用程序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);
答案 0 :(得分:1)
要仔细检查的事情:
a)您确定在两个应用程序中都添加了android.permission.RECEIVE_SMS
权限吗?
b)您是否可以从两个清单中删除此部分以确保意图过滤不会搞砸?
<data android:scheme="sms" />
<data android:port="5555" />
c)如果b)有效,您可能有兴趣在Receiver代码中进行过滤。
答案 1 :(得分: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]);
}
}