通信録、通話記録、メールの取得

5750 ワード

アドレス帳を取得するには、次の手順に従います.
    private ArrayList getPhoneNum(Context context) {
        ArrayList numList = new ArrayList();

        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;
    }
戻り結果:135018080--趙麗穎
必要な権限:



最近の連絡先(通話履歴):
    private ArrayList getPhoneNum(Context context) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        ArrayList numList = new ArrayList();

        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;
    }
結果:趙麗穎--135018080--1--2016-08-15 07:45:59--120--北京移動
必要な権限:
    

メッセージの取得:
    private ArrayList getPhoneNum(Context context) {
        Uri CONTENT_URI = Uri.parse("content://sms");
        ArrayList numList = new ArrayList();
        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 " + "
" + "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; }

回复结果:赵丽穎--+86135 0101 8080--今晩8時に万達は映画を見ます--2016-08-16 11:52:35-1
必要な権限: