android.provider.ContactsContract.CommonDataKinds.Phone Java Examples

The following examples show how to use android.provider.ContactsContract.CommonDataKinds.Phone. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: Contact.java    From experimental-fall-detector-android-app with MIT License 6 votes vote down vote up
@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();
    }
}
 
Example #2
Source File: ContactsCursorLoader.java    From call_manage with MIT License 6 votes vote down vote up
/**
 * Builds contact uri by given name and phone number
 *
 * @param phoneNumber
 * @param contactName
 * @return Builder.build()
 */
private static Uri buildUri(String phoneNumber, String contactName) {
    Uri.Builder builder;
    if (phoneNumber != null && !phoneNumber.isEmpty()) {
        builder = Uri.withAppendedPath(Phone.CONTENT_FILTER_URI, Uri.encode(phoneNumber)).buildUpon();
        builder.appendQueryParameter(ContactsContract.STREQUENT_PHONE_ONLY, "true");
    } else if (contactName != null && !contactName.isEmpty()) {
        builder = Uri.withAppendedPath(Phone.CONTENT_FILTER_URI, Uri.encode(contactName)).buildUpon();
        builder.appendQueryParameter(ContactsContract.PRIMARY_ACCOUNT_NAME, "true");
    } else {
        builder = Phone.CONTENT_URI.buildUpon();
    }

    builder.appendQueryParameter(Contacts.EXTRA_ADDRESS_BOOK_INDEX, "true");
    builder.appendQueryParameter(ContactsContract.REMOVE_DUPLICATE_ENTRIES, "true");
    return builder.build();
}
 
Example #3
Source File: CallLog.java    From android_9.0.0_r45 with Apache License 2.0 6 votes vote down vote up
private static void updateNormalizedNumber(Context context, ContentResolver resolver,
        String dataId, String number) {
    if (TextUtils.isEmpty(number) || TextUtils.isEmpty(dataId)) {
        return;
    }
    final String countryIso = getCurrentCountryIso(context);
    if (TextUtils.isEmpty(countryIso)) {
        return;
    }
    final String normalizedNumber = PhoneNumberUtils.formatNumberToE164(number,
            getCurrentCountryIso(context));
    if (TextUtils.isEmpty(normalizedNumber)) {
        return;
    }
    final ContentValues values = new ContentValues();
    values.put(Phone.NORMALIZED_NUMBER, normalizedNumber);
    resolver.update(Data.CONTENT_URI, values, Data._ID + "=?", new String[] {dataId});
}
 
Example #4
Source File: ContactsHelper.java    From NonViewUtils with Apache License 2.0 6 votes vote down vote up
/**
 * 得到手机通讯录联系人信息
 */
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();
    }
}
 
Example #5
Source File: ContactAccessor.java    From mollyim-android with GNU General Public License v3.0 6 votes vote down vote up
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;
}
 
Example #6
Source File: List7.java    From codeexamples-android with Eclipse Public License 1.0 6 votes vote down vote up
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);
    }
}
 
Example #7
Source File: ContactUtils.java    From call_manage with MIT License 6 votes vote down vote up
/**
 * Open contact edit page in default contacts app by contact's id
 *
 * @param activity
 * @param number
 */
public static void openContactToEditByNumber(Activity activity, String number) {
    try {
        long contactId = ContactUtils.getContactByPhoneNumber(activity, number).getContactId();
        Uri uri = ContentUris.withAppendedId(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                contactId);
        Intent intent = new Intent(Intent.ACTION_EDIT);
        intent.setDataAndType(uri, ContactsContract.Contacts.CONTENT_ITEM_TYPE);
        intent.putExtra("finishActivityOnSaveCompleted", true);
        //add the below line
        intent.addFlags(FLAG_ACTIVITY_CLEAR_TOP);
        activity.startActivityForResult(intent, 1);
    } catch (Exception e) {
        Toast.makeText(activity, "Oops there was a problem trying to open the contact :(", Toast.LENGTH_SHORT).show();
    }
}
 
