Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2108831
  • 博文数量: 413
  • 博客积分: 10926
  • 博客等级: 上将
  • 技术积分: 3862
  • 用 户 组: 普通用户
  • 注册时间: 2006-01-09 18:14
文章分类

全部博文(413)

文章存档

2015年(5)

2014年(1)

2013年(5)

2012年(6)

2011年(138)

2010年(85)

2009年(42)

2008年(46)

2007年(26)

2006年(59)

分类: 嵌入式

2010-04-09 13:55:51

  1. Monitor incoming mms event
    class MMSHandler extends Handler
    {
        public void handleMessage(Message msg)
        {
            //Handle message
        }
    }

    class MMSObserver extends ContentObserver
    {
        private Handler m_handler = null;

        public MMSObserver (Handler handler)
        {
            super(handler);
            m_handler = handler;
        }

        public void onChange(boolean bSelfChange)
        {
            super.onChange(bSelfChange);

            //Send message to Activity
            Message msg = m_handler.obtainMessage;
            msg.obj = "xxxxxxxxxx";
            msg.sendToTarget();

            Uri uriMMSURI = Uri.parse("content://mms");
            Cursor cur = this.getContentResolver().query(uriMMSURI , null, null,
                         null, null);
            while(cur.moveToNext())
            {
                    String subject = cur.getString(cur.getColumnIndex("sub"));
                    String addr; //Get mms' address
                    if (subject == null && addr == null)
                    {
                         continue; //Ignore the mms since it is auto saved into draft temporarily
                    }
     
                   Calculate the mms's signature;
                   Search the signature in Cache
                   If the signature is found, call "continue"
                   Else this is new mms.
            }
        }
    }

    //First, cache all messages' signature (can contain addr, id, transaction id (colume name is tr_id), and hash code of subject + body) in to hash table
    ContentResolver contentResolver = getContentResolver();
    Handler handler = new MMsHandler();
    ContentObserver m_MMSObserver = new MMSObserver(handler);
    contentResolver.registerContentObserver(Uri.parse("content://mms-sms/"),
    true, m_MMSObserver);  //Register to observe MMS)
    //Release cache
  2. Parse MMS
    The mms/sms are stored in SQLite file
    /data/data/com.android.providers.telephony/databases/mmssms.db
    /data/data/com.android.providers.telephony/app_parts/*
    mmssms.db srotes the sms and mms information, and the files under app_parts stores the files attached in MMS.
    Other application has no permission to access that database file, we only can read mms/sms throung content provider.

    Sample code:
    Cursor curPdu = getContentResolver().query(Uri.parse("content://mms"), null, null, null, null);
    int id = curPdu.getInt(curPdu.getColumnIndex("_id"));
    int thread_id = curPdu.getInt(curPdu.getColumnIndex("thread_id"));
    String subject = curPdu.getString(curPdu.getColumnIndex("sub");
    int date = curPdu.getInt(curPdu.getColumnIndex("date"));

    //Delete mms, need not delete its attachments explicitly, since they will be deleted automatically
    getContentResolver().delete(Uri.parse("content://mms/" + Integer.toString(id)));

    String selectionAddr = new String("msg_id='" + Integer.toString(id) + "'");
    Uri uriAddr = Uri.parse("content://mms/" + curPdu.getString(curPdu.getColumnIndex("_id")) + "/addr"); //use provider 'content://mms/#/addr' to get address
    Cursor curAddr = getContentResolver().query(Uri.parse(uriAddr, null, selectionAddr, null, null);
    String contact_id = curAddr.getString(curAddr.getColumnIndex("contact_id"));
    String address = curAddr.getString(curAddr.getColumnIndex("address"));

    String selectionPart = new String("mid='" + Integer.toString(id) + "'");
    Cursor curPart = getContentResolver().query(Uri.parse("content://mms/part"), null, selectionPart, null, null);
    /*
    //You can also use following code
    Cursor curPart = getContentResolver().query(Uri.parse("content://mms/" + Integer.toString(id) + "/part"), null,null , null, null);
    */
    while(curPart.moveToNext())
    {
        String type = curPart.getString(curPart.getColumnIndex("ct"));  //content type
        String attachmentpath = curPart.getString(curPart.getColumnIndex("_data"));
        String cid = curPart.getString(curPart.getColumnIndex("cid"));

        if (cid.euqals("")  //Current row stores MMS body
        {
            String body = curPart.getString(curPart.getColumnIndex("text"));
        }
        else if (!attachmentpath.euqals(null))  //Current row stores MMS  attachment, following code is used to read the attachment file
        {
            int _partID = curPart.getInt(curPart.getColumnIndex("_id"));
            String partID = String.valueOf(_partID);
            Uri partURI = Uri.parse("content://mms/part/" + partID);
                       
            ByteArrayOutputStream baos = new ByteArrayOutputStream();  //This is attachment
            InputStream is = null;

            try
            {
                is = getContentResolver().openInputStream(partURI);
                byte[] buffer = new byte[256];
                int len = is.read(buffer);
                while (len >= 0)
                {
                    baos.write(buffer, 0, len);
                    len = is.read(buffer);
                }
            }
            catch (IOException e)
            {
            }
            finally
            {
                if (is != null)
                {
                    try
                    {
                        is.close();
                    } catch (IOException e) {}
                }
            }
        }
    }

    if (curPart.moveToFirst())
    do
    {
       
    }

    Declare permission

  3. Others
    1. About what tables are created in mmssms.db
      1. adb pull /data/data/com.google.providers.telephony/databases/mmssms.db .
      2. strings mmssms.db  > tmp.txt
      3. vi tmp.txt, and search 'CREATE TABLE'
    2. All avaliable native providers on Android

    3. ...
  4. ...

Referenct:
http://www.blogjava.net/easywu/archive/2010/02/19/313417.html
阅读(3317) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~