短信发送流程图形剖析(含编码)Android平台.doc_第1页
短信发送流程图形剖析(含编码)Android平台.doc_第2页
短信发送流程图形剖析(含编码)Android平台.doc_第3页
短信发送流程图形剖析(含编码)Android平台.doc_第4页
短信发送流程图形剖析(含编码)Android平台.doc_第5页
已阅读5页,还剩3页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

Android平台 短信发送流程图形剖析(含编码)转自:/blog/1306918 其他SMS/MMS分析:/category/187047本文对Android平台短信发送流程进行了走读和剖析,特别是编码部分,今天将流程整理出来,以便平时参考,也希望对大家有用!先上图,下面2个图是用PPT画的,这里截图附上来: 流程图1:Framewoks 流程图2:发送流程编码解析:从上图中的GsmSMSDispatcher的sendText开始分析Java代码 1. /GsmSMSDispatcher.java2. /*inheritDoc*/3. Override4. /入口5. protectedvoidsendText(StringdestAddr,StringscAddr,Stringtext,6. PendingIntentsentIntent,PendingIntentdeliveryIntent)7. SmsMessage.SubmitPdupdu=SmsMessage.getSubmitPdu(/转分析8. scAddr,destAddr,text,(deliveryIntent!=null);9. 10. HashMapmap=SmsTrackerMapFactory(destAddr,scAddr,text,pdu);11. SmsTrackertracker=SmsTrackerFactory(map,sentIntent,deliveryIntent,12. RadioTechnologyFamily.RADIO_TECH_3GPP);13. sendRawPdu(tracker);/转I分析14. 15. 16. 分析:17. /SmsMessage.java18. publicstaticSubmitPdugetSubmitPdu(StringscAddress,19. StringdestinationAddress,Stringmessage,20. booleanstatusReportRequested)21. returngetSubmitPdu(scAddress,destinationAddress,message,statusReportRequested,null);/转分析22. 23. 24. 分析25. /SmsMessage.java26. publicstaticSubmitPdugetSubmitPdu(StringscAddress,27. StringdestinationAddress,Stringmessage,28. booleanstatusReportRequested,byteheader)29. returngetSubmitPdu(scAddress,destinationAddress,message,statusReportRequested,header,ENCODING_UNKNOWN/*默认编码方式*/);/转分析30. 31. 32. 分析编码核心函数33. /SmsMessage.java34. publicstaticSubmitPdugetSubmitPdu(StringscAddress,35. StringdestinationAddress,Stringmessage,36. booleanstatusReportRequested,byteheader,intencoding)37. /338. /Performnullparameterchecks.39. if(message=null|destinationAddress=null)40. returnnull;41. 42. 43. SubmitPduret=newSubmitPdu();44. /MTI=SMS-SUBMIT,UDHI=header!=null45. bytemtiByte=(byte)(0x01|(header!=null?0x40:0x00);46. /MTI:bit0和bit1UDHI:bit647. /0x01=000000010x40=0010000048. ByteArrayOutputStreambo=getSubmitPduHead(49. scAddress,destinationAddress,mtiByte,50. statusReportRequested,ret);/转分析51. /UserData(andlength)/TP-DCS和TP-UDL52. byteuserData;53. if(encoding=ENCODING_UNKNOWN)54. /First,tryencodingitwiththeGSMalphabet55. encoding=ENCODING_7BIT;/默认先采用ENCODING_7BIT编码模式56. 57. try58. if(encoding=ENCODING_7BIT)59. userData=GsmAlphabet.stringToGsm7BitPackedWithHeader(message,header);60. /采用ENCODING_7BIT进行编码,若出现编码异常,函数会抛出异常:EncodeException,转至处。编码成功转至。stringToGsm7BitPackedWithHeader分析转处61. else/assumeUCS-262. try63. userData=encodeUCS2(message,header);64. catch(UnsupportedEncodingExceptionuex)65. Log.e(LOG_TAG,66. ImplausibleUnsupportedEncodingException,67. uex);68. returnnull;69. 70. 71. catch(EncodeExceptionex)/7bit编码模式失败后,就采用UCS-2进行编码72. /Encodingtothe7-bitalphabetfailed.Letsseeifwecan73. /senditasaUCS-2encodedmessage74. try75. userData=encodeUCS2(message,header);76. encoding=ENCODING_16BIT;77. catch(UnsupportedEncodingExceptionuex)78. Log.e(LOG_TAG,79. ImplausibleUnsupportedEncodingException,80. uex);81. returnnull;82. 83. 84. 85. if(encoding=ENCODING_7BIT)/7bit编码成功86. if(0xff&userData0)MAX_USER_DATA_SEPTETS)87. /Messagetoolong88. returnnull;89. 90. /TP-Data-Coding-Scheme91. /Defaultencoding,uncompressed92. /TotestwritingmessagestotheSIMcard,changethisvalue0x0093. /to0x12,whichmeansbits1and0containmessageclass,andthe94. /classis2.Notethatthistakeseffectforthesender.Inother95. /words,messagessentbythephonewiththischangewillendupon96. /thereceiversSIMcard.Youcanthensendmessagestoyourself97. /(onaphonewiththischange)andtheyllendupontheSIMcard.98. /0x12=00010010未压缩,class2,存储到SIM卡99. /0x00=00000000未压缩,class0,GSM7bit编码100. bo.write(0x00);101. else/assumeUCS-2102. if(0xff&userData0)MAX_USER_DATA_BYTES)103. /Messagetoolong104. returnnull;105. 106. /TP-Data-Coding-Scheme107. /Class3,UCS-2encoding,uncompressed108. bo.write(0x0b);/0x0b=00001011未压缩,class3,UCS-2编码109. 110. 111. /(noTP-Validity-Period)112. bo.write(userData,0,userData.length);113. ret.encodedMessage=bo.toByteArray();114. returnret;115. 116. 117. 分析118. /SmsMessage.java119. privatestaticByteArrayOutputStreamgetSubmitPduHead(120. StringscAddress,StringdestinationAddress,bytemtiByte,121. booleanstatusReportRequested,SubmitPduret)122. /scAddress为短信中心号码,destinationAddress为目标地址号码123. /mtiByte为MTI和UDHI编码数据,见上面分析,statusReportRequested为状态报告/ret下面会写入数据到ret124. 125. ByteArrayOutputStreambo=newByteArrayOutputStream(126. MAX_USER_DATA_BYTES+40);127. 128. /SMSCaddresswithlengthoctet,or0129. if(scAddress=null)130. ret.encodedScAddress=null;131. else132. ret.encodedScAddress=PhoneNumberUworkPortionToCalledPartyBCDWithLength(133. scAddress);134. 135. /TP-Message-Type-Indicator(andfriends)136. if(statusReportRequested)137. /SetTP-Status-Report-Requestbit./TP-SRRbit5,0x20=00100000138. mtiByte|=0x20;139. if(Config.LOGD)Log.d(LOG_TAG,SMSstatusreportrequested);140. 141. bo.write(mtiByte);142. 143. /spaceforTP-Message-Reference/TP-MR144. bo.write(0);145. 146. bytedaBytes;147. 148. daBytes=PhoneNumberUworkPortionToCalledPartyBCD(destinationAddress);149. 150. /destinationaddresslengthinBCDdigits,ignoringTONbyteandpad151. /TODOShouldbebetter.152. bo.write(daBytes.length-1)*2153. -(daBytesdaBytes.length-1&0xf0)=0xf0?1:0);154. 155. /destinationaddress156. bo.write(daBytes,0,daBytes.length);157. 158. /TP-Protocol-Identifier/TP-PID159. bo.write(0);160. returnbo;161. 162. 163. 分析164. /GsmAlphabet.java165. publicstaticbytestringToGsm7BitPackedWithHeader(Stringdata,byteheader)166. throwsEncodeException/这里传进来的head为null167. 168. if(header=null|header.length=0)169. returnstringToGsm7BitPacked(data);/转分析170. 171. 172. intheaderBits=(header.length+1)*8;173. intheaderSeptets=(headerBits+6)/7;174. 175. byteret=stringToGsm7BitPacked(data,headerSeptets,true);176. 177. /Pasteintheheader178. ret1=(byte)header.length;179. System.arraycopy(header,0,ret,2,header.length);180. returnret;181. 182. 183. 分析184. /GsmAlphabet.java185. publicstaticbytestringToGsm7BitPacked(Stringdata)186. throwsEncodeException187. returnstringToGsm7BitPacked(data,0,true);/转分析188. 189. 190. 分析191. /GsmAlphabet.java192. publicstaticbytestringToGsm7BitPacked(Stringdata,intstartingSeptetOffset,193. booleanthrowException)throwsEncodeException194. intdataLen=data.length();195. intseptetCount=countGsmSeptets(data,throwException)+startingSeptetOffset;196. /当传入的字符串data中含有charToGsm,charToGsmExtended中没有的字符时(例如汉字),该函数会抛出异常,这样在调用处会捕获该异常。然后会采用UCS-2方式进行编码。197. 198. if(septetCount255)199. thrownewEncodeException(Payloadcannotexceed255septets);200. 201. intbyteCount=(septetCount*7)+7)/8;202. byteret=newbytebyteCount+1;/Includespaceforonebytelengthprefix.203. for(inti=0,septets=startingSeptetOffset,bitOffset=startingSeptetOffset*7;204. idataLen&septetsseptetCount;205. i+,bitOffset+=7)206. charc=data.charAt(i);207. intv=GsmAlphabet.charToGsm(c,throwException);208. if(v=GSM_EXTENDED_ESCAPE)209. v=GsmAlphabet.charToGsmExtended(c);/Lookuptheextendedchar.210. packSmsChar(ret,bitOffset,GSM_EXTENDED_ESCAPE);211. bitOffset+=7;212. septets+;213. 214. packSmsChar(ret,bitOffset,v);215. septets+;216. 217. ret0=(byte)(septetCount);/Validatedbycheckabove.218. returnret;219. 220. 221. I分析:222. /SMSDispatcher.java223. protectedvoidsendRawPdu(SmsTrackertracker)224. HashMapmap=tracker.mData;225. bytepdu=(byte)map.get(pdu);226. 227. PendingIntentsentIntent=tracker.mSentIntent;228. if(mSmsSendDisabled)229. if(sentIntent!=null)230. try231. sentIntent.send(RESULT_ERROR_NO_SERVICE);232. catch(CanceledExceptionex)233. 234. Log.d(TAG,Devicedoesnotsupportsendingsms.);235. return;236. 237. 238. if(pdu=null)239. if(sentIntent!=null)240. try241. sentIntent.send(RESULT_ERROR_NULL_PDU);242. catch(CanceledExceptionex)243. 244. return;245. 246. 247. intss=mPhone.getServiceState().getState();248. 249. /ifIMSnotregisteredondataandvoiceisnotavailable.250. if(!isIms()&ss!=S

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论