Android 获取未读未接来电和未读短信数量

一、未读短信

 

  首先注册Observer,当有新短信或彩信来的时候会调用 onChange方法,我们可以在onChange方法中去获取未读短信和彩信,然后做一些UI上的处理!

    private ContentObserver newMmsContentObserver = new ContentObserver(new Handler()) {
        public void onChange(boolean selfChange) {
            int mNewSmsCount = getNewSmsCount() + getNewMmsCount();
        }
    };
    private void registerObserver() {
        unregisterObserver();
        getContentResolver().registerContentObserver(Uri.parse("content://sms"), true,
                newMmsContentObserver);
        getContentResolver().registerContentObserver(MmsSms.CONTENT_URI, true,
                newMmsContentObserver);
    }

    private synchronized void unregisterObserver() {
        try {
            if (newMmsContentObserver != null) {
                getContentResolver().unregisterContentObserver(newMmsContentObserver);
            }
            if (newMmsContentObserver != null) {
                getContentResolver().unregisterContentObserver(newMmsContentObserver);
            }
        } catch (Exception e) {
            Log.e(TAG, "unregisterObserver fail");
        }
    }

  得到未读短信数量:

    private int getNewSmsCount() {
        int result = 0;
        Cursor csr = getContentResolver().query(Uri.parse("content://sms"), null,
                "type = 1 and read = 0", null, null);
        if (csr != null) {
            result = csr.getCount();
            csr.close();
        }
        return result;
    }

 

  获取未读彩信数量 

    private int getNewMmsCount() {
        int result = 0;
        Cursor csr = getContentResolver().query(Uri.parse("content://mms/inbox"),
                null, "read = 0", null, null);
        if (csr != null) {
            result = csr.getCount();
            csr.close();
        }
        return result;
    }

 

二、未接来电

 未接来电不能用 Observer监听,不过当有新的未接来电时,系统会发送一个广播com.android.phone.NotificationMgr.MissedCall_intent(锁屏上显示的未接来电数量就是通知监听这个广播实现的)

        final IntentFilter filter = new IntentFilter();
        filter.addAction("com.android.phone.NotificationMgr.MissedCall_intent");
        final Application application = getApplication();
        application.registerReceiver(new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                String action = intent.getAction();
                if (action != null && "com.android.phone.NotificationMgr.MissedCall_intent".equals(action)) {
                    int mMissCallCount = intent.getExtras().getInt("MissedCallNumber");
                }
            }
        }, filter);

  广播只是当有新的未接来电时再会发送,但是如果有旧的未接来电没有读取时,上面的广播就得不到数据 了,那就必须得从数据库中查找了

    private int readMissCall() {
        int result = 0;
        Cursor cursor = getContentResolver().query(CallLog.Calls.CONTENT_URI, new String[] {
                Calls.TYPE
            }, " type=? and new=?", new String[] {
                    Calls.MISSED_TYPE + "", "1"
            }, "date desc");

        if (cursor != null) {
            result = cursor.getCount();
            cursor.close();
        }
        return result;
    }

 

 

你可能感兴趣的:(android)