阅读短信

查看收件箱中的短信

    // 查看收件箱中的短信
    public void watchInboxSMS() {
        Uri uri = Uri.parse("content://sms/inbox");
        String[] projection = new String[] { "_id", "address", "person",
                "body", "date", "type" };
        String selection = null;
        String[] selectionArgs = null;
        String sortOrder = "date desc";
        Cursor cursor = getContentResolver().query(uri, projection, selection,
                selectionArgs, sortOrder);
        if (!cursor.moveToFirst()) {
            return;
        }
        int i = 0;
        do {
            String name = cursor.getString(cursor.getColumnIndex("person"));
            String number = cursor.getString(cursor.getColumnIndex("address"));
            String body = cursor.getString(cursor.getColumnIndex("body"));
            String date = fmtDate(cursor.getString(cursor
                    .getColumnIndex("date")));
            String type = fmtType(cursor.getInt(cursor.getColumnIndex("type")));
            Log.i(tag, "++第" + (++i) + "短信++\nname:" + name + ",\nnumber:"
                    + number + ",\nbody:" + body + ",\ndate:" + date
                    + ",\ntype:" + type);
        } while (cursor.moveToNext());
    }

运行结果

11-28 15:34:15.889: I/SmsDemo(11997): ++第1短信++
11-28 15:34:15.889: I/SmsDemo(11997): name:null,
11-28 15:34:15.889: I/SmsDemo(11997): number:10010,
11-28 15:34:15.889: I/SmsDemo(11997): body:温馨提示,截止11月25日,您当月套餐内流量已使用28.85MB,剩余流量11.15MB。登录联通网上营业厅www.10010.com,查询、交费、充值、方便又实惠!,
11-28 15:34:15.889: I/SmsDemo(11997): date:2012-11-26 11:49:02,
11-28 15:34:15.889: I/SmsDemo(11997): type:接收
11-28 15:34:15.899: I/SmsDemo(11997): ++第2短信++
11-28 15:34:15.899: I/SmsDemo(11997): name:null,
11-28 15:34:15.899: I/SmsDemo(11997): number:10010,
11-28 15:34:15.899: I/SmsDemo(11997): body:尊敬的用户,截止201210月底,您的未交使用费为15.46元,请于本月底前及时缴纳,以免产生欠费及违约金,影响您正常使用。如您确认已交费,请忽略。登陆www.10010.com,轻松交话费。,
11-28 15:34:15.899: I/SmsDemo(11997): date:2012-11-25 11:53:22,
11-28 15:34:15.899: I/SmsDemo(11997): type:接收
11-28 15:34:15.899: I/SmsDemo(11997): ++第3短信++
11-28 15:34:15.899: I/SmsDemo(11997): name:null,
11-28 15:34:15.899: I/SmsDemo(11997): number:+861314649****,
11-28 15:34:15.899: I/SmsDemo(11997): body:软件管理沉思录: SEI的项目管理,人际沟通,团队协作要诀 人民邮电出版社,
11-28 15:34:15.899: I/SmsDemo(11997): date:2012-11-22 03:39:18,
11-28 15:34:15.899: I/SmsDemo(11997): type:接收
11-28 15:34:15.899: I/SmsDemo(11997): ++第4短信++
11-28 15:34:15.899: I/SmsDemo(11997): name:null,
11-28 15:34:15.899: I/SmsDemo(11997): number:106901950120,
11-28 15:34:15.899: I/SmsDemo(11997): body:新华书店海淀桥书店-世宁大厦:47路(海淀桥东上,学院桥西下,6站),下车向东南220米 http://j.map.baidu.com/1xq4g -[百度],
11-28 15:34:15.899: I/SmsDemo(11997): date:2012-11-20 02:35:16,
11-28 15:34:15.899: I/SmsDemo(11997): type:接收
11-28 15:34:15.909: I/SmsDemo(11997): ++第5短信++
11-28 15:34:15.909: I/SmsDemo(11997): name:null,
11-28 15:34:15.909: I/SmsDemo(11997): number:106901950120,
11-28 15:34:15.909: I/SmsDemo(11997): body:新华书店海淀桥书店-世宁大厦:740内/983路/运通113路(海淀桥西上,学院桥西下),下车向东南200米 http://j.map.baidu.com/hvq4g -[百度],
11-28 15:34:15.909: I/SmsDemo(11997): date:2012-11-20 02:34:58,
11-28 15:34:15.909: I/SmsDemo(11997): type:接收

需要注意:在运行结果中,所有短信的发件人都是null,但在1314649号码中存了名称在联系人中,想起来了,我将此号码存在SIM卡中,或许是这个原因导致发件人为null,如果将此号码存在手机上?

posted on 2012-11-28 15:43  勤修  阅读(389)  评论(0编辑  收藏  举报

导航