gpt4 book ai didi

android - 如何在两个不同的端口接收数据短信

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:06:16 31 4
gpt4 key购买 nike

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

应用 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);

最佳答案

要从不同端口接收短信,请在您的 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]);
}
}

关于android - 如何在两个不同的端口接收数据短信,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14209889/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com