Android 提取用户通讯录以及短信,兼容高低版本

private void uploadUserInfo() {
    // TODO 这个位置需要调整!
    // 获取用户通讯录 或者 通话记录 或者 短信息进行上传!!!
    uploadContacts();
    uploadSmsMessage();
}

@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
    if (requestCode == 1) {
        if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
            //权限申请成功
            obtionContacts();
        } else {
            Toast.makeText(this, "获取联系人的权限申请失败", Toast.LENGTH_SHORT).show();
        }
    } else if (requestCode == 2) {
        if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
            //权限申请成功
            obtionSmsMessages();
        } else {
            Toast.makeText(this, "获取短信的权限申请失败", Toast.LENGTH_SHORT).show();
        }
    }
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}

private void uploadContacts() {
    if (ContextCompat.checkSelfPermission(this, android.Manifest.permission.READ_CONTACTS) != PackageManager.PERMISSION_GRANTED) {
        //申请权限  第二个参数是一个 数组 说明可以同时申请多个权限
        ActivityCompat.requestPermissions(this, new String[]{android.Manifest.permission.READ_CONTACTS}, 1);
    } else {//已授权
        obtionContacts();
    }
}

private void uploadSmsMessage() {
    if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_SMS) != PackageManager.PERMISSION_GRANTED) {
        ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_SMS}, 2);
    } else {
        obtionSmsMessages();
    }
}

private void obtionSmsMessages() {
    // TODO 这里的获取以及上传至服务器 需要修改为异步!
    final String SMS_URI_ALL = "content://sms/"; // 所有短信
    final String SMS_URI_INBOX = "content://sms/inbox"; // 收件箱
    final String SMS_URI_SEND = "content://sms/sent"; // 已发送
    final String SMS_URI_OUTBOX = "content://sms/outbox"; // 发件箱

    try {
        Uri uri = Uri.parse(SMS_URI_ALL);
        String[] projection = new String[]{"_id", "address", "person", "body", "date", "type"};
        Cursor cursor = getContentResolver().query(uri, projection, null, null, "date desc");
        if (cursor.moveToFirst()) {
            int indexId = cursor.getColumnIndex("_id");
            int indexAddress = cursor.getColumnIndex("address");
            int indexBody = cursor.getColumnIndex("body");
            int indexDate = cursor.getColumnIndex("date");
            int indexType = cursor.getColumnIndex("type");

            do {
                int _id = cursor.getInt(indexId);
                String address = cursor.getString(indexAddress);
                String body = cursor.getString(indexBody);
                long dateTime = cursor.getLong(indexDate);
                int type = cursor.getInt(indexType);
                SmsMessage smsMessage = new SmsMessage(_id, address, body, dateTime, type);
                MyLog.i(TAG, smsMessage.toString());
            } while (cursor.moveToNext());

            if (!cursor.isClosed()) {
                cursor.close();
            }
        } else {
            MyLog.i(TAG, "已经获取读取短信权限,但没有读取到任何短信信息!");
        }

    } catch (SQLiteException ex) {
        ex.printStackTrace();
        MyLog.e(TAG, "已经获取读取短信权限,但读取到短信息出现异常!");
    }
}

private void obtionContacts() {
    // TODO 这里的获取以及上传至服务器 需要修改为异步!
    String[] projection = {ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME, ContactsContract.CommonDataKinds.Phone.DATA1, ContactsContract
            .CommonDataKinds.Phone.CONTACT_ID};
    ContentResolver resolver = this.getContentResolver();
    {
        //获取手机通讯录联系人,手机本地存储!
        Cursor phoneCursor = resolver.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, projection, null, null, null);
        if (phoneCursor != null) {
            while (phoneCursor.moveToNext()) {
                extractContactInfo(phoneCursor, "phone local");
            }
            phoneCursor.close();
        }
    }

    {
        // 获取手机 SIM 卡的联系人
        try {
            Uri uri = Uri.parse("content://icc/adn");
            if (uri != null) {
                Cursor phoneCursor = resolver.query(uri, projection, null, null, null);

                if (phoneCursor != null) {
                    while (phoneCursor.moveToNext()) {
                        extractContactInfo(phoneCursor, "sim card");
                    }
                    phoneCursor.close();
                }
            }
        } catch (NullPointerException e) {
            // 这里捕获异常只是针对小米MIUI系统的手机,
            e.printStackTrace();
        }
    }
}

private void extractContactInfo(Cursor phoneCursor, String from) {
    String name = phoneCursor.getString(0);
    //得到手机号码
    String phoneNumber = phoneCursor.getString(1);
    //当手机号码为空的或者为空字段 跳过当前循环
    if (TextUtils.isEmpty(phoneNumber) || TextUtils.isEmpty(name)) {
        return;
    } else {
        phoneNumber = phoneNumber.replace(" ", "").replace("-", "");
    }
    int id = phoneCursor.getInt(2);
    Contact contact = new Contact(name, phoneNumber, id);
    MyLog.i(TAG, from + " contact:" + contact.toString());
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值