【问题标题】:receive SMS from a port [duplicate]从端口接收短信[重复]
【发布时间】:2016-09-27 06:38:16
【问题描述】:

我想创建一个可以从端口接收SMS 的应用程序!但是当运行它时,我的msg.getMessageBody() 返回null。我该如何解决?

ma​​nifext.xml

<receiver android:name="data.SMSReceiver" android:enabled="true">
        <intent-filter>
            <action android:name="android.provider.telephony.SMS_RECEIVED" />
            <data android:scheme="sms"
                android:host="*"
                android:port="4030"/>
        </intent-filter>

广播接收器代码

public class SMSReceiver extends BroadcastReceiver {

private static final String SHORTCODE = "+9810004473";

@Override
public void onReceive(Context context, Intent intent) {
    Bundle bundle = intent.getExtras();

    Object[] messages = (Object[])bundle.get("pdus");
    SmsMessage[] sms = new SmsMessage[messages.length];
    for(int n=0; n < messages.length; n++) {
        sms[n] = SmsMessage.createFromPdu((byte[]) messages[n]);
    }
    for(SmsMessage msg : sms) {
        if(TextUtils.equals(msg.getOriginatingAddress(), SHORTCODE)) {
            Toast.makeText(context, "SMS: " + msg.getMessageBody(), Toast.LENGTH_LONG).show();
        }
    }
}

}

我的Toast 显示:SMS: null

【问题讨论】:

  • 数据短信不会有正文。使用SmsMessage#getUserData() 方法检索数据,该数据将采用byte 数组的形式,如上面链接帖子中接受的答案所示。
  • tnx!没错!

标签: android null sms port


【解决方案1】:

您可以将此代码替换为您在onReceive 中的代码:

// Get the object of SmsManager
final SmsManager sms = SmsManager.getDefault();

public void onReceive(Context context, Intent intent) {

    // Retrieves a map of extended data from the intent.
    final Bundle bundle = intent.getExtras();

    try {

      Bundle bundle = intent.getExtras(); 

        String recMsgString = "";            
        String fromAddress = "";
        SmsMessage recMsg = null;
        byte[] data = null;
        if (bundle != null)
        {
            //---retrieve the SMS message received---
           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();
            }

    } catch (Exception e) {
        Log.e("SmsReceiver", "Exception smsReceiver" +e);

    }
}    

好的,如果您之前已经对您的消息进行了编码并且您现在想对其进行解码,您必须按照错误的步骤进行操作,但如果不是删除您的整个 onReceive 并替换我的代码。 您在错误的地方解密您的消息,您必须按照我写的确切步骤进行操作。你必须在这里得到消息(在得到data之后):

    if (data!=null){
                    for(int index=0; index<data.length; ++index)
                    {
                           recMsgString += Character.toString((char)data[index]);
                    } 
                }
String decryptedData = Base64.decode(recMsgString ,...)//add this line

在得到recMsgString 中的消息字符串后,您可以使用Base64 类对其进行解密。

【讨论】:

  • 不!在您的代码中,消息为空! :(
  • TNX 米拉德!现在,我可以从 data = recMsg.getUserData() 获取字节数组,但是 recMsgString 具有字节格式!和新字符串(数据,“UTF-8”)不起作用:|
  • recMsgString 被定义为不能转换为字节的字符串。此行将消息作为字符串 for(int index=0; index&lt;data.length; ++index) { recMsgString += Character.toString((char)data[index]); }
  • recMsgString 格式错误!因为数据被编码了!例如:对于从门户发送的“测试消息”,recMsgString 等于“dGVzdCBtZXNzYWdl”:|
  • 米拉德!!! :))))) 做!数据 = Base64.decode(数据,Base64.DEFAULT); String m1 = new String(data, "UTF-8");
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多