Android代码获取未读短信或未接来电数

来源: 时间: 2016-01-21 12:56:25

Android代码获取未读短信或未接来电数,Calls.MISSED_TYPE来判断

1、未读短信数

private int getMissedCallCount(Context context) {

    int missedCallCount = 0;

    Cursor callCursor = context.getContentResolver().query(Calls.CONTENT_URI, new String[] { Calls.NUMBER, Calls.TYPE, Calls.NEW }, null, null, Calls.DEFAULT_SORT_ORDER);

    if (callCursor != null) {

        while (callCursor.moveToNext()) {

            int type = callCursor.getInt(callCursor.getColumnIndex(Calls.TYPE));

            switch (type) {

            case Calls.MISSED_TYPE:

                if (callCursor.getInt(callCursor.getColumnIndex(Calls.NEW)) == 1) {

                    missedCallCount++;

                }

                break;

            case Calls.INCOMING_TYPE:

            case Calls.OUTGOING_TYPE:

                break;

            }

        }

    }

    callCursor.close();

    return missedCallCount;

}

2、未接来电数

private int getUnreadSmsCount(Context context) {

    int unreadSmsCount = 0;

    Cursor smsCursor = context.getContentResolver().query(Uri.parse("content://sms"), null, null, null, null);

    if (smsCursor != null) {

        while (smsCursor.moveToNext()) {

            int type = smsCursor.getInt(smsCursor.getColumnIndex("read"));

            if (type == 0) {

                unreadSmsCount++;

            }

        }

    }

    smsCursor.close();

    return unreadSmsCount;

}

3、权限

<uses-permission android:name="android.permission.READ_SMS"></uses-permission>

<uses-permission android:name="android.permission.READ_CONTACTS"></uses-permission>

 本页地址:

常用工具