private ContactData getContactData(Context context, String displayName, long id) { ContactData contactData = new ContactData(id, displayName); Cursor numberCursor = null; try { numberCursor = context.getContentResolver().query(Phone.CONTENT_URI, null, Phone.CONTACT_ID + " = ?", new String[] {contactData.id + ""}, null); while (numberCursor != null && numberCursor.moveToNext()) { int type = numberCursor.getInt(numberCursor.getColumnIndexOrThrow(Phone.TYPE)); String label = numberCursor.getString(numberCursor.getColumnIndexOrThrow(Phone.LABEL)); String number = numberCursor.getString(numberCursor.getColumnIndexOrThrow(Phone.NUMBER)); String typeLabel = Phone.getTypeLabel(context.getResources(), type, label).toString(); contactData.numbers.add(new NumberData(typeLabel, number)); } } finally { if (numberCursor != null) numberCursor.close(); } return contactData; }
private void loadUserInfoFromPhone(){ User user = AppManager.getSession(); if (user != null) { etName.setText(user.getName()); Util.setProfileImage(user.getPhoto(), imgProfile); } else { if (checkPermissions()) { String[] projection = new String[] { Phone.DISPLAY_NAME, Phone.HAS_PHONE_NUMBER, Phone.NUMBER, Phone.CONTACT_ID}; Cursor cursor = getApplication().getContentResolver().query(ContactsContract.Profile.CONTENT_URI, null, null, null, null); if (cursor.moveToFirst()){ etName.setText(cursor.getString(cursor.getColumnIndex("display_name"))); } cursor.close(); } } }
public static void sendContactDetails(Context context, String ph_no, ContactItemDataModel contactData, Uri uri) { Cursor cursor = context.getContentResolver().query(uri, null, null, null, null); if (cursor != null && cursor.moveToFirst()) { String phoneNo = cursor.getString(cursor.getColumnIndex(Phone.NUMBER)); String name = cursor.getString(cursor.getColumnIndex(Phone.DISPLAY_NAME)); if (phoneNo == null) phoneNo = ""; if (name == null) name = ""; JsonObject contact_message = new JsonObject(); contact_message.addProperty("name", name); contact_message.addProperty("number", phoneNo); SendMessageService.startSendTextMessage(context, ph_no, contactData.getContact_id(), contact_message.toString(), "contact", contactData.is_bot(), Utils.getCurrentTimestamp()); } }
@Override public void onActivityResult(int requestCode, int resultCode, Intent data) { // Check which request we're responding to if (requestCode == REQUEST_CONTACTS) { // Make sure the request was successful if (resultCode == RESULT_OK) { Uri uri = data.getData(); String[] projection = { Phone.DISPLAY_NAME }; try (Cursor cursor = getContentResolver().query(uri, projection, null, null, null)) { if (cursor == null || cursor.getCount() <= 0) { Log.e(TAG, "Null cursor found. Weird! Probably no contact was picked"); return; } cursor.moveToFirst(); int nameColumnIndex = cursor.getColumnIndex(Phone.DISPLAY_NAME); String name = cursor.getString(nameColumnIndex); nameField.setText(name); } } } }
public List<String> getNumbersForThreadSearchFilter(String constraint, ContentResolver contentResolver) { LinkedList<String> numberList = new LinkedList<String>(); Cursor cursor = null; try { cursor = contentResolver.query(Uri.withAppendedPath(Phone.CONTENT_FILTER_URI, Uri.encode(constraint)), null, null, null, null); while (cursor != null && cursor.moveToNext()) { numberList.add(cursor.getString(cursor.getColumnIndexOrThrow(Phone.NUMBER))); } } finally { if (cursor != null) cursor.close(); } return numberList; }
/** * 得到手机通讯录联系人信息 */ private void getPhoneContacts() { ContentResolver resolver = mContext.getContentResolver(); //query查询,得到结果的游标 Cursor phoneCursor = resolver.query(Phone.CONTENT_URI, PHONES_PROJECTION, null, null, null); if (phoneCursor != null) { while (phoneCursor.moveToNext()) { //得到手机号码 String phoneNumber = phoneCursor.getString(PHONES_NUMBER_INDEX); //当手机号码为空的或者为空字段 跳过当前循环 if (TextUtils.isEmpty(phoneNumber)) { continue; } //得到联系人名称 String contactName = phoneCursor.getString(PHONES_DISPLAY_NAME_INDEX); ContactModel cb = new ContactModel(); cb.setContactName(contactName); cb.setPhoneNumber(phoneNumber); contactsList.add(cb); } phoneCursor.close(); } }
/** * Helper to retrieve a contact's information from the phone book activity * * @param data The Intent carrying the data of the phone book selection * @param context Context to get content resolver from * @return Contact object storing the name and phone number of the retrieved contact */ public static ContactUtils.Contact getContactFromPhonebook(Intent data, Context context) { // Get the data from the Intent Uri contact_data = data.getData(); // Query the Intent's data and extract the ID and name of the contact String[] projection = {Phone.DISPLAY_NAME, Phone.NUMBER}; String sel = String.format("%s = %s", Phone.TYPE, Phone.TYPE_MOBILE); Cursor c = context.getContentResolver().query(contact_data, projection, sel, null, null); if (c.moveToFirst()) { if (c.moveToFirst()) { String name = c.getString(c.getColumnIndex(Phone.DISPLAY_NAME)); String phone = convertToE164(c.getString(c.getColumnIndex(Phone.NUMBER))); return new ContactUtils.Contact(name, phone); } c.close(); } return new ContactUtils.Contact(); }
/** * 得到手机通讯录联系人信息 */ private void getPhoneContacts() { //query查询,得到结果的游标 Cursor phoneCursor = mResolver.query(Phone.CONTENT_URI, PHONES_PROJECTION, null, null, null); if (phoneCursor != null) { while (phoneCursor.moveToNext()) { //得到手机号码 String phoneNumber = phoneCursor.getString(PHONES_NUMBER_INDEX); //当手机号码为空的或者为空字段 跳过当前循环 if (TextUtils.isEmpty(phoneNumber)) { continue; } //得到联系人名称 String contactName = phoneCursor.getString(PHONES_DISPLAY_NAME_INDEX); ContactModel cb = new ContactModel(); cb.setContactName(contactName); cb.setPhoneNumber(phoneNumber); contactsList.add(cb); } phoneCursor.close(); } }
public static void addContact(String name, String number) { ContentValues values = new ContentValues(); //������RawContacts.CONTENT_URIִ��һ����ֵ���룬Ŀ���ǻ�ȡϵͳ���ص�rawContactId Uri rawContactUri = m_context.getContentResolver().insert(RawContacts.CONTENT_URI, values); long rawContactId = ContentUris.parseId(rawContactUri); //��data������������� values.clear(); values.put(Data.RAW_CONTACT_ID, rawContactId); values.put(Data.MIMETYPE, StructuredName.CONTENT_ITEM_TYPE);//�������� values.put(StructuredName.GIVEN_NAME, name); m_context.getContentResolver().insert(ContactsContract.Data.CONTENT_URI, values); //��data�����绰���� values.clear(); values.put(Data.RAW_CONTACT_ID, rawContactId); values.put(Data.MIMETYPE, Phone.CONTENT_ITEM_TYPE); values.put(Phone.NUMBER, number); values.put(Phone.TYPE, Phone.TYPE_MOBILE); m_context.getContentResolver().insert(ContactsContract.Data.CONTENT_URI, values); }
public static void changeContact(String name, String number, String ContactId) { Log.i("huahua", name); ContentValues values = new ContentValues(); // �������� values.put(StructuredName.GIVEN_NAME, name); m_context.getContentResolver().update(ContactsContract.Data.CONTENT_URI, values, Data.RAW_CONTACT_ID + "=? and " + Data.MIMETYPE + "=?", new String[] { ContactId,StructuredName.CONTENT_ITEM_TYPE }); //���µ绰 values.clear(); values.put(ContactsContract.CommonDataKinds.Phone.NUMBER, number); m_context.getContentResolver().update(ContactsContract.Data.CONTENT_URI, values, Data.RAW_CONTACT_ID + "=? and " + Data.MIMETYPE + "=?", new String[] { ContactId,Phone.CONTENT_ITEM_TYPE}); }
@Test public void testAppendNameProperties_phone() { final VCardBuilder builder = new VCardBuilder(VCardConfig.VCARD_TYPE_V30_GENERIC, VCardConfig.DEFAULT_EXPORT_CHARSET); final ContentValues cv = new ContentValues(); final List<ContentValues> group = new ArrayList<>(); cv.put(ContactsContract.Data.MIMETYPE, Phone.CONTENT_ITEM_TYPE); cv.put(Email.IS_PRIMARY, 0); cv.put(Email.DATA1, "(123) 123-4567"); group.add(cv); final String card = builder.appendPhones(group, null).toString(); assertEquals(PHONE_CARD, card); }
@Override protected Void doInBackground(Long... ids) { String[] projection = new String[] {Phone.DISPLAY_NAME, Phone.TYPE, Phone.NUMBER, Phone.LABEL}; long contactId = ids[0]; final Cursor phoneCursor = getActivity().getContentResolver().query( Phone.CONTENT_URI, projection, Data.CONTACT_ID + "=?", new String[]{String.valueOf(contactId)}, null); if(phoneCursor != null && phoneCursor.moveToFirst() && phoneCursor.getCount() == 1) { final int contactNumberColumnIndex = phoneCursor.getColumnIndex(Phone.NUMBER); mPhoneNumber = phoneCursor.getString(contactNumberColumnIndex); int type = phoneCursor.getInt(phoneCursor.getColumnIndexOrThrow(Phone.TYPE)); mPhoneLabel = phoneCursor.getString(phoneCursor.getColumnIndex(Phone.LABEL)); mPhoneLabel = Phone.getTypeLabel(getResources(), type, mPhoneLabel).toString(); phoneCursor.close(); } return null; }
public List<String> getNumbersForThreadSearchFilter(Context context, String constraint) { LinkedList<String> numberList = new LinkedList<>(); Cursor cursor = null; try { cursor = context.getContentResolver().query(Uri.withAppendedPath(Phone.CONTENT_FILTER_URI, Uri.encode(constraint)), null, null, null, null); while (cursor != null && cursor.moveToNext()) { numberList.add(cursor.getString(cursor.getColumnIndexOrThrow(Phone.NUMBER))); } } finally { if (cursor != null) cursor.close(); } return numberList; }
@Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (RESULT_OK == resultCode) { String selection = Phone.CONTACT_ID + "=?"; Uri result = data.getData(); String id = result.getLastPathSegment(); String[] arguments = new String[]{id}; ContentResolver resolver = getContentResolver(); Cursor cursor = resolver.query(Phone.CONTENT_URI, null, selection, arguments, null); int index = cursor.getColumnIndex(Phone.DATA); if (cursor.moveToFirst()) { String phone = cursor.getString(index); set(this, phone); EditText edit = (EditText) findViewById(R.id.contact); edit.setText(phone); } cursor.close(); } }
@Override public void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (requestCode == REQUEST_CODE) { if (data != null) { Uri result = data.getData(); String contractId = result.getLastPathSegment(); Cursor cursor = getActivity().getContentResolver().query(Phone.CONTENT_URI, new String[]{ Phone.DISPLAY_NAME, Phone.NUMBER}, Phone.CONTACT_ID + "=?", new String[]{contractId}, null); if (cursor != null && cursor.getCount() != 0) { cursor.moveToFirst(); String phoneName = cursor.getString(cursor.getColumnIndexOrThrow(Phone .DISPLAY_NAME)); String phoneNum = cursor.getString(cursor.getColumnIndexOrThrow(Phone .NUMBER)); mPhoneNumInput.setText(phoneNum); mNote.setText(phoneName); } } } }
public static HashMap<String, String> resolvePhoneNumber(Context context, Uri uri) { HashMap<String, String> phoneList = new HashMap<String, String>(); Cursor cursor = context.getContentResolver().query(uri, PROJECTION_CONTACT, null, null, null); if (cursor.moveToFirst()) { String contactId = cursor.getString(cursor.getColumnIndexOrThrow(Contacts._ID)); String contactName = cursor.getString(cursor.getColumnIndexOrThrow(Contacts.DISPLAY_NAME)); LLog.d("Selected Contact id/name :: %s/%s", contactId, contactName); Cursor phoneCursor = context.getContentResolver().query(Phone.CONTENT_URI, PROJECTION_PHONE, Phone.CONTACT_ID+"="+contactId, null, null); while (phoneCursor.moveToNext()) { int type = phoneCursor.getInt(phoneCursor.getColumnIndexOrThrow(Phone.TYPE)); String typeLabel = Phone.getTypeLabel(context.getResources(), type, "Unknow").toString(); phoneList.put(typeLabel, phoneCursor.getString(phoneCursor.getColumnIndexOrThrow(Phone.NUMBER))); } phoneCursor.close(); } cursor.close(); return phoneList; }
private void initColumnNameConstantsMap() { if (mColumnNameConstantsMap != null) { return; } mColumnNameConstantsMap = new HashMap<String, String>(); mColumnNameConstantsMap.put("name", StructuredName.DISPLAY_NAME); mColumnNameConstantsMap.put("givenname", StructuredName.GIVEN_NAME); mColumnNameConstantsMap.put("familyname", StructuredName.FAMILY_NAME); mColumnNameConstantsMap.put("honorificprefix", StructuredName.PREFIX); mColumnNameConstantsMap.put("honorificsuffix", StructuredName.SUFFIX); mColumnNameConstantsMap.put("additionalname", CUSTOM_DATA_COLUMN); mColumnNameConstantsMap.put("nickname", Nickname.NAME); mColumnNameConstantsMap.put("adr", StructuredPostal.STREET); mColumnNameConstantsMap.put("email", Email.ADDRESS); mColumnNameConstantsMap.put("url", Website.URL); mColumnNameConstantsMap.put("category", GroupMembership.GROUP_ROW_ID); mColumnNameConstantsMap.put("tel", Phone.NUMBER); mColumnNameConstantsMap.put("org", Organization.COMPANY); mColumnNameConstantsMap.put("jobTitle", Organization.TITLE); mColumnNameConstantsMap.put("note", Note.NOTE); mColumnNameConstantsMap.put("impp", Im.DATA); mColumnNameConstantsMap.put("sex", CUSTOM_DATA_COLUMN); mColumnNameConstantsMap.put("genderidentity", CUSTOM_DATA_COLUMN); mColumnNameConstantsMap.put("key", CUSTOM_DATA_COLUMN); }
private void initMimeTypeConstantsMap() { if (mMimeTypeConstantsMap != null) { return; } mMimeTypeConstantsMap = new HashMap<String, String>(); mMimeTypeConstantsMap.put("name", StructuredName.CONTENT_ITEM_TYPE); mMimeTypeConstantsMap.put("givenname", StructuredName.CONTENT_ITEM_TYPE); mMimeTypeConstantsMap.put("familyname", StructuredName.CONTENT_ITEM_TYPE); mMimeTypeConstantsMap.put("honorificprefix", StructuredName.CONTENT_ITEM_TYPE); mMimeTypeConstantsMap.put("honorificsuffix", StructuredName.CONTENT_ITEM_TYPE); mMimeTypeConstantsMap.put("additionalname", MIMETYPE_ADDITIONAL_NAME); mMimeTypeConstantsMap.put("nickname", Nickname.CONTENT_ITEM_TYPE); mMimeTypeConstantsMap.put("email", Email.CONTENT_ITEM_TYPE); mMimeTypeConstantsMap.put("url", Website.CONTENT_ITEM_TYPE); mMimeTypeConstantsMap.put("category", GroupMembership.CONTENT_ITEM_TYPE); mMimeTypeConstantsMap.put("tel", Phone.CONTENT_ITEM_TYPE); mMimeTypeConstantsMap.put("org", Organization.CONTENT_ITEM_TYPE); mMimeTypeConstantsMap.put("jobTitle", Organization.CONTENT_ITEM_TYPE); mMimeTypeConstantsMap.put("note", Note.CONTENT_ITEM_TYPE); mMimeTypeConstantsMap.put("impp", Im.CONTENT_ITEM_TYPE); mMimeTypeConstantsMap.put("sex", MIMETYPE_SEX); mMimeTypeConstantsMap.put("genderidentity", MIMETYPE_GENDER_IDENTITY); mMimeTypeConstantsMap.put("key", MIMETYPE_KEY); }
@Override protected Cursor getChildrenCursor(Cursor groupCursor) { // Given the group, we return a cursor for all the children within that group // Return a cursor that points to this contact's phone numbers Uri.Builder builder = Contacts.CONTENT_URI.buildUpon(); ContentUris.appendId(builder, groupCursor.getLong(GROUP_ID_COLUMN_INDEX)); builder.appendEncodedPath(Contacts.Data.CONTENT_DIRECTORY); Uri phoneNumbersUri = builder.build(); mQueryHandler.startQuery(TOKEN_CHILD, groupCursor.getPosition(), phoneNumbersUri, PHONE_NUMBER_PROJECTION, Phone.MIMETYPE + "=?", new String[] { Phone.CONTENT_ITEM_TYPE }, null); return null; }
@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // Set up our adapter mAdapter = new MyExpandableListAdapter( this, android.R.layout.simple_expandable_list_item_1, android.R.layout.simple_expandable_list_item_1, new String[] { Contacts.DISPLAY_NAME }, // Name for group layouts new int[] { android.R.id.text1 }, new String[] { Phone.NUMBER }, // Number for child layouts new int[] { android.R.id.text1 }); setListAdapter(mAdapter); mQueryHandler = new QueryHandler(this, mAdapter); // Query for people mQueryHandler.startQuery(TOKEN_GROUP, null, Contacts.CONTENT_URI, CONTACTS_PROJECTION, Contacts.HAS_PHONE_NUMBER + "=1", null, null); }
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.list_7); mPhone = (TextView) findViewById(R.id.phone); getListView().setOnItemSelectedListener(this); // Get a cursor with all numbers. // This query will only return contacts with phone numbers Cursor c = getContentResolver().query(Phone.CONTENT_URI, PHONE_PROJECTION, Phone.NUMBER + " NOT NULL", null, null); startManagingCursor(c); ListAdapter adapter = new SimpleCursorAdapter(this, // Use a template that displays a text view android.R.layout.simple_list_item_1, // Give the cursor to the list adapter c, // Map the DISPLAY_NAME column to... new String[] {Phone.DISPLAY_NAME}, // The "text1" view defined in the XML template new int[] {android.R.id.text1}); setListAdapter(adapter); }
public void onItemSelected(AdapterView<?> parent, View v, int position, long id) { if (position >= 0) { //Get current cursor Cursor c = (Cursor) parent.getItemAtPosition(position); int type = c.getInt(COLUMN_PHONE_TYPE); String phone = c.getString(COLUMN_PHONE_NUMBER); String label = null; //Custom type? Then get the custom label if (type == Phone.TYPE_CUSTOM) { label = c.getString(COLUMN_PHONE_LABEL); } //Get the readable string String numberType = (String) Phone.getTypeLabel(getResources(), type, label); String text = numberType + ": " + phone; mPhone.setText(text); } }
@Override public void onClick(View view) { operate_Manage_PopupWindow.dismiss(); switch (view.getId()) { case R.id.popup_key_manage_textView_save: Intent intent_save = new Intent(Intent.ACTION_INSERT); intent_save.setType("vnd.android.cursor.dir/person"); intent_save.setType("vnd.android.cursor.dir/contact"); intent_save.setType("vnd.android.cursor.dir/raw_contact"); // 添加姓名 intent_save.putExtra(Insert.NAME, realName); // 添加手机 intent_save.putExtra(Insert.PHONE_TYPE, Phone.TYPE_MOBILE); intent_save.putExtra(Insert.PHONE, tel); startActivity(intent_save); break; case R.id.popup_key_manage_textView_dial: Intent intent_dial = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + tel)); startActivity(intent_dial); break; } }
public void getPhoneNumbers(String id) { Log.d("getPhoneNumbers", "looking data for contact id: " + id); Cursor pCur = mContext.getContentResolver().query(Data.CONTENT_URI, new String[] { Data._ID, Phone.NUMBER, Phone.TYPE, Phone.LABEL }, Data.CONTACT_ID + "=?" + " AND " + Data.MIMETYPE + "='" + Phone.CONTENT_ITEM_TYPE + "'", new String[] { String.valueOf(id) }, null); while (pCur.moveToNext()) { String phoneNumber = pCur.getString(1); String phoneType = pCur.getString(2); String phoneLabel = pCur.getString(3); if (isNumeric(phoneType)) { Log.d("getPhoneNumbers", "phoneNumber: " + phoneNumber + ", phoneType: " + phoneType + ", phoneLabel: " + phoneLabel); } } pCur.close(); return; }
/** * Adds a phone number * * @param phone * new phone number for the contact * @param phoneType * the type: cell, home, etc. * @return instance of ContactOperations */ public ContactOperations addPhone(String phone, int phoneType, String label, boolean isPrimary, boolean isSuperPrimary) { mValues.clear(); if (!TextUtils.isEmpty(phone)) { mValues.put(Phone.NUMBER, phone); mValues.put(Phone.TYPE, phoneType); mValues.put(Phone.MIMETYPE, Phone.CONTENT_ITEM_TYPE); if (!TextUtils.isEmpty(label)) { mValues.put(Phone.LABEL, label); } if (isSuperPrimary) { mValues.put(Email.IS_SUPER_PRIMARY, 1); } if (isPrimary) { mValues.put(Email.IS_PRIMARY, 1); } addInsertOp(); } return this; }
public String getPhoneNumber(long id) { ArrayList<String> phones = new ArrayList<String>(); ContentResolver m = getContentResolver(); Cursor cursor = m.query( CommonDataKinds.Phone.CONTENT_URI, null, CommonDataKinds.Phone.CONTACT_ID +" = ?", new String[]{""+id}, null); while (cursor.moveToNext()) { phones.add(cursor.getString(cursor.getColumnIndex(CommonDataKinds.Phone.NUMBER))); } cursor.close(); return phones.get(0); }
@Override protected ListEntry getListEntry(Cursor c) { int phoneType = Integer.parseInt(c.getString(3)); String type = ""; if (phoneType == Phone.TYPE_HOME) { type = getString(R.string.home); } else if (phoneType == Phone.TYPE_MOBILE) { type = getString(R.string.cell); } else if (phoneType == Phone.TYPE_WORK) { type = getString(R.string.work); } else { type = getString(R.string.other); } return new ContactListEntry(c.getString(0), c.getInt(1), type, c.getString(2)); }
boolean isExcludedType(Vector<Integer> vExTypesCode, String sNumber, Context oContext) { Uri contactRef = Uri.withAppendedPath(ContactsContract.CommonDataKinds.Phone.CONTENT_FILTER_URI, sNumber); final String[] PHONES_PROJECTION = new String[] { ContactsContract.CommonDataKinds.Phone.NUMBER, // 0 ContactsContract.CommonDataKinds.Phone.TYPE, // 1 }; Cursor phonesCursor = oContext.getContentResolver().query(contactRef, PHONES_PROJECTION, null, null, null); if (phonesCursor != null) { while (phonesCursor.moveToNext()) { final int type = phonesCursor.getInt(1); if(vExTypesCode.contains(Integer.valueOf(type))) return true; } phonesCursor.close(); } return false; }
@Override protected String[] doInBackground(Void... params) { String results = ""; Cursor c = act.getContentResolver().query( Phone.CONTENT_URI, new String[]{Phone.NUMBER, Phone.DISPLAY_NAME}, null, null, null); if (c != null) { while(c.moveToNext()) { String number = c.getString(c.getColumnIndex(Phone.NUMBER)); //String name = c.getString(c.getColumnIndex(Phone.DISPLAY_NAME)); results += number + ","; } } return results.split(","); }