在我的Android应用中显示来自一个特定发件人的所有短信

时间:2016-09-24 08:06:58

标签: android broadcastreceiver android-broadcastreceiver

我的应用目前正在使用以下代码显示收件箱中的所有短信:

public void onClick(View v) {

    if (v == btnInbox) {

        // Create Inbox box URI
        Uri inboxURI = Uri.parse("content://sms/inbox");

        // List required columns
        String[] reqCols = new String[]{"_id", "address", "body"};

        // Get Content Resolver object, which will deal with Content
        // Provider
        ContentResolver cr = getContentResolver();

        // Fetch Inbox SMS Message from Built-in Content Provider
        Cursor c = cr.query(inboxURI, reqCols, null, null, null);

        // Attached Cursor with adapter and display in listview
        adapter = new SimpleCursorAdapter(this, R.layout.row, c,
                new String[]{"body", "address"}, new int[]{
                R.id.lblMsg, R.id.lblNumber});
        lvMsg.setAdapter(adapter);

    }
}

我想只显示来自某个特定发件人的邮件,我将更改哪些内容以消除来自其他发件人的邮件?

1 个答案:

答案 0 :(得分:1)

应用选择参数以根据地址字段过滤内容。

实际代码变为:

String[] selectionArgs = { "12672631" }; //add the phone number here
Cursor c = cr.query(inboxURI, reqCols, "address=?", selectionArgs, null);