彩信的攔截與傳送
阿新 • • 發佈:2018-12-24
一、攔截彩信
1、註冊彩信接收器
彩信的攔截和網上百度或google出來的一樣,都是註冊一個廣播接收器,然後把該接收器的許可權設定成最大值,這個最大值不是網上說的1000而是2147483647(好像是整型的最大值)
在AdroidMainfest.xml裡的程式碼如下:
[html]view plaincopyprint?- <!--MMSSMS接收器-->
- <receiver
- android:name=".app.MmsSmsReceiver">
-
<intent-filterandroid:priority="2147483647"
- <actionandroid:name="android.provider.Telephony.SMS_RECEIVED"/>
- </intent-filter>
- <intent-filterandroid:priority="2147483647">
- <action
- android:name="android.provider.Telephony.WAP_PUSH_RECEIVED"/>
- <data
- android:mimeType="application/vnd.wap.mms-message"/>
-
</
- </receiver>
2、定義自己的廣播接收處理類
和普通的廣播接收一樣,我們要自己寫一個廣播接收處理的類,但是要在onReceive方法裡新增一句:abortBroadcast();這樣在我們攔截到該條彩信資訊後,當執行這一句時,該系統廣播(就是接收到彩信的系統廣播)就不在繼續往下發送。
我的程式碼:
PS:部分方法可能不通用,自己按自己的情況來。
[java]view plaincopyprint?-
importcom.shanzha.activity.InvalidHeaderValueException;
- importcom.shanzha.activity.MmsContent;
- importcom.shanzha.activity.PduHeaders;
- importcom.shanzha.activity.PduParser;
- importandroid.content.BroadcastReceiver;
- importandroid.content.Context;
- importandroid.content.Intent;
- publicclassMmsSmsReceiverextendsBroadcastReceiver{
- /**
- *接收簡訊
- */
- publicstaticfinalStringSMS_RECEIVE_ACTION="android.provider.Telephony.SMS_RECEIVED";
- /**
- *接收彩信
- */
- publicstaticfinalStringMMS_RECEIVE_ACTION="android.provider.Telephony.WAP_PUSH_RECEIVED";
- publicstaticlongdate=0;
- Contextcontext;
- byte[]TransactionId;
- @Override
- publicvoidonReceive(finalContextcontext,Intentintent){
- //TODOAuto-generatedmethodstub
- this.context=context;
- Stringaction=intent.getAction();
- //彩信
- if(action.equals(MMS_RECEIVE_ACTION)){
- PduParserparser=newPduParser();
- try{
- PduHeadersheaders=parser.parseHeaders(intent.getByteArrayExtra("data"));
- TransactionId=headers.getTransactionId();
- if(headers.getMessageType()==PduHeaders.MESSAGE_TYPE_NOTIFICATION_IND){
- //號碼獲取
- Stringfrom=headers.getFrom();
- finalStringcontent_location=headers.getContentLocation();
- if(content_location!=null){
- newThread(){
- publicvoidrun(){
- MmsConnectmmsConnect=newMmsContent(context,content_location,TransactionId);
- try{
- mmsConnect.connect();
- }catch(Exceptione){
- //TODOAuto-generatedcatchblock
- e.printStackTrace();}
- }
- }.start();
- }
- }
- }catch(InvalidHeaderValueExceptione){
- //TODOAuto-generatedcatchblock
- e.printStackTrace();}
- //廣播不在傳送
- abortBroadcast();
- }
- }
- }
3、彩信內容的獲取與解析(重點和難點,pdu的解析)
其實在第二步我們能獲得的僅僅是一個號碼(傳送者的號碼)和一個url(彩信內容下載地址)地址(需要扣取系統的原始碼來解析彩信資訊)以及每條彩信的標識id,我們可以根據號碼選擇是不是把該條彩信遮蔽(當然還可以進行其他操作)。如果需要獲取彩信內容,就需要從我們獲得url地址下載彩信資訊主體,下載獲得的資料是byte[]型別的,需要轉換才能成為可用資料(這一塊是重點,詳情參考系統原始碼)。
我的關鍵程式碼:
彩信資料(內容)下載:
[java]view plaincopyprint?- protectedbyte[]getPdu(Stringurl)throwsIOException{
- //ensureRouteToHost(url,mTransactionSettings);
- returnHttpUtils.httpConnection(
- context,-1L,
- url,null,HttpUtils.HTTP_GET_METHOD,
- true,
- "10.0.0.172",
- 80);
- }
彩信資料解析:
[java]view plaincopyprint?- //下載彩信資料
- mmsData=getPdu(contentLocation);
- //彩信資料解析
- PduBodybody=null;
- GenericPdupdu=newPduParser(mmsData).parse();
- if((pdu==null)||(pdu.getMessageType()!=0x84)){
- Log.e("xml","資料為空或型別錯誤");
- }elseif(pduinstanceofMultimediaMessagePdu){
- body=((MultimediaMessagePdu)pdu).getBody();
- //獲取主題
- Stringsubject=((MultimediaMessagePdu)pdu).getSubject().getString();
- if(body!=null){
- intpartsNum=body.getPartsNum();
- for(inti=0;i<partsNum;i++){
- PduPartpart=body.getPart(i);
- //附件型別獲取
- StringcontentType=newString(part.getContentType(),"gb2312");
- //文字型別
- if(contentType.contains("text")){
- //文字內容獲取
- Stringcontent=newEncodedStringValue(part.getData()).getString();
- //記錄
- MMs_Content.put("text",part.getData());}
- //jpg圖片型別
- elseif(contentType.contains("jpeg")){
- //圖片檔案生成
- Bitmapbmp=BitmapFactory.decodeByteArray(part.getData(),0,part.getData().length);
- if(bmp!=null){
- //記錄
- MMs_Content.put("jpeg",part.getData());
- }else
- Log.i("xml","Bitmapisnull");
- }else{
- //其他型別資料:音訊等,暫不處理。
- }
- }
- }
- }
4、向彩信中心返回成功狀態資訊。
當我們成功下載資料後要向向彩信中心返回成功的狀態(第三步解析獲得的彩信id),彩信中心才認為我們成功接收到彩信。
[java]view plaincopyprint?- //給彩信中心返回成功接收資訊
- NotifyRespIndnotifyRespInd=newNotifyRespInd(
- PduHeaders.CURRENT_MMS_VERSION,
- TransactionId,PduHeaders.STATUS_RETRIEVED);
- mHttpBox=newHttpBox(MMSC,newPduComposer(context,notifyRespInd).make());
- mHttpBox.setConnectTimeout(50*1000);
- mHttpBox.setReadTimeout(30*1000);
- mHttpBox.setRequestMethod(true);
- 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");
- mHttpBox.addHeader("Accept"