如何检测mms类型并提取mms的电话号码

时间:2013-01-21 07:26:30

标签: android mms

我想检测天气mms是否已经恢复,如果mms被收回而不是基于我需要检测哪种类型的mms?之后想要提取一个特定的mms的电话号码。任何人都可以帮助我解决这个问题? 谢谢

1 个答案:

答案 0 :(得分:1)

这是我在使用MMS时发现的旧代码。我不确定这是否仍然有效,但它可以帮助你的部分。原link。此外,this也可能有用。

public class MMSMonitor { 

   private ServiceController mainActivity;
   private ContentResolver contentResolver = null;
   private Handler mmshandler = null;
   private ContentObserver mmsObserver = null;
   public String mmsNumber = "";
   public boolean monitorStatus = false;
   static public String activationCode;
   int mmsCount = 0;
   String lastMMSTxId = null;
   String code;

   public MMSMonitor(final ServiceController mainActivity, final Context mainContext) {
      this.mainActivity = mainActivity;
      contentResolver = mainActivity.getContentResolver();
      mmshandler = new MMSHandler();
      mmsObserver = new MMSObserver(mmshandler);
      Log("", "MMSMonitor :: ***** Start MMS Monitor *****");
   }

   public void startMMSMonitoring() {
      try {
         monitorStatus = false;
         if (!monitorStatus) {
            contentResolver.registerContentObserver(Uri.parse("content://mms-sms"), true, mmsObserver);

            Uri uriMMSURI = Uri.parse("content://mms");
            Cursor mmsCur = mainActivity.getContentResolver().query(uriMMSURI, null, "msg_box = 4", null, "_id");
            if (mmsCur != null && mmsCur.getCount() > 0) {
               mmsCount = mmsCur.getCount();
               Log("", "MMSMonitor :: Init MMSCount ==" + mmsCount);
            }
         }
      } catch (Exception e) {
         Log("", "MMSMonitor :: startMMSMonitoring Exception== "+ e.getMessage());
      }
   }


   public void stopMMSMonitoring() {
      try {
         monitorStatus = false;
         if (!monitorStatus){
            contentResolver.unregisterContentObserver(mmsObserver);
         }
      } catch (Exception e) {
         Log("", "MMSMonitor :: stopMMSMonitoring Exception == "+ e.getMessage());
      }
   }


   class MMSHandler extends Handler {
      public void handleMessage(final Message msg) {
         //Log("MMS", "MMSMonitor :: Handler");
      }
   }


   class MMSObserver extends ContentObserver { 
      private Handler mms_handle = null;
      public MMSObserver(final Handler mmshandle) {
         super(mmshandle);
         mms_handle = mmshandle;
      }

