zoukankan      html  css  js  c++  java
  • 内容提供器-联系人1

    选择一个联系人:

        private void pickContact(){
            Intent pickContactIntent = new Intent(Intent.ACTION_PICK, Uri.parse("content://contacts/people"));
            pickContactIntent.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_TYPE);
            startActivityForResult(pickContactIntent, PICK_CONTACT_REQUEST);
        }

    返回:

        @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            if(requestCode == PICK_CONTACT_REQUEST){
                if(resultCode == RESULT_OK){
                    Uri result = data.getData();
                    //long contactId = ContentUris.parseId(result);
    
                    ContentResolver cr = getContentResolver();
                    Cursor phones = cr.query(result, null ,null, null, null);
    
                    int rawContactId = phones.getColumnIndex(ContactsContract.Data.RAW_CONTACT_ID);
                    int nameIdx = phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME);
                    int contactIdIdx = phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone._ID);
                    int phoneNumberIdx = phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
                    int photoIdIdx = phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.PHOTO_ID);
                    phones.moveToFirst();
    
                    String idContact = phones.getString(contactIdIdx);
                    String name = phones.getString(nameIdx);
                    int types = phones.getInt(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE));
                    String phoneNumber = phones.getString(phoneNumberIdx);
    
                    Bitmap bitmap = loadContactPhoto(cr, phones.getLong(rawContactId));
                    showSelectedNumber(types, phoneNumber, name, bitmap);
    
                    phones.close();
                }
            }
         }

    加载联系人头像

    public static Bitmap loadContactPhoto(ContentResolver cr, long id) {
    Uri uri = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, id);
    InputStream input = ContactsContract.Contacts.openContactPhotoInputStream(cr, uri);
    if (input == null) {
    return null;
    }
    return BitmapFactory.decodeStream(input);
    }

    打印所有联系人:

                    Cursor phones = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,null,null, null);
                    // use the cursor to access the contacts
                    while (phones.moveToNext())
                    {
                        String name=phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
                        // get display name
                        phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
    
                        String types = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE));
    
                        // get phone number
                        System.out.println(name + "........"+types+".........."+phoneNumber);
                    }
                    phones.close();

     

    如果是只选择一个电话,可以这样:

    private static final String[] phoneProjection = new String[] { ContactsContract.CommonDataKinds.Phone.DATA };
    Uri contactUri = data.getData();
    if (null == contactUri) return;
    //no tampering with Uri makes this to work without READ_CONTACTS permission
    Cursor cursor = getContentResolver().query(contactUri, phoneProjection, null, null, null);
    if (null == cursor) return;
    try {
    while (cursor.moveToNext()) {
    String number = cursor.getString(0);
    // ... use "number" as you wish
    Toast.makeText(getBaseContext(), number, Toast.LENGTH_LONG).show();
    }
    } finally {
    cursor.close();
    }
  • 相关阅读:
    Http中的patch
    如何实现腾讯地图的路径规划功能?
    各类数据库分页SQL语法
    ABC222F
    ABC222 G
    LG5308 [COCI2019] Quiz(wqs二分+斜率优化DP)
    [USACO21OPEN] Portals G(Kruskal)
    【做题笔记】SP27379 BLUNIQ
    【做题笔记】CF938C Constructing Tests
    CSP-J/S2021 自闭记
  • 原文地址:https://www.cnblogs.com/yansc/p/7611360.html
Copyright © 2011-2022 走看看