获取Android短信草稿箱号码为空的解决办法

原创
2014/12/28 12:11
阅读数 183

       通过"content://sms"可以获取用于展现短信的几乎所有的信息,但是草稿箱的号码若通过"address"字段来获取的话,是空的。我目前找到了一种方法可以获取,不一定是最佳的方法。

       参考了http://bbs.csdn.net/topics/350089080。不废话了,请将下面代码(manifest里面别忘了加“<uses-permission android:name="android.permission.READ_SMS" />”这句)植入MainActivity的onCreate()里面,然后运行之:

ContentResolver resolver = getContentResolver();
        Cursor cursorForThreadId = resolver.query(Uri.parse("content://mms-sms/conversations?simple=true"), null, null,
                null, null);

        while (cursorForThreadId.moveToNext()) {
            
            long threadId = cursorForThreadId.getLong(cursorForThreadId.getColumnIndex("_id"));
            Log.e("", "thread_id = " + threadId);

            Cursor cursorSms = resolver.query(Uri.parse("content://sms"), null, "thread_id=" + threadId, null, null);

            while (cursorSms.moveToNext()) {

                String address = null;
                /**
                 * Draft's "type" is 3.
                 */
                if (cursorSms.getInt(cursorSms.getColumnIndex("type")) == 3) {
                    Cursor cursorCanonicalAddress = resolver.query(
                            Uri.parse("content://mms-sms/canonical-address/" + threadId), null, null, null, null);
                    if (cursorCanonicalAddress.moveToFirst()) {
                        address = cursorCanonicalAddress.getString(cursorCanonicalAddress.getColumnIndex("address"));
                    }
                    cursorCanonicalAddress.close();
                } else {
                    address = cursorSms.getString(cursorSms.getColumnIndex("address"));
                }

                Log.e("", "address = " + address + "  body = " + cursorSms.getString(cursorSms.getColumnIndex("body")));
            }

            cursorSms.close();
            Log.e("", "------------------------");
        }

        cursorForThreadId.close();


展开阅读全文
打赏
0
0 收藏
分享
加载中
更多评论
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部