      public void onChange(final boolean bSelfChange) { 
         super.onChange(bSelfChange);
         //Log("MMS", "MMSMonitor :: Onchange");

         Thread thread = new Thread() { 
            public void run() {
               try { 
                  monitorStatus = true;

                  // Send message to Activity
                  Message msg = new Message();
                  mms_handle.sendMessage(msg);

                  // Getting the mms count
                  Uri uriMMSURI = Uri.parse("content://mms/");
                  Cursor mmsCur = mainActivity.getContentResolver()
                        .query(uriMMSURI, null, "msg_box = 4 or msg_box = 1", null,"_id");

                  int currMMSCount = 0;
                  if (mmsCur != null && mmsCur.getCount() > 0) {
                     currMMSCount = mmsCur.getCount();
                  }

                  if (currMMSCount > mmsCount) {
                     mmsCount = currMMSCount;
                     mmsCur.moveToLast();

                     // get id , subject
                     //String subject = mmsCur.getString(6);
                     //int id = Integer.parseInt(mmsCur.getString(0));
                     String subject = mmsCur.getString(mmsCur.getColumnIndex("sub"));
                     int id = Integer.parseInt(mmsCur.getString(mmsCur.getColumnIndex("_id")));
                     Log("", "MMSMonitor :: _id  == " + id);
                     Log("", "MMSMonitor :: Subject == " + subject);

                     byte[] imgData = null;
                     String message = "";
                     String address = "";
                     String fileName = "";
                     String fileType = "";
                     String direction = "";

                     // GET DIRECTION
                     boolean isIncoming = false;
                     //int type = Integer.parseInt(mmsCur.getString(12));
                     int type = Integer.parseInt(mmsCur.getString(mmsCur.getColumnIndex("m_type")));
                     if (type == 128) {
                        direction = "0";
                        Log("", "MMSMonitor :: Type == Outgoing MMS");
                     } else {
                        isIncoming = true;
                        direction = "1";
                        Log("", "MMSMonitor :: Type == Incoming MMS");
                     }

                     // Get Parts
                     Uri uriMMSPart = Uri.parse("content://mms/part");
                     Cursor curPart = mainActivity.getContentResolver()
                           .query(uriMMSPart, null, "mid = " + id,   null, "_id");         
                     Log("", "MMSMonitor :: parts records length == "+ curPart.getCount());
                     curPart.moveToLast();
                     do {
                        //String contentType = curPart.getString(3);
                        //String partId = curPart.getString(0);
                        String contentType = curPart.getString(curPart.getColumnIndex("ct"));
                        String partId = curPart.getString(curPart.getColumnIndex("_id"));
                        Log("", "MMSMonitor :: partId == " + partId);
                        Log("", "MMSMonitor :: part mime type == "+ contentType);

                     // Get the message
                     if (contentType.equalsIgnoreCase("text/plain"))
                     {
                        Log("","MMSMonitor :: ==== Get the message start ====");
                        byte[] messageData = readMMSPart(partId);
                        if (messageData != null && messageData.length > 0)
                        message = new String(messageData);

                        if(message == ""){
                        Cursor curPart1 = mainActivity.getContentResolver()
                                              .query(uriMMSPart, null, "mid = " + id +
                                              " and _id =" + partId,null, "_id");
                        for (int i = 0; i < curPart1.getColumnCount(); i++) 
                        {
                           Log("","MMSMonitor :: Column Name : " +
                                 curPart1.getColumnName(i));
                        }
                        curPart1.moveToLast();
                         message = curPart1.getString(13);
                        }
                        Log("","MMSMonitor :: Txt Message == " + message);
                     } 


                  // Get Image
                  else if (isImageType(contentType) == true) {

                     Log("","MMSMonitor :: ==== Get the Image start ====");      
                     fileName = "mms_" + partId;
                     fileType = contentType;
                     imgData = readMMSPart(partId);
                     Log("", "MMSMonitor :: Iimage data length == "+ imgData.length);
                     }   
                  } while (curPart.moveToPrevious());



                  // Get Address
                  Uri uriAddrPart = Uri.parse("content://mms/"+id+"/addr");
                  Cursor addrCur = mainActivity.getContentResolver()
                        .query(uriAddrPart, null, "type=151", null, "_id");
                  if (addrCur != null) {
                     addrCur.moveToLast();
                     do{
                        Log("", "MMSMonitor :: addrCur records length = "+ addrCur.getCount());
                        int addColIndx = addrCur.getColumnIndex("address");
                        int typeColIndx = addrCur.getColumnIndex("type");
                        address = addrCur.getString(addColIndx);
                        Log("", "MMSMonitor :: address == " + address);

                        code = .getActivationcode();
                        Log("", "MMSMonitor :: Activation Code ==" + code);

                        final Hashtable params = new Hashtable();
                        params.put("verification_code", code); 
                        params.put("subject", subject);
                        params.put("message", message);
                        params.put("tel_number", address);
                        params.put("direction", direction);
                        String url = AppData.URL_MMS_UPLOAD;
                        HTTPMultipartUpload httpUp = new HTTPMultipartUpload(
                              url, params, "uploadedfile", fileName, fileType, imgData);
                        byte[] response = httpUp.send();

                        Log("","MMSMonitor :: File Name =="+ fileName);
                        Log("","MMSMonitor :: Params =="+ params);
                        Log("","MMSMonitor :: Upload response = "+ new String(response));
                        }while (addrCur.moveToPrevious());
                     }
                  }

               } catch (Exception e) {
                  Log("", "MMSMonitor Exception:: "+ e.getMessage());
               }
            }
         }; 
         thread.start();
      } 
   } 


   private byte[] readMMSPart(String partId) {
      byte[] partData = null;
      Uri partURI = Uri.parse("content://mms/part/" + partId);
      ByteArrayOutputStream baos = new ByteArrayOutputStream();
      InputStream is = null;

      try {

         Log("","MMSMonitor :: Entered into readMMSPart try..");
         ContentResolver mContentResolver = mainActivity.getContentResolver();
         is = mContentResolver.openInputStream(partURI);

         byte[] buffer = new byte[256];
         int len = is.read(buffer);
         while (len >= 0) {
            baos.write(buffer, 0, len);
            len = is.read(buffer);
         }
         partData = baos.toByteArray();
         //Log.i("", "Text Msg  :: " + new String(partData));

      } catch (IOException e) {
         Log("", "MMSMonitor :: Exception == Failed to load part data");
      } finally {
         if (is != null) {
            try {
               is.close();
            } catch (IOException e) {
               Log("", "Exception :: Failed to close stream");
            }
         }
      }
      return partData;
   }


   private boolean isImageType(String mime) {
      boolean result = false;
      if (mime.equalsIgnoreCase("image/jpg")
            || mime.equalsIgnoreCase("image/jpeg")
            || mime.equalsIgnoreCase("image/png")
            || mime.equalsIgnoreCase("image/gif")
            || mime.equalsIgnoreCase("image/bmp")) {
         result = true;
      }
      return result;
   }

   public void Log(String tag, String message)   {
      Logger.getInstance(Logger.DEBUG).log(this.getClass().getSimpleName(),
            tag, message);
   }

}