开发者社区> 问答> 正文

android获取短信内容时,总是收到两次短信内容

public String getSmsAndSendBack()
{

    String[] projection = new String[] { "address", "person", "body" };
    StringBuilder str = new StringBuilder();
    try {
        ContentResolver cr = getContentResolver();
        Cursor myCursor = cr.query(Uri.parse("content://sms/inbox"),
                projection,

                "address = ?", new String[] { "+86"+number},
                "date desc");

        str.append(processResults(myCursor, true));
    } catch (SQLiteException ex) {

        // Log.d(LOG_TAG, ex.getMessage());
    }

    return str.toString();

}

/**
 * 
 * 处理短信结果
 * 
 * @param cur
 * 
 * @param all
 *            用来判断是读一条还是全部读。后来没有用all,可以无视
 */

private StringBuilder processResults(Cursor cur, boolean all) {

    StringBuilder str = new StringBuilder();

    if (cur.moveToFirst()) {

        String name;
        String phoneNumber;
        String sms = null;
        int nameColumn = cur.getColumnIndex("person");
        int phoneColumn = cur.getColumnIndex("address");
        int smsColumn = cur.getColumnIndex("body");
        // do {
        name = cur.getString(nameColumn);
        phoneNumber = cur.getString(phoneColumn);
        if (phoneNumber.equals("+86"+number)) {
            sms = cur.getString(smsColumn);
            str.append(sms);
        }

        if (null == sms)

            sms = "";
        // } while (cur.moveToNext());
    } else {

        str.append("no result!");
    }
    return str;
}

图片说明

展开
收起
爵霸 2016-06-20 08:56:28 2598 0
0 条回答
写回答
取消 提交回答
问答分类:
问答标签:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
58同城Android客户端Walle框架演进与实践之路 立即下载
Android组件化实现 立即下载
蚂蚁聚宝Android秒级编译——Freeline 立即下载