1. Android API說明:java
sendDataMessage(String destinationAddress, String scAddress, short destinationPort,byte[] data, PendingIntent sentIntent, PendingIntent deliveryIntent);android
destinationAddress:目的地(對方手機號)數組
scAddress:源地址(通常是本機手機號,null表示使用本機號碼)網絡
destinationPort:目的端口(對方接受短信的端口號)this
data:需發送的數據內容spa
sentIntent:數據發送成功/失敗後,會broadcast這個intent調試
deliveryIntent:數據到達對方後,會broadcast這個intent日誌
2. 使用方法:code
2.1 發送端xml
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中註冊:
<intent-filter> <action android:name="android.intent.action.DATA_SMS_RECEIVED" /> <data android:scheme="sms" /> <data android:host="localhost" /> <data android:port="1000" /> <!-- 應與發送端同樣,實際使用時不一樣也無影響 --> </intent-filter>
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.length; 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< +CMS ERROR: 38
第一行表示發送SMS;AT> AT+CMGS=24表示經過AT發送SMS;第四行是實際發送;第五行返回AT錯誤,38表示網絡制式錯誤,如返回OK則表示成功。對於0061000b813176814453f500040b06050403e8000074657374的解析,能夠百度,紅色部分是須要發送的byte數組(即test)。
總結:上面是sendDataMessage的基本用法,想要了解更多內容,須要自行上網搜索。