我尝试从 telnet 向模拟器发送短信
使用这个命令:sms send (number) (body)
由于某些原因,有时会以普通英文查看消息,而有时会以汉字查看消息!
例如,当我尝试这个时,我得到了完美的信息:
sms send 123 hey man what's up
当我尝试这个时,我得到了汉字:
sms send 123 car id =5, latitude = 4343.343, longitude =23112
这是我的代码:
public class SmsReceiver extends BroadcastReceiver {
ArrayList<SmsReceivedListener> listeners = new ArrayList<SmsReceivedListener>();
private static SmsReceivedListener listener;
private static final String SMS_RECEIVED = "android.provider.Telephony.SMS_RECEIVED";
@SuppressLint("NewApi") @Override
public void onReceive(final Context context, final Intent intent) {
if (intent != null && SMS_RECEIVED.equals(intent.getAction())) {
final SmsMessage smsMessage = extractSmsMessage(intent);
if(getSmsReceivedListener()!=null)
{
String body = smsMessage.getMessageBody();
Toast.makeText((Context) context, body,
Toast.LENGTH_LONG).show();
}
}
}
@SuppressLint("NewApi") private SmsMessage extractSmsMessage(final Intent intent) {
final Bundle pudsBundle = intent.getExtras();
final Object[] pdus = (Object[]) pudsBundle.get("pdus");
final SmsMessage smsMessage = SmsMessage.createFromPdu((byte[]) pdus[0]);
return smsMessage;
}
}