1. 程式人生 > >獲取通訊錄、通話記錄、簡訊

獲取通訊錄、通話記錄、簡訊

獲取通訊錄:

    private ArrayList<String> getPhoneNum(Context context) {
        ArrayList<String> numList = new ArrayList<String>();

        ContentResolver cr = context.getContentResolver();

        Cursor cursor = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, ContactsContract.Contacts.SORT_KEY_ALTERNATIVE+" ASC");

        while (cursor.moveToNext()) {
            String contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
            Cursor phone = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
                    ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = " + contactId, null, null);

            while (phone.moveToNext()) {
                String strPhoneNumber = phone.getString(phone.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));//手機號碼
                String strPhoneName = phone.getString(phone.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));//通訊錄姓名
                numList.add(strPhoneNumber+"--" +strPhoneName);
                Log.v("tag", "strPhoneNumber:" + strPhoneNumber);
            }

            phone.close();
        }
        cursor.close();
        return numList;
    }
返回結果為:13501018080--趙麗穎

所需許可權:

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



獲取最近聯絡人(通話記錄):
    private ArrayList<String> getPhoneNum(Context context) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        ArrayList<String> numList = new ArrayList<String>();

        ContentResolver contentResolver = context.getContentResolver();
        Cursor cursor = null;
        try {
            cursor = contentResolver.query(CallLog.Calls.CONTENT_URI, null, null, null, CallLog.Calls.DATE + " desc");
            if (cursor == null)
                return null;

            while (cursor.moveToNext()) {
                String name = cursor.getString(cursor.getColumnIndex(CallLog.Calls.CACHED_NAME));
                String number = cursor.getString(cursor.getColumnIndex(CallLog.Calls.NUMBER));
                int type = cursor.getInt(cursor.getColumnIndex(CallLog.Calls.TYPE));//1:來電(incoming calls); 2:去電(outgoing calls); 3:未接電話(missed calls)
                long lDate = cursor.getLong(cursor.getColumnIndex(CallLog.Calls.DATE));
                long duration = cursor.getLong(cursor.getColumnIndex(CallLog.Calls.DURATION));//通話時間/秒
                String news = cursor.getString(cursor.getColumnIndex(CallLog.Calls.GEOCODED_LOCATION));//返回:北京 聯通

                numList.add(name + "-" + number + "-" + type + "-" + sdf.format(new Date(lDate)) + "-" + duration + "-" + news);
            }
        } catch (SecurityException e) {

        } finally {
            if (cursor != null) {
                cursor.close();
            }
        }
        return numList;
    }
返回結果:趙麗穎--13501018080--1--2016-08-15 07:45:59--120--北京 移動

需要許可權:

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


獲取簡訊:

    private ArrayList<String> getPhoneNum(Context context) {
        Uri CONTENT_URI = Uri.parse("content://sms");
        ArrayList<String> numList = new ArrayList<String>();
        ContentResolver contentResolver = context.getContentResolver();
        Cursor cursor = contentResolver.query(CONTENT_URI, null, null, null, Telephony.Sms.DEFAULT_SORT_ORDER);
        if (cursor == null)
            return null;

        int nameColumn = cursor.getColumnIndex("person");// 聯絡人姓名列表序號
        int phoneNumberColumn = cursor.getColumnIndex("address");// 手機號
        int smsbodyColumn = cursor.getColumnIndex("body");// 簡訊內容
        int dateColumn = cursor.getColumnIndex("date");// 日期
        int typeColumn = cursor.getColumnIndex("type");// 收發型別 1表示接受 2表示傳送
        while (cursor.moveToNext()) {
            String nameId = cursor.getString(nameColumn);
            String phoneNumber = cursor.getString(phoneNumberColumn);
            String smsbody = cursor.getString(smsbodyColumn);
            Date d = new Date(Long.parseLong(cursor.getString(dateColumn)));
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd " + "\n" + "hh:mm:ss");
            String date = dateFormat.format(d);
            String type = cursor.getString(typeColumn);
            String name = "未命名";

            Uri personUri = Uri.withAppendedPath( ContactsContract.PhoneLookup.CONTENT_FILTER_URI, phoneNumber);

            Cursor cur = contentResolver.query(personUri, new String[] { ContactsContract.PhoneLookup.DISPLAY_NAME }, null, null, null );

            if( cur.moveToFirst() ) {
                int nameIndex = cur.getColumnIndex(ContactsContract.PhoneLookup.DISPLAY_NAME);

                name = cur.getString(nameIndex);
            }
            cur.close();

            numList.add(name + "-" + phoneNumber + "-" + smsbody + "-" + date + "-" + type);
        }

        cursor.close();
        return numList;
    }

返回結果:趙麗穎--+86 135 0101 8080--今晚八點萬達看電影--2016-08-16 11:52:35--1

需要許可權:

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