接收长二进制短信时遇到问题。客户端应该接收多部分二进制短信。在搜索 stackoverflow 时,人们提到 onReceive() 只能被触发一次。从那里您可以连接多个 SMS。
我不确定发送二进制短信时是否相同。我收到 2 条短信,并且 onReceive 被触发了两次。SMS 彼此分开,因此无法连接。
安卓清单:
<uses-permission android:name="android.permission.READ_SMS" />
<uses-permission android:name="android.permission.SEND_SMS" />
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.INTERNET" />
<receiver
android:name="com.myapp.smsnotification.SMSReceiver"
android:enabled="true" >
<intent-filter android:priority="100" >
<action android:name="android.intent.action.DATA_SMS_RECEIVED" />
<data
android:host="localhost"
android:port="9515"
android:scheme="sms" />
</intent-filter>
</receiver>
SMSReceiver.java
在我的 onReceive()
Bundle bundle = intent.getExtras();
if (bundle != null) {
Object[] pdus = (Object[]) bundle.get("pdus");
SmsMessage[] messages = new SmsMessage[pdus.length];
for (int i = 0; i < pdus.length; i++) {
messages[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
}
try {
if (messages.length > -1) {
ByteArrayOutputStream os = new ByteArrayOutputStream();
for (int i = 0; i < messages.length; i++) {
SmsMessage message = messages[i];
os.write(message.getUserData());
}
byte[] stream = os.toByteArray();
// do something with stream
}
} catch (Exception e) {
Log.e(TAG, e.getMessage());
}
}