android读取联系人,短信,通话记录,IMSI

android平台是通过ContentProvider来提供数据访问的统一接口,而访问这些数据需要创建ContentResolver客服端。

android读取联系人:

ContentResolver cr = getContentResolver(); //创建ContentProvider客服端
        //获取通讯录
        Cursor cursor = cr.query(ContactsContract.Contacts.CONTENT_URI,null,null,null,null);

        while(cursor.moveToNext()){

            //获取通信录姓名
            int nameColumnIndex = cursor.getColumnIndex(ContactsContract.PhoneLookup.DISPLAY_NAME);
            String name = cursor.getString(nameColumnIndex);

            //获取该条目录下的所有电话
            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);
            String number = new String("");
            while(phone.moveToNext()){
                number += phone.getString(phone.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)) + " ";
            }
// 下面这种写法话报错,cursor.getColumnIndex返回-1,可能是存在多个号码的原因
//            String number = cursor.getString(cursor.getColumnIndex(ContactsContract.PhoneLookup.NUMBER));

            String content = name + ":" + number;

            Log.v("Content",content);
        }

android读取短信

        Cursor cursor = cr.query(Uri.parse("content://sms/"),null,null,null,null);
        while(cursor.moveToNext()){
            String number = cursor.getString(cursor.getColumnIndex("address"));//手机号
            String body = cursor.getString(cursor.getColumnIndex("body"));//短信内容

            Log.v("Content",number + body);
        }

安卓读取通话记录

ContentResolver cr = getContentResolver(); //创建ContentProvider客服端
Cursor cursor = cr.query(CallLog.Calls.CONTENT_URI,null,null,null,null);
        while(cursor.moveToNext()){
            //号码
            String number = cursor.getString(cursor.getColumnIndex(CallLog.Calls.NUMBER));
            //呼叫类型
            String type;
            switch (Integer.parseInt(cursor.getString(cursor.getColumnIndex(CallLog.Calls.TYPE)))) {
                case CallLog.Calls.INCOMING_TYPE:
                    type = "呼入";
                    break;
                case CallLog.Calls.OUTGOING_TYPE:
                    type = "呼出";
                    break;
                case CallLog.Calls.MISSED_TYPE:
                    type = "未接";
                    break;
                default:
                    type = "挂断";//应该是挂断.根据我手机类型判断出的
                    break;
            }
            SimpleDateFormat sfd = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date date = new Date(Long.parseLong(cursor.getString(cursor.getColumnIndexOrThrow(CallLog.Calls.DATE))));
            //呼叫时间
            String time = sfd.format(date);
            //联系人
            String name = cursor.getString(cursor.getColumnIndexOrThrow(CallLog.Calls.CACHED_NAME));
            //通话时间,单位:s
            String duration = cursor.getString(cursor.getColumnIndexOrThrow(CallLog.Calls.DURATION));

            String content = time +"  号码:" + number + "  " + type + "  通话时间(s): " + duration;
            Log.v("xx",content);
        }

读取手机IMSI

TelephonyManager tm = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
String DEVICE_ID = tm.getDeviceId();

所需要的权限

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

向上学习
ContentProvider机制

你可能感兴趣的:(android学习)