我正在使用端口在我的应用程序中发送数据消息。
我在发送数据消息时遇到问题(android 中的 sendDataMessage)。我的代码适用于 MCI sim 卡,我的意思是当发送方和接收方都有 MCI sim 卡时。
如果发件人有 MCI,而收件人有 Irancell sim 卡,则消息正确传送。
相反,如果发送者有一个 Irancell sim 而接收者有一个 MCI sim,则根本不会发送 :((虽然在这种情况下文本消息发送/接收正常工作,但数据消息将不起作用)。
此外,当发送者和接收者拥有 Irancell sims 时,它可以正常工作。请帮我解决这个问题。
我的清单代码:
<receiver android:name=".SmsListener">
<intent-filter android:priority="999">
<action android:name="android.intent.action.DATA_SMS_RECEIVED" />
<intent-filter android:priority="999">
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
<data android:scheme="sms" />
<data android:port="7442" />
</intent-filter>
<intent-filter android:priority="1000">
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
<meta-data android:name="port" android:value="7442" />
我的短信监听器:
public void onReceive(Context context, Intent intent)
{
Toast.makeText(context, "Yuhuuu", Toast.LENGTH_LONG).show();
MainActivity.smsReceived(true);
Bundle bundle = intent.getExtras();
String recMsgString = "";
String fromAddress = "";
SmsMessage recMsg = null;
byte[] data = null;
if (bundle != null) {
Object[] pdus = (Object[]) bundle.get("pdus");
for (int i = 0; i < pdus.length; i++) {
recMsg = SmsMessage.createFromPdu((byte[]) pdus[i]);
try {
data = recMsg.getUserData();
} catch (Exception e) {
}
if (data != null) {
for (int index = 0; index < data.length; ++index) {
recMsgString += Character.toString((char) data[index]);
}
}
fromAddress = recMsg.getOriginatingAddress();
}
}
System.out.println(recMsgString + " ,this Has been Recieved from: " + fromAddress);
}
和我的 DataMessage 发件人代码:
String messageText = "some text";
short SMS_PORT = 7442;
SmsManager smsManager = SmsManager.getDefault();
String phoneNumber = "+98910*******";
smsManager.sendDataMessage(phoneNumber, null, shortValue, messageText, null, null);
}