1. Android API说明:
sendDataMessage(String destinationAddress, String scAddress, short destinationPort,byte[] data, PendingIntent sentIntent, PendingIntent deliveryIntent);
destinationAddress:目的地(对方手机号)
scAddress:源地址(一般是本机手机号,null表示使用本机号码)
destinationPort:目的端口(对方接受短信的端口号)
data:需发送的数据内容
sentIntent:数据发送成功/失败后,会broadcast这个intent
deliveryIntent:数据到达对方后,会broadcast这个intent
2. 使用方法:
2.1 发送端
SmsManager manager = SmsManager.getDefault();
String SENT = "sms_sent";
String DELIVERED = "sms_delivered";
PendingIntent sentPI = PendingIntent.getActivity(this, 0, new Intent(SENT), 0);
PendingIntent deliveredPI = PendingIntent.getActivity(this, 1, new Intent(DELIVERED), 0);
byte[] data = "test".getBytes();
manager.sendDataMessage( "136*********" , null, (short) 1000, data, sentPI, deliveredPI);
2.2 接收端
AndroidManifest.xml中注册:
java代码中解析:
String action = intent.getAction();
if (action.equals("android.intent.action.DATA_SMS_RECEIVED"))
{
Bundle bundle = arg1.getExtras();
SmsMessage[] msgs = null;
if (bundle != null)
{
Object[] pdus = (Object[]) bundle.get("pdus");
msgs = new SmsMessage[pdus.length];
for (int i = 0; i
{
msgs[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
byte data[] = SmsMessage.createFromPdu((byte[]) pdus[i]).getUserData();
System.out.println("received: " + ByteBufferUtil.printlnByteArrayToUnsignHexString(data));
}
}
}
3. 调试
使用logcat -b radio -v time查看ril日志,例:
11-19 16:33:32.073 D/RIL ( 134): onRequest: SEND_SMS
11-19 16:33:32.073 D/AT ( 134): AT> AT+CMGS=24
11-19 16:33:32.104 D/AT ( 134): AT
11-19 16:33:32.104 D/AT ( 134): AT> 0061000b813176814453f500040b06050403e8000074657374^Z
11-19 16:33:44.205 D/AT ( 134): AT
第一行表示发送SMS;AT> AT+CMGS=24表示通过AT发送SMS;第四行是实际发送;第五行返回AT错误,38表示网络制式错误,如返回OK则表示成功。对于0061000b813176814453f500040b06050403e8000074657374的解析,可以百度,红色部分是需要发送的byte数组(即test)。
总结:上面是sendDataMessage的基本用法,想要了解更多内容,需要自行上网搜索。