Example #8
Source File: ContactsSyncAdapterService.java    From haxsync with GNU General Public License v2.0 6 votes vote down vote up
private static HashMap<String, Long> loadPhoneContacts(Context c){
	mContentResolver = c.getContentResolver();
	HashMap<String, Long> contacts = new HashMap<String, Long>();
	Cursor cursor = mContentResolver.query(
			Phone.CONTENT_URI,
			   new String[]{Phone.DISPLAY_NAME, Phone.RAW_CONTACT_ID},
			   null,
			   null,
			   null);
	while (cursor.moveToNext()) {
		contacts.put(cursor.getString(cursor.getColumnIndex(Phone.DISPLAY_NAME)), cursor.getLong(cursor.getColumnIndex(Phone.RAW_CONTACT_ID)));
		//names.add(cursor.getString(cursor.getColumnIndex(Phone.DISPLAY_NAME)));
	}
	cursor.close();
	return contacts;	
}
 
Example #9
Source File: ContactAccessor.java    From Silence with GNU General Public License v3.0 6 votes vote down vote up
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;
}
 
Example #10
Source File: List7.java    From codeexamples-android with Eclipse Public License 1.0 6 votes vote down vote up
@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);
}
 
Example #11
Source File: ExpandableList2.java    From codeexamples-android with Eclipse Public License 1.0 6 votes vote down vote up
@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);
}
 
Example #12
Source File: ExpandableList2.java    From codeexamples-android with Eclipse Public License 1.0 6 votes vote down vote up
@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;
}
 
Example #13
Source File: ContactAccessor.java    From Silence with GNU General Public License v3.0 6 votes vote down vote up
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;
}
 
Example #14
Source File: ContactsHelper.java    From AndroidBase with Apache License 2.0 6 votes vote down vote up
/**
 * 得到手机通讯录联系人信息
 */
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();
    }
}
 
Example #15
Source File: ContactsHelper.java    From zone-sdk with MIT License 6 votes vote down vote up
/**
 * 得到手机通讯录联系人信息
 */
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();
    }
}
 
Example #16
Source File: SelectContactModule.java    From react-native-select-contact with MIT License 5 votes vote down vote up
private void addPhoneEntry(WritableArray phones, Cursor cursor, Activity activity) {
    String phoneNumber = cursor.getString(cursor.getColumnIndex(Phone.NUMBER));
    int phoneType = cursor.getInt(cursor.getColumnIndex(Phone.TYPE));
    String phoneLabel = cursor.getString(cursor.getColumnIndex(Phone.LABEL));
    CharSequence typeLabel = Phone.getTypeLabel(activity.getResources(), phoneType, phoneLabel);

    WritableMap phoneEntry = Arguments.createMap();
    phoneEntry.putString("number", phoneNumber);
    phoneEntry.putString("type", String.valueOf(typeLabel));

    phones.pushMap(phoneEntry);
}
 
