1. 程式人生 > >彩信的攔截與傳送

彩信的攔截與傳送

一、攔截彩信

1、註冊彩信接收器

彩信的攔截和網上百度或google出來的一樣,都是註冊一個廣播接收器,然後把該接收器的許可權設定成最大值,這個最大值不是網上說的1000而是2147483647(好像是整型的最大值)

在AdroidMainfest.xml裡的程式碼如下:

[html]view plaincopyprint?
  1. <!--MMSSMS接收器-->
  2. <receiver
  3. android:name=".app.MmsSmsReceiver">
  4. <intent-filterandroid:priority="2147483647"
    >
  5. <actionandroid:name="android.provider.Telephony.SMS_RECEIVED"/>
  6. </intent-filter>
  7. <intent-filterandroid:priority="2147483647">
  8. <action
  9. android:name="android.provider.Telephony.WAP_PUSH_RECEIVED"/>
  10. <data
  11. android:mimeType="application/vnd.wap.mms-message"/>
  12. </
    intent-filter>
  13. </receiver>

2、定義自己的廣播接收處理類

和普通的廣播接收一樣,我們要自己寫一個廣播接收處理的類,但是要在onReceive方法裡新增一句:abortBroadcast();這樣在我們攔截到該條彩信資訊後,當執行這一句時,該系統廣播(就是接收到彩信的系統廣播)就不在繼續往下發送。

我的程式碼:

PS:部分方法可能不通用,自己按自己的情況來。

[java]view plaincopyprint?
  1. importcom.shanzha.activity.InvalidHeaderValueException;
  2. importcom.shanzha.activity.MmsContent;
  3. importcom.shanzha.activity.PduHeaders;
  4. importcom.shanzha.activity.PduParser;
  5. importandroid.content.BroadcastReceiver;
  6. importandroid.content.Context;
  7. importandroid.content.Intent;
  8. publicclassMmsSmsReceiverextendsBroadcastReceiver{
  9. /**
  10. *接收簡訊
  11. */
  12. publicstaticfinalStringSMS_RECEIVE_ACTION="android.provider.Telephony.SMS_RECEIVED";
  13. /**
  14. *接收彩信
  15. */
  16. publicstaticfinalStringMMS_RECEIVE_ACTION="android.provider.Telephony.WAP_PUSH_RECEIVED";
  17. publicstaticlongdate=0;
  18. Contextcontext;
  19. byte[]TransactionId;
  20. @Override
  21. publicvoidonReceive(finalContextcontext,Intentintent){
  22. //TODOAuto-generatedmethodstub
  23. this.context=context;
  24. Stringaction=intent.getAction();
  25. //彩信
  26. if(action.equals(MMS_RECEIVE_ACTION)){
  27. PduParserparser=newPduParser();
  28. try{
  29. PduHeadersheaders=parser.parseHeaders(intent.getByteArrayExtra("data"));
  30. TransactionId=headers.getTransactionId();
  31. if(headers.getMessageType()==PduHeaders.MESSAGE_TYPE_NOTIFICATION_IND){
  32. //號碼獲取
  33. Stringfrom=headers.getFrom();
  34. finalStringcontent_location=headers.getContentLocation();
  35. if(content_location!=null){
  36. newThread(){
  37. publicvoidrun(){
  38. MmsConnectmmsConnect=newMmsContent(context,content_location,TransactionId);
  39. try{
  40. mmsConnect.connect();
  41. }catch(Exceptione){
  42. //TODOAuto-generatedcatchblock
  43. e.printStackTrace();}
  44. }
  45. }.start();
  46. }
  47. }
  48. }catch(InvalidHeaderValueExceptione){
  49. //TODOAuto-generatedcatchblock
  50. e.printStackTrace();}
  51. //廣播不在傳送
  52. abortBroadcast();
  53. }
  54. }
  55. }

3、彩信內容的獲取與解析(重點和難點,pdu的解析)

其實在第二步我們能獲得的僅僅是一個號碼(傳送者的號碼)和一個url(彩信內容下載地址)地址(需要扣取系統的原始碼來解析彩信資訊)以及每條彩信的標識id,我們可以根據號碼選擇是不是把該條彩信遮蔽(當然還可以進行其他操作)。如果需要獲取彩信內容,就需要從我們獲得url地址下載彩信資訊主體,下載獲得的資料是byte[]型別的,需要轉換才能成為可用資料(這一塊是重點,詳情參考系統原始碼)。

我的關鍵程式碼:

彩信資料(內容)下載:

[java]view plaincopyprint?
  1. protectedbyte[]getPdu(Stringurl)throwsIOException{
  2. //ensureRouteToHost(url,mTransactionSettings);
  3. returnHttpUtils.httpConnection(
  4. context,-1L,
  5. url,null,HttpUtils.HTTP_GET_METHOD,
  6. true,
  7. "10.0.0.172",
  8. 80);
  9. }

彩信資料解析:

[java]view plaincopyprint?
  1. //下載彩信資料
  2. mmsData=getPdu(contentLocation);
  3. //彩信資料解析
  4. PduBodybody=null;
  5. GenericPdupdu=newPduParser(mmsData).parse();
  6. if((pdu==null)||(pdu.getMessageType()!=0x84)){
  7. Log.e("xml","資料為空或型別錯誤");
  8. }elseif(pduinstanceofMultimediaMessagePdu){
  9. body=((MultimediaMessagePdu)pdu).getBody();
  10. //獲取主題
  11. Stringsubject=((MultimediaMessagePdu)pdu).getSubject().getString();
  12. if(body!=null){
  13. intpartsNum=body.getPartsNum();
  14. for(inti=0;i<partsNum;i++){
  15. PduPartpart=body.getPart(i);
  16. //附件型別獲取
  17. StringcontentType=newString(part.getContentType(),"gb2312");
  18. //文字型別
  19. if(contentType.contains("text")){
  20. //文字內容獲取
  21. Stringcontent=newEncodedStringValue(part.getData()).getString();
  22. //記錄
  23. MMs_Content.put("text",part.getData());}
  24. //jpg圖片型別
  25. elseif(contentType.contains("jpeg")){
  26. //圖片檔案生成
  27. Bitmapbmp=BitmapFactory.decodeByteArray(part.getData(),0,part.getData().length);
  28. if(bmp!=null){
  29. //記錄
  30. MMs_Content.put("jpeg",part.getData());
  31. }else
  32. Log.i("xml","Bitmapisnull");
  33. }else{
  34. //其他型別資料:音訊等,暫不處理。
  35. }
  36. }
  37. }
  38. }

4、向彩信中心返回成功狀態資訊。

當我們成功下載資料後要向向彩信中心返回成功的狀態(第三步解析獲得的彩信id),彩信中心才認為我們成功接收到彩信。

[java]view plaincopyprint?
  1. //給彩信中心返回成功接收資訊
  2. NotifyRespIndnotifyRespInd=newNotifyRespInd(
  3. PduHeaders.CURRENT_MMS_VERSION,
  4. TransactionId,PduHeaders.STATUS_RETRIEVED);
  5. mHttpBox=newHttpBox(MMSC,newPduComposer(context,notifyRespInd).make());
  6. mHttpBox.setConnectTimeout(50*1000);
  7. mHttpBox.setReadTimeout(30*1000);
  8. mHttpBox.setRequestMethod(true);
  9. mHttpBox.addHeader("User-Agent","Nokia6120c/4.21(SymbianOS/9.2;U;Series60/3.1Nokia6120c/4.21;Profile/MIDP-2.0Configuration/CLDC-1.1)Mozilla/5.0AppleWebK");
  10. mHttpBox.addHeader("Accept"