最近要写一个Android app,其中一个功能要发短信,直接照抄Android API Demos的例子OS\SMS Messaging,在自己的手机上测试,发现总是报错SmsManager.RESULT_ERROR_NO_SERVICE,理解不能。
于是开始Google。发现网上很少有人提到这个错误,而且Android上发短信,全部都是用的API Demos的发短信的例子,或者使用Intent调用系统短信App来发短信。虽然用Intent调用系统短信App来发短信也可以当作一个workround,但用户体验不好,感觉不爽。我的应用里的发短信流程应该是这样的:用户点击按钮,弹出ProgressDialog,程序在后台悄悄的发短信,开枪的不要,然后告诉用户短信发成功没有。
既然网上几乎全部的coder都用API Demos的短信例子,包括几本ebook(《Beginning Android Application Development - 8 Messaging and Networking》、《Professional Android 4 Application Development》)都是,我觉得要么是Google在撒谎,Android的SmsManager其实有重大BUG;要么就是我每次打开Ecllipse的方式不对,或者我其实生活在Matrix里。
NND,继续深挖。于是就找到了“adb logcat -b radio”这个用法,即查看GSM模块的通讯log。下面就是系统短信Activity和API Demos短信Activity的log比较:
系统短信Activity的log |
---|
06-10 15:18:26.058 D/SMS (28645): encoding detail>TextEncodingDetails { msgCount=1, codeUnitCount=2, codeUnitsRemaining=68, codeUnitSize=3, languageTable=0, languageShiftTable=0 } 06-10 15:18:26.178 D/RILJ_GSM( 418): [3085]> REPORT_SMS_MEMORY_STATUS: true 06-10 15:18:26.178 D/RILJ ( 418): [3086]> REPORT_SMS_MEMORY_STATUS: true 06-10 15:18:26.188 D/RIL_SWITCH( 100): CT_C+W_enable is NULL, set the value to disable. 06-10 15:18:26.188 D/RIL_SWITCH( 100): ril switch GO HTC RIL 06-10 15:18:26.188 D/RILJ ( 418): [3086]< REPORT_SMS_MEMORY_STATUS 06-10 15:18:26.208 D/RILJ_GSM( 418): [3085]< REPORT_SMS_MEMORY_STATUS 06-10 15:18:26.218 D/RILJ_GSM( 418): [3087]> REPORT_SMS_MEMORY_STATUS: true 06-10 15:18:26.218 D/RILJ ( 418): [3088]> REPORT_SMS_MEMORY_STATUS: true 06-10 15:18:26.228 D/RIL_SWITCH( 100): CT_C+W_enable is NULL, set the value to disable. 06-10 15:18:26.228 D/RIL_SWITCH( 100): ril switch GO HTC RIL 06-10 15:18:26.228 D/RILJ ( 418): [3088]< REPORT_SMS_MEMORY_STATUS 06-10 15:18:26.238 D/RILJ_GSM( 418): [3087]< REPORT_SMS_MEMORY_STATUS 06-10 15:18:26.679 D/GSM ( 418): laugnagetable/shifttable: 0/0 06-10 15:18:26.679 D/GSM ( 418): GEP countGsmSeptets: -1 06-10 15:18:26.679 D/SMS ( 418): sendRawPduWithBundle 06-10 15:18:26.689 D/SMS ( 418): checkInSegmentToRIL> SmsTracker@411f02c0, RetryCnt> 0 06-10 15:18:26.689 D/RILJ_GSM( 418): sendSMS pdu : 01000b813145189164f700080454755475 06-10 15:18:26.689 D/RILJ_GSM( 418): [3089]> SEND_SMS 06-10 15:18:27.410 D/RILJ_GSM( 418): [3090]> REPORT_SMS_MEMORY_STATUS: true 06-10 15:18:27.410 D/RILJ ( 418): [3091]> REPORT_SMS_MEMORY_STATUS: true 06-10 15:18:27.410 D/RIL_SWITCH( 100): CT_C+W_enable is NULL, set the value to disable. 06-10 15:18:27.410 D/RIL_SWITCH( 100): ril switch GO HTC RIL 06-10 15:18:27.410 D/RILJ ( 418): [3091]< REPORT_SMS_MEMORY_STATUS 06-10 15:18:29.812 D/RILMUX ( 744): main(2656) GSM0710 buffer. Stored 0 06-10 15:18:29.812 D/RILMUX ( 744): main(2657) Frames received/dropped: 8632/0 06-10 15:18:31.333 D/RILJ_GSM( 418): [3089]< SEND_SMS { messageRef = 232, errorCode = -1, ackPdu = null} 06-10 15:18:31.333 D/SMS ( 418): handleMessage > 2 06-10 15:18:31.333 D/SMS ( 418): pre error Code: -1 06-10 15:18:31.333 D/SMS ( 418): msgRef> 232, trytpmr> 0 06-10 15:18:31.333 D/SMS ( 418): send complete: SmsTracker@411f02c0 06-10 15:18:31.333 D/SMS ( 418): SMS send complete. Broadcasting intent: PendingIntent{411ce330: android.os.BinderProxy@40eb9c80} 06-10 15:18:31.333 D/SMS ( 418): framework sent intent: SMS_MO/number/1402384711344/1 06-10 15:18:31.433 D/RILJ_GSM( 418): [3090]< REPORT_SMS_MEMORY_STATUS 06-10 15:18:31.794 D/RILJ_GSM( 418): [UNSL]< UNSOL_RESPONSE_NEW_SMS 06-10 15:18:31.794 D/RILJ_GSM( 418): RIL_UNSOL_RESPONSE_NEW_SMS pdu : 0891683108200805F0040D91683145189164F70008416001518142230454755475 06-10 15:18:31.794 D/GSM ( 418): SMS SC address: +8613800280500 06-10 15:18:31.794 D/GSM ( 418): SMS SC timestamp: 1402384704000 06-10 15:18:31.804 V/RILC_IMC( 104): processWakeupCallback 06-10 15:18:31.804 D/SMS ( 418): handleMessage > 1 |
API Demos短信Activity的log |
06-10 14:20:05.949 D/SMS (32003): encoding detail>TextEncodingDetails { msgCount=1, codeUnitCount=13, codeUnitsRemaining=147, codeUnitSize=1, languageTable=0, languageShiftTable=0 } 06-10 14:20:05.959 D/GSM (32003): SMS status report requested 06-10 14:20:05.959 D/GSM (32003): laugnagetable/shifttable: 0/0 06-10 14:20:05.959 D/GSM (32003): GEP countGsmSeptets: 13 06-10 14:20:05.969 D/GSM (32003): charToLanguageTable/shifttable: android.util.SparseIntArray@4100bf78/android.util.SparseIntArray@41014aa8 06-10 14:20:05.969 D/GSM (32003): htc septets count/septets: 13/13 06-10 14:20:05.969 D/CDMA ( 418): [RuimSmsInterfaceManager] sendRawPdu: smsc=null pdu=[B@40f6a400 sentIntentPendingIntent{40f6a430: android.os.BinderProxy@40d86ca0} deliveryIntentPendingIntent{40f6a450: android.os.BinderProxy@40d86d00} 06-10 14:20:05.969 D/SMS ( 418): sendRawPduWithBundle 06-10 14:20:05.969 D/SMS ( 418): handleNotInService, message send fail ss : 1 |
请恕我眼拙,没能从上面的log里看出究竟API Demos短信Activity到底哪里出错了。
于是继续Google,发现了不少有意思的东西:
- SilentSMS:作者用reflection调用了IccSmsInterfaceManager来操作发送短信。虽然看起来很酷,可App的安装需要root权限,所以我没有急着测试这个project;
- Android SMS/MMS/Google Voice Sending Library:作者override了很多Android telephony相关的类,还是beta版本。感觉为了发一个短信而已,用不着这么大的lib吧?
- text+:一款用WIFI来发短信的免费Android App。还有很多类似的产品。其实这类产品已经脱离里简简单单的短信功能了,整个一社交型应用了,国内类似的应用也很多,如微信、QQ等。只不过text+等还是支持将message以SMS发到没有安装text+的手机上。
抱怨这么多,其实就是纠结于为什么网上都能用SmsManager这个简单的API来发短信,而我这边就是不行?!原因究竟何在?!!!
于是继续郁闷地测试,删除系统短信草稿箱里的草稿,看到菜单“设置->短信(SMS)”,于是手贱地点进去:
发送报告 为您发送的每条信息请求一个发送报告 |
服务中心(卡槽一) +8613800XXXXXX |
服务中心(卡槽二) |
管理 UIM 卡信息 管理 CDMA UIM 卡中存储的信息 |
管理 SIM 卡信息 管理 GSM SIM 卡中存储的信息 |
发送报告,唔,这个勾没打,估计会收不到delivery回馈……卡槽一卡槽二,唔,我这个是双卡双待的手机,是有两个卡槽的…………wait,我了个去的,不会吧,难道是因为我这个双卡双待的手机没有插电信的卡而电信的卡又是主卡SmsManager就TMD直接连到主卡上然后报错了吧?!SmsManager,你能更brief点吗?
立马找同事的单卡手机跑了下API Demos,短信发送成功……
又找了另一个同事的手机,双卡双待,副卡槽空的,主卡槽是电信的,插了电信卡,跑API Demos,短信发送成功……
心中那个神兽奔腾啊
Google了三天,看了一堆资料,原来是这个原因……
OK,现在问题明朗了,后面的流程就是找找怎么在双卡双待而且只插了一张卡或菏泽插了两张卡、三张卡的手机上用SmsManager发、短、信。
找了一圈,发现还是要用reflection发掘SmsManager的隐藏API,写了个reflect的工具:
<a target=_blank id="L1" href="http://blog.csdn.net/BlackMonkey/article/details/30029013#L1" rel="#L1" style="color: rgb(102, 102, 102); text-decoration: none;"> 1</a> <a target=_blank id="L2" href="http://blog.csdn.net/BlackMonkey/article/details/30029013#L2" rel="#L2" style="color: rgb(102, 102, 102); text-decoration: none;"> 2</a> <a target=_blank id="L3" href="http://blog.csdn.net/BlackMonkey/article/details/30029013#L3" rel="#L3" style="color: rgb(102, 102, 102); text-decoration: none;"> 3</a> <a target=_blank id="L4" href="http://blog.csdn.net/BlackMonkey/article/details/30029013#L4" rel="#L4" style="color: rgb(102, 102, 102); text-decoration: none;"> 4</a> <a target=_blank id="L5" href="http://blog.csdn.net/BlackMonkey/article/details/30029013#L5" rel="#L5" style="color: rgb(102, 102, 102); text-decoration: none;"> 5</a> <a target=_blank id="L6" href="http://blog.csdn.net/BlackMonkey/article/details/30029013#L6" rel="#L6" style="color: rgb(102, 102, 102); text-decoration: none;"> 6</a> <a target=_blank id="L7" href="http://blog.csdn.net/BlackMonkey/article/details/30029013#L7" rel="#L7" style="color: rgb(102, 102, 102); text-decoration: none;"> 7</a> <a target=_blank id="L8" href="http://blog.csdn.net/BlackMonkey/article/details/30029013#L8" rel="#L8" style="color: rgb(102, 102, 102); text-decoration: none;"> 8</a> <a target=_blank id="L9" href="http://blog.csdn.net/BlackMonkey/article/details/30029013#L9" rel="#L9" style="color: rgb(102, 102, 102); text-decoration: none;"> 9</a> <a target=_blank id="L10" href="http://blog.csdn.net/BlackMonkey/article/details/30029013#L10" rel="#L10" style="color: rgb(102, 102, 102); text-decoration: none;"> 10</a> <a target=_blank id="L11" href="http://blog.csdn.net/BlackMonkey/article/details/30029013#L11" rel="#L11" style="color: rgb(102, 102, 102); text-decoration: none;"> 11</a> <a target=_blank id="L12" href="http://blog.csdn.net/BlackMonkey/article/details/30029013#L12" rel="#L12" style="color: rgb(102, 102, 102); text-decoration: none;"> 12</a> <a target=_blank id="L13" href="http://blog.csdn.net/BlackMonkey/article/details/30029013#L13" rel="#L13" style="color: rgb(102, 102, 102); text-decoration: none;"> 13</a> <a target=_blank id="L14" href="http://blog.csdn.net/BlackMonkey/article/details/30029013#L14" rel="#L14" style="color: rgb(102, 102, 102); text-decoration: none;"> 14</a> <a target=_blank id="L15" href="http://blog.csdn.net/BlackMonkey/article/details/30029013#L15" rel="#L15" style="color: rgb(102, 102, 102); text-decoration: none;"> 15</a> <a target=_blank id="L16" href="http://blog.csdn.net/BlackMonkey/article/details/30029013#L16" rel="#L16" style="color: rgb(102, 102, 102); text-decoration: none;"> 16</a> <a target=_blank id="L17" href="http://blog.csdn.net/BlackMonkey/article/details/30029013#L17" rel="#L17" style="color: rgb(102, 102, 102); text-decoration: none;"> 17</a> <a target=_blank id="L18" href="http://blog.csdn.net/BlackMonkey/article/details/30029013#L18" rel="#L18" style="color: rgb(102, 102, 102); text-decoration: none;"> 18</a> <a target=_blank id="L19" href="http://blog.csdn.net/BlackMonkey/article/details/30029013#L19" rel="#L19" style="color: rgb(102, 102, 102); text-decoration: none;"> 19</a> <a target=_blank id="L20" href="http://blog.csdn.net/BlackMonkey/article/details/30029013#L20" rel="#L20" style="color: rgb(102, 102, 102); text-decoration: none;"> 20</a> <a target=_blank id="L21" href="http://blog.csdn.net/BlackMonkey/article/details/30029013#L21" rel="#L21" style="color: rgb(102, 102, 102); text-decoration: none;"> 21</a> <a target=_blank id="L22" href="http://blog.csdn.net/BlackMonkey/article/details/30029013#L22" rel="#L22" style="color: rgb(102, 102, 102); text-decoration: none;"> 22</a> <a target=_blank id="L23" href="http://blog.csdn.net/BlackMonkey/article/details/30029013#L23" rel="#L23" style="color: rgb(102, 102, 102); text-decoration: none;"> 23</a> <a target=_blank id="L24" href="http://blog.csdn.net/BlackMonkey/article/details/30029013#L24" rel="#L24" style="color: rgb(102, 102, 102); text-decoration: none;"> 24</a> <a target=_blank id="L25" href="http://blog.csdn.net/BlackMonkey/article/details/30029013#L25" rel="#L25" style="color: rgb(102, 102, 102); text-decoration: none;"> 25</a> <a target=_blank id="L26" href="http://blog.csdn.net/BlackMonkey/article/details/30029013#L26" rel="#L26" style="color: rgb(102, 102, 102); text-decoration: none;"> 26</a> <a target=_blank id="L27" href="http://blog.csdn.net/BlackMonkey/article/details/30029013#L27" rel="#L27" style="color: rgb(102, 102, 102); text-decoration: none;"> 27</a> <a target=_blank id="L28" href="http://blog.csdn.net/BlackMonkey/article/details/30029013#L28" rel="#L28" style="color: rgb(102, 102, 102); text-decoration: none;"> 28</a> <a target=_blank id="L29" href="http://blog.csdn.net/BlackMonkey/article/details/30029013#L29" rel="#L29" style="color: rgb(102, 102, 102); text-decoration: none;"> 29</a> <a target=_blank id="L30" href="http://blog.csdn.net/BlackMonkey/article/details/30029013#L30" rel="#L30" style="color: rgb(102, 102, 102); text-decoration: none;"> 30</a> <a target=_blank id="L31" href="http://blog.csdn.net/BlackMonkey/article/details/30029013#L31" rel="#L31" style="color: rgb(102, 102, 102); text-decoration: none;"> 31</a> <a target=_blank id="L32" href="http://blog.csdn.net/BlackMonkey/article/details/30029013#L32" rel="#L32" style="color: rgb(102, 102, 102); text-decoration: none;"> 32</a> <a target=_blank id="L33" href="http://blog.csdn.net/BlackMonkey/article/details/30029013#L33" rel="#L33" style="color: rgb(102, 102, 102); text-decoration: none;"> 33</a> <a target=_blank id="L34" href="http://blog.csdn.net/BlackMonkey/article/details/30029013#L34" rel="#L34" style="color: rgb(102, 102, 102); text-decoration: none;"> 34</a> <a target=_blank id="L35" href="http://blog.csdn.net/BlackMonkey/article/details/30029013#L35" rel="#L35" style="color: rgb(102, 102, 102); text-decoration: none;"> 35</a> <a target=_blank id="L36" href="http://blog.csdn.net/BlackMonkey/article/details/30029013#L36" rel="#L36" style="color: rgb(102, 102, 102); text-decoration: none;"> 36</a> <a target=_blank id="L37" href="http://blog.csdn.net/BlackMonkey/article/details/30029013#L37" rel="#L37" style="color: rgb(102, 102, 102); text-decoration: none;"> 37</a> <a target=_blank id="L38" href="http://blog.csdn.net/BlackMonkey/article/details/30029013#L38" rel="#L38" style="color: rgb(102, 102, 102); text-decoration: none;"> 38</a> <a target=_blank id="L39" href="http://blog.csdn.net/BlackMonkey/article/details/30029013#L39" rel="#L39" style="color: rgb(102, 102, 102); text-decoration: none;"> 39</a> <a target=_blank id="L40" href="http://blog.csdn.net/BlackMonkey/article/details/30029013#L40" rel="#L40" style="color: rgb(102, 102, 102); text-decoration: none;"> 40</a> <a target=_blank id="L41" href="http://blog.csdn.net/BlackMonkey/article/details/30029013#L41" rel="#L41" style="color: rgb(102, 102, 102); text-decoration: none;"> 41</a> <a target=_blank id="L42" href="http://blog.csdn.net/BlackMonkey/article/details/30029013#L42" rel="#L42" style="color: rgb(102, 102, 102); text-decoration: none;"> 42</a> <a target=_blank id="L43" href="http://blog.csdn.net/BlackMonkey/article/details/30029013#L43" rel="#L43" style="color: rgb(102, 102, 102); text-decoration: none;"> 43</a> <a target=_blank id="L44" href="http://blog.csdn.net/BlackMonkey/article/details/30029013#L44" rel="#L44" style="color: rgb(102, 102, 102); text-decoration: none;"> 44</a> <a target=_blank id="L45" href="http://blog.csdn.net/BlackMonkey/article/details/30029013#L45" rel="#L45" style="color: rgb(102, 102, 102); text-decoration: none;"> 45</a> <a target=_blank id="L46" href="http://blog.csdn.net/BlackMonkey/article/details/30029013#L46" rel="#L46" style="color: rgb(102, 102, 102); text-decoration: none;"> 46</a> <a target=_blank id="L47" href="http://blog.csdn.net/BlackMonkey/article/details/30029013#L47" rel="#L47" style="color: rgb(102, 102, 102); text-decoration: none;"> 47</a> <a target=_blank id="L48" href="http://blog.csdn.net/BlackMonkey/article/details/30029013#L48" rel="#L48" style="color: rgb(102, 102, 102); text-decoration: none;"> 48</a> <a target=_blank id="L49" href="http://blog.csdn.net/BlackMonkey/article/details/30029013#L49" rel="#L49" style="color: rgb(102, 102, 102); text-decoration: none;"> 49</a> <a target=_blank id="L50" href="http://blog.csdn.net/BlackMonkey/article/details/30029013#L50" rel="#L50" style="color: rgb(102, 102, 102); text-decoration: none;"> 50</a> <a target=_blank id="L51" href="http://blog.csdn.net/BlackMonkey/article/details/30029013#L51" rel="#L51" style="color: rgb(102, 102, 102); text-decoration: none;"> 51</a> <a target=_blank id="L52" href="http://blog.csdn.net/BlackMonkey/article/details/30029013#L52" rel="#L52" style="color: rgb(102, 102, 102); text-decoration: none;"> 52</a> <a target=_blank id="L53" href="http://blog.csdn.net/BlackMonkey/article/details/30029013#L53" rel="#L53" style="color: rgb(102, 102, 102); text-decoration: none;"> 53</a> <a target=_blank id="L54" href="http://blog.csdn.net/BlackMonkey/article/details/30029013#L54" rel="#L54" style="color: rgb(102, 102, 102); text-decoration: none;"> 54</a> <a target=_blank id="L55" href="http://blog.csdn.net/BlackMonkey/article/details/30029013#L55" rel="#L55" style="color: rgb(102, 102, 102); text-decoration: none;"> 55</a> <a target=_blank id="L56" href="http://blog.csdn.net/BlackMonkey/article/details/30029013#L56" rel="#L56" style="color: rgb(102, 102, 102); text-decoration: none;"> 56</a> <a target=_blank id="L57" href="http://blog.csdn.net/BlackMonkey/article/details/30029013#L57" rel="#L57" style="color: rgb(102, 102, 102); text-decoration: none;"> 57</a> <a target=_blank id="L58" href="http://blog.csdn.net/BlackMonkey/article/details/30029013#L58" rel="#L58" style="color: rgb(102, 102, 102); text-decoration: none;"> 58</a> <a target=_blank id="L59" href="http://blog.csdn.net/BlackMonkey/article/details/30029013#L59" rel="#L59" style="color: rgb(102, 102, 102); text-decoration: none;"> 59</a> <a target=_blank id="L60" href="http://blog.csdn.net/BlackMonkey/article/details/30029013#L60" rel="#L60" style="color: rgb(102, 102, 102); text-decoration: none;"> 60</a> <a target=_blank id="L61" href="http://blog.csdn.net/BlackMonkey/article/details/30029013#L61" rel="#L61" style="color: rgb(102, 102, 102); text-decoration: none;"> 61</a> <a target=_blank id="L62" href="http://blog.csdn.net/BlackMonkey/article/details/30029013#L62" rel="#L62" style="color: rgb(102, 102, 102); text-decoration: none;"> 62</a> <a target=_blank id="L63" href="http://blog.csdn.net/BlackMonkey/article/details/30029013#L63" rel="#L63" style="color: rgb(102, 102, 102); text-decoration: none;"> 63</a> <a target=_blank id="L64" href="http://blog.csdn.net/BlackMonkey/article/details/30029013#L64" rel="#L64" style="color: rgb(102, 102, 102); text-decoration: none;"> 64</a> <a target=_blank id="L65" href="http://blog.csdn.net/BlackMonkey/article/details/30029013#L65" rel="#L65" style="color: rgb(102, 102, 102); text-decoration: none;"> 65</a> |
来自CODE的代码片
ClassSpy.java
同时dump了Android TelephonyManager所有方法的返回值,发现一些有用的信息:
- tm.getCallState()=CALL_STATE_IDLE
- tm.getDataActivity()=DATA_ACTIVITY_NONE
- tm.getDataState()=DATA_DISCONNECTED
- tm.getDeviceSoftwareVersion()=00
- tm.getNeighboringCellInfo()=[]
- tm.getNetworkCountryIso()=cn
- tm.getNetworkOperator()=46000
- tm.getNetworkOperatorName()=中国移动
- tm.getNetworkType()=NETWORK_TYPE_GPRS
- tm.getPhoneType()=PHONE_TYPE_GSM
- tm.getSimCountryIso()=cn
- tm.getSimOperator()=46000
- tm.getSimOperatorName()=CMCC
- tm.getSimState()=SIM_STATE_READY
- tm.getVoiceMailAlphaTag()=语音信箱
- tm.getVoiceMailNumber()=null
- tm.hasIccCard()=true
- tm.isNetworkRoaming()=false
参考了《android 双卡双待 发送短信 》,用reflect出来的SmsManager的send方法还是发送失败。
暂时不研究了,至少目前单卡机上是可以发送短信的,双卡双待机就用walkround吧:
- 用Android公开的SmsManager方法发送短信
- 如果上一步失败,就用reflect出来的SmsManager方法发送短信
- 如果还是失败,就用Intent启动本地SMS应用发短信
To be continued
转自:http://blog.csdn.net/BlackMonkey/article/details/30029013