Example #17
Source File: RecipientAlternatesAdapter.java    From ChipsLibrary with Apache License 2.0 5 votes vote down vote up
private static Cursor doQuery(final CharSequence constraint,final int limit,final Long directoryId,final Account account,final ContentResolver resolver,final Query query)
{
String constraintStr=constraint.toString();
final Uri.Builder builder;
String selection=null;
String[] selectionArgs=null;
if(query!=Queries.PHONE)
  builder=query.getContentFilterUri().buildUpon().appendPath(constraintStr).appendQueryParameter(ContactsContract.LIMIT_PARAM_KEY,String.valueOf(limit+BaseRecipientAdapter.ALLOWANCE_FOR_DUPLICATES));
else
  {
  builder=query.getContentUri().buildUpon().appendQueryParameter(ContactsContract.LIMIT_PARAM_KEY,String.valueOf(limit+BaseRecipientAdapter.ALLOWANCE_FOR_DUPLICATES));
  selection=Contacts.DISPLAY_NAME+" LIKE ? OR "+Phone.NUMBER+" LIKE ?";
  constraintStr="%"+constraintStr+"%";
  selectionArgs=new String[] {constraintStr,constraintStr};
  }
if(directoryId!=null)
  builder.appendQueryParameter(ContactsContract.DIRECTORY_PARAM_KEY,String.valueOf(directoryId));
if(account!=null)
  {
  builder.appendQueryParameter(BaseRecipientAdapter.PRIMARY_ACCOUNT_NAME,account.name);
  builder.appendQueryParameter(BaseRecipientAdapter.PRIMARY_ACCOUNT_TYPE,account.type);
  }
// final long start = System.currentTimeMillis();
final Uri uri=builder.build();
final Cursor cursor=resolver.query(uri,query.getProjection(),selection,selectionArgs,null);
// final long end = System.currentTimeMillis();
// if (DEBUG) {
// Log.d(TAG, "Time for autocomplete (query: " + constraint + ", directoryId: " + directoryId
// + ", num_of_results: " + (cursor != null ? cursor.getCount() : "null") + "): " + (end - start)
// + " ms");
// }
return cursor;
}
 
Example #18
Source File: ContactPhoneAdapter.java    From Contacts with MIT License 5 votes vote down vote up
@Override
public void bindData(View view, BaseType data, int position)
{
	final ContactPhoneHolder holder = (ContactPhoneHolder) getHolder(view);

	final ContactPhone phone = ((ContactPhone) data);

	PhoneNumberUtil phoneUtil = PhoneNumberUtil.getInstance();
	
	try
	{
		PhoneNumber numberProto = phoneUtil.parse(phone.phoneNumber, "");
		holder.primaryText.setText(phoneUtil.format(numberProto, PhoneNumberFormat.NATIONAL));
	}
	catch (NumberParseException e)
	{
		holder.primaryText.setText(phone.phoneNumber);
	}

	holder.secondaryText.setText(Phone.getTypeLabel(holder.secondaryText.getResources(), phone.type, phone.label));
	
	holder.messageButton.setOnClickListener(new View.OnClickListener() {
		
		@Override
		public void onClick(View v)
		{
			PhoneUtil.sendSMS(v.getContext(), phone.phoneNumber);
		}
	});
}
 
Example #19
Source File: ContactUtils.java    From BigApp_Discuz_Android with Apache License 2.0 5 votes vote down vote up
public static void insertContact(Context context, String name, String phone) {
	// 首先插入空值,再得到rawContactsId ,用于下面插值
	ContentValues values = new ContentValues();
	// insert a null value
	Uri rawContactUri = context.getContentResolver().insert(
			RawContacts.CONTENT_URI, values);
	long rawContactsId = ContentUris.parseId(rawContactUri);

	// 往刚才的空记录中插入姓名
	values.clear();
	// A reference to the _ID that this data belongs to
	values.put(StructuredName.RAW_CONTACT_ID, rawContactsId);
	// "CONTENT_ITEM_TYPE" MIME type used when storing this in data table
	values.put(Data.MIMETYPE, StructuredName.CONTENT_ITEM_TYPE);
	// The name that should be used to display the contact.
	values.put(StructuredName.DISPLAY_NAME, name);
	// insert the real values
	context.getContentResolver().insert(Data.CONTENT_URI, values);
	// 插入电话
	values.clear();
	values.put(Phone.RAW_CONTACT_ID, rawContactsId);
	// String "Data.MIMETYPE":The MIME type of the item represented by this
	// row
	// String "CONTENT_ITEM_TYPE": MIME type used when storing this in data
	// table.
	values.put(Data.MIMETYPE, Phone.CONTENT_ITEM_TYPE);
	values.put(Phone.NUMBER, phone);
	context.getContentResolver().insert(Data.CONTENT_URI, values);
}
 
Example #20
Source File: ContactUtil.java    From customview-samples with Apache License 2.0 5 votes vote down vote up
/**
 * 全量获取手机联系人信息
 * @return
 */
private static List<PhoneInfo> getMobileContactInner() {
    List list = new ArrayList<PhoneInfo>();
    Cursor cursor = null;
    ContentResolver contentResolver = MyApplication.getApplication().getContentResolver();
    if(sMobileContactObserver == null){
        Log.d("hyh", "ContactUtil: getMobileContactInner: 注册MobileContactObserver");
        sMobileContactObserver = new MobileContactObserver(null);
        contentResolver.registerContentObserver(Phone.CONTENT_URI,false,sMobileContactObserver);
    }
    try {
        cursor = contentResolver.query(Phone.CONTENT_URI,
            null, null, null, null);
        while (cursor.moveToNext()) {
            //读取通讯录的姓名
            String name = cursor.getString(cursor
                .getColumnIndex(Phone.DISPLAY_NAME));

            //读取通讯录的号码
            String number = cursor.getString(cursor
                .getColumnIndex(Phone.NUMBER));

            //联系人id,不过同一个联系人有可能存多个号码,所以会存在不同号码对应相同id的情况
            String contactId = cursor.getString(cursor
                .getColumnIndex(Phone.CONTACT_ID));

            String version = cursor.getString(cursor.getColumnIndex(ContactsContract.RawContacts.VERSION));

            PhoneInfo phoneInfo = new PhoneInfo(contactId,version,name,number);
            list.add(phoneInfo);
        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (cursor != null) {
            cursor.close();
        }
    }
    return list;
}
 
Example #21
Source File: ContactAccessor.java    From mollyim-android with GNU General Public License v3.0 5 votes vote down vote up
public Set<String> getAllContactsWithNumbers(Context context) {
  Set<String> results = new HashSet<>();

  try (Cursor cursor = context.getContentResolver().query(Phone.CONTENT_URI, new String[] {Phone.NUMBER}, null ,null, null)) {
    while (cursor != null && cursor.moveToNext()) {
      if (!TextUtils.isEmpty(cursor.getString(0))) {
        results.add(PhoneNumberFormatter.get(context).format(cursor.getString(0)));
      }
    }
  }

  return results;
}
 
Example #22
Source File: ContactUtils.java    From BigApp_Discuz_Android with Apache License 2.0 5 votes vote down vote up
public void getUserInfo(Context context) {
	Cursor cursor = context.getContentResolver().query(
			ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
	while (cursor.moveToNext()) {
		String id = cursor.getString(cursor
				.getColumnIndex(ContactsContract.Contacts._ID));
		String name = cursor.getString(cursor
				.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
		// Log.d(TAG , "Name is : "+name);
		int isHas = Integer
				.parseInt(cursor.getString(cursor
						.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER)));
		if (isHas > 0) {
			Cursor c = context.getContentResolver().query(
					ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
					null,
					ContactsContract.CommonDataKinds.Phone.CONTACT_ID
							+ " = " + id, null, null);
			while (c.moveToNext()) {
				String number = c
						.getString(c
								.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
				// Log.d(TAG , "Number is : "+number);
			}
			c.close();
		}
	}
	cursor.close();
}
 
Example #23
Source File: ContactUtils.java    From BigApp_Discuz_Android with Apache License 2.0 5 votes vote down vote up
public static ArrayList<String> getNameFromPhone(Context context,
		String number) {
	ArrayList<String> name = new ArrayList<String>();
	String[] projection = { ContactsContract.PhoneLookup.DISPLAY_NAME,
			ContactsContract.CommonDataKinds.Phone.NUMBER };

	Cursor cursor = context.getContentResolver().query(
			ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
			projection, // Which columns to return.
			ContactsContract.CommonDataKinds.Phone.NUMBER + " = '" + number
					+ "'", // WHERE clause.
			null, // WHERE clause value substitution
			null); // Sort order.

	if (cursor == null) {
		// Log.d(TAG, "getPeople null");
		return null;
	}
	// Log.d(TAG, "getPeople cursor.getCount() = " + cursor.getCount());
	for (int i = 0; i < cursor.getCount(); i++) {
		cursor.moveToPosition(i);

		int nameFieldColumnIndex = cursor
				.getColumnIndex(ContactsContract.PhoneLookup.DISPLAY_NAME);
		String nameTemp = cursor.getString(nameFieldColumnIndex);
		// Log.i(TAG, "" + name + " .... " + nameFieldColumnIndex);
		name.add(nameTemp);
	}
	cursor.close();
	return name;

}
 
Example #24
Source File: ContactUtils.java    From BigApp_Discuz_Android with Apache License 2.0 5 votes vote down vote up
/**
 * 
 * @Title getPhoneFromName
 * @Description 根据姓名取得手机号码
 * @param
 * @return String 返回类型
 */
public static ArrayList<String> getPhoneFromName(Context context,
		String name) {
	ArrayList<String> phone = new ArrayList<String>();
	String[] projection = { ContactsContract.PhoneLookup.DISPLAY_NAME,
			ContactsContract.CommonDataKinds.Phone.NUMBER };
	String[] selectionArgs = new String[] { "%" + name + "%" };
	Cursor cursor = context.getContentResolver().query(
			ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
			projection, // Which columns to return.
			ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME
					+ " like ? ", // WHERE clause.
			selectionArgs, // WHERE clause value substitution
			null); // Sort order.

	if (cursor == null) {
		// Log.d(TAG, "getPeople null");
		return null;
	}
	ZogUtils.printLog(ContactUtils.class, "getPeople cursor.getCount() = "
               + cursor.getCount());
	for (int i = 0; i < cursor.getCount(); i++) {
		cursor.moveToPosition(i);

		int nameFieldColumnIndex = cursor
				.getColumnIndex(ContactsContract.PhoneLookup.DISPLAY_NAME);
		name = cursor.getString(nameFieldColumnIndex);

		String phoneNumber = cursor
				.getString(cursor
						.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
		// Log.i(TAG, "" + name + " .... " + nameFieldColumnIndex);

		phone.add(phoneNumber);

	}
	cursor.close();
	return phone;

}
 
Example #25
Source File: ContactOperations.java    From tindroid with Apache License 2.0 5 votes vote down vote up
/**
 * Adds a phone number
 *
 * @param phone     new phone number for the contact
 * @param phoneType the type: cell, home, etc.
 * @return instance of ContactOperations
 */
ContactOperations addPhone(final String phone, int phoneType) {
    mValues.clear();
    if (!TextUtils.isEmpty(phone)) {
        mValues.put(Phone.NUMBER, phone);
        mValues.put(Phone.TYPE, phoneType);
        mValues.put(Phone.MIMETYPE, Phone.CONTENT_ITEM_TYPE);
        addInsertOp();
    }
    return this;
}
 
Example #26
Source File: ContactOperations.java    From tindroid with Apache License 2.0 5 votes vote down vote up
/**
 * Updates contact's phone
 *
 * @param existingNumber phone number stored in contacts provider
 * @param phone          new phone number for the contact
 * @param uri            Uri for the existing raw contact to be updated
 * @return instance of ContactOperations
 */
ContactOperations updatePhone(String existingNumber, String phone, Uri uri) {
    mValues.clear();
    if (!TextUtils.equals(phone, existingNumber)) {
        mValues.put(Phone.NUMBER, phone);
        addUpdateOp(uri);
    }
    return this;
}
 
Example #27
Source File: ContactOperations.java    From tindroid with Apache License 2.0 5 votes vote down vote up
/**
 * Adds an insert operation into the batch
 */
private void addInsertOp() {
    if (!mIsNewContact) {
        mValues.put(Phone.RAW_CONTACT_ID, mRawContactId);
    }
    ContentProviderOperation.Builder builder = newInsertCpo(Data.CONTENT_URI, mIsSyncContext).withValues(mValues);
    if (mIsNewContact) {
        builder.withValueBackReference(Data.RAW_CONTACT_ID, mBackReference);
    }

    mBatchOperation.add(builder.build());
}
 
Example #28
Source File: ContactsManager.java    From tindroid with Apache License 2.0 5 votes vote down vote up
private static int vcardTypeToDbType(VxCard.ContactType tp) {
    switch (tp) {
        case MOBILE:
            return Phone.TYPE_MOBILE;
        case HOME:
        case PERSONAL:
            return Phone.TYPE_HOME;
        case WORK:
        case BUSINESS:
            return Phone.TYPE_WORK;
    }
    return Phone.TYPE_OTHER;
}
 
Example #29
Source File: BaseRecipientAdapter.java    From ChipsLibrary with Apache License 2.0 5 votes vote down vote up
private Cursor doQuery(final CharSequence constraint,final int limit,final Long directoryId)
{
String constraintStr=constraint.toString();
final Uri.Builder builder;
String selection=null;
String[] selectionArgs=null;
if(mQuery!=Queries.PHONE)
  builder=mQuery.getContentFilterUri().buildUpon().appendPath(constraintStr);
else
  {
  builder=mQuery.getContentUri().buildUpon();
  // search for either contact name or its phone number
  selection=Contacts.DISPLAY_NAME+" LIKE ? OR "+Phone.NUMBER+" LIKE ?";
  constraintStr="%"+constraintStr+"%";
  selectionArgs=new String[] {constraintStr,constraintStr};
  }
builder.appendQueryParameter(ContactsContract.LIMIT_PARAM_KEY,String.valueOf(limit+ALLOWANCE_FOR_DUPLICATES));
if(directoryId!=null)
  builder.appendQueryParameter(ContactsContract.DIRECTORY_PARAM_KEY,String.valueOf(directoryId));
if(mAccount!=null)
  {
  builder.appendQueryParameter(PRIMARY_ACCOUNT_NAME,mAccount.name);
  builder.appendQueryParameter(PRIMARY_ACCOUNT_TYPE,mAccount.type);
  }
// final long start = System.currentTimeMillis();
final Uri uri=builder.build();
final Cursor cursor=mContentResolver.query(uri,mQuery.getProjection(),selection,selectionArgs,null);
// final long end = System.currentTimeMillis();
// if (DEBUG) {
// Log.d(TAG, "Time for autocomplete (query: " + constraint + ", directoryId: " + directoryId
// + ", num_of_results: " + (cursor != null ? cursor.getCount() : "null") + "): " + (end - start)
// + " ms");
// }
return cursor;
}
 
Example #30
Source File: SpeedDialAdapter.java    From emerald-dialer with GNU General Public License v3.0 5 votes vote down vote up
@Override
public View getView(int position, View convertView, ViewGroup parent) {
	View view;
	
	if (convertView == null) {
		view = LayoutInflater.from(contextRef.get()).inflate(R.layout.speed_dial_entry, null);
	} else {
		view = convertView;
	}
	
	String order = (new Integer(position+1)).toString();
	((TextView)view.findViewById(R.id.entry_order)).setText(order);
	String number = SpeedDial.getNumber(contextRef.get(), order);
	String result;
	if (position == 0) {
		result = contextRef.get().getResources().getString(R.string.voice_mail);
		((TextView)view.findViewById(R.id.entry_title)).setText(result);
		return view;
	}
	String contactName = null;
	Cursor cursor = contextRef.get().getContentResolver().query(Phone.CONTENT_URI, new String[]{Phone.DISPLAY_NAME}, Phone.NUMBER + "=?", new String[]{number}, null);
	if (cursor != null) {
		if (cursor.getCount() > 0) {
			cursor.moveToFirst();
			contactName = cursor.getString(0);
			cursor.close();
		}
	}
	if (null == contactName) {
		result = !number.equals("") ? number : contextRef.get().getResources().getString(R.string.tap_for_addition);
	} else {
		result = contactName+" ("+number+")";
	}
	((TextView)view.findViewById(R.id.entry_title)).setText(result);
	return view;
}