下面列出了android.provider.Contacts#android.provider.Contacts.People 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
public void bindContactPhoneView(View view, Context context, Cursor cursor) {
// Get values
String value = cursor.getString(cursor.getColumnIndex(Phones.NUMBER));
String displayName = cursor.getString(cursor.getColumnIndex(Phones.DISPLAY_NAME));
Long peopleId = cursor.getLong(cursor.getColumnIndex(Phones.PERSON_ID));
Uri uri = ContentUris.withAppendedId(People.CONTENT_URI, peopleId);
Bitmap bitmap = getContactPhoto(context, uri, false, R.drawable.ic_contact_picture_holo_dark);
// Get views
TextView tv = (TextView) view.findViewById(R.id.name);
TextView sub = (TextView) view.findViewById(R.id.number);
TextView label = (TextView) view.findViewById(R.id.label);
ImageView imageView = (ImageView) view.findViewById(R.id.contact_photo);
// Bind
label.setVisibility(View.GONE);
view.setTag(value);
tv.setText(displayName);
sub.setText(value);
imageView.setImageBitmap(bitmap);
}
public void create(Contact newPhoneContact) throws Exception {
// first, we have to create the contact
ContentValues newPhoneValues = new ContentValues();
newPhoneValues.put(Contacts.People.NAME, newPhoneContact.getName());
Uri newPhoneRow = resolver.insert(Contacts.People.CONTENT_URI, newPhoneValues);
// then we have to add a number
newPhoneValues.clear();
newPhoneValues.put(Contacts.People.Phones.TYPE, Contacts.People.Phones.TYPE_MOBILE);
newPhoneValues.put(Contacts.Phones.NUMBER, newPhoneContact.getNumber());
// insert the new phone number in the database using the returned uri from creating the contact
newPhoneRow = resolver.insert(Uri.withAppendedPath(newPhoneRow, Contacts.People.Phones.CONTENT_DIRECTORY), newPhoneValues);
// if contacts uri returned, there was an error with adding the number
if (newPhoneRow.getPath().contains("people")) {
throw new Exception(String.valueOf(R.string.error_phone_number_not_stored));
}
// if phone uri returned, everything went OK
if (!newPhoneRow.getPath().contains("phones")) {
// some unknown error has happened
throw new Exception(String.valueOf(R.string.error_phone_number_error));
}
}
public Uri retrieveContactUri(Contact contact) {
String id = contact.getId();
String[] projection = new String[] { Contacts.Phones.PERSON_ID };
String path = null;
Cursor result;
if (null != id) {
Uri uri = ContentUris.withAppendedId(Contacts.Phones.CONTENT_URI, Long.valueOf(id));
result = resolver.query(uri, projection, null, null, null);
} else {
String selection = "name='?' AND number='?'";
String[] selectionArgs = new String[] { contact.getName(), contact.getNumber() };
result = resolver.query(Contacts.Phones.CONTENT_URI, projection, selection, selectionArgs, null);
}
if (null != result) {
result.moveToNext();
path = result.getString(0);
result.close();
}
if (null == path) {
return null;
}
return Uri.withAppendedPath(Contacts.People.CONTENT_URI, path);
}
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
// This is called when a new Loader needs to be created. This
// sample only has one Loader, so we don't care about the ID.
// First, pick the base URI to use depending on whether we are
// currently filtering.
Uri baseUri;
if (mCurFilter != null) {
baseUri = Uri.withAppendedPath(People.CONTENT_FILTER_URI, Uri.encode(mCurFilter));
} else {
baseUri = People.CONTENT_URI;
}
// Now create and return a CursorLoader that will take care of
// creating a Cursor for the data being displayed.
String select = "((" + People.DISPLAY_NAME + " NOTNULL) AND ("
+ People.DISPLAY_NAME + " != '' ))";
return new CursorLoader(getActivity(), baseUri,
CONTACTS_SUMMARY_PROJECTION, select, null,
People.DISPLAY_NAME + " COLLATE LOCALIZED ASC");
}
@Override public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
// Give some text to display if there is no data. In a real
// application this would come from a resource.
setEmptyText("No phone numbers");
// We have a menu item to show in action bar.
setHasOptionsMenu(true);
// Create an empty adapter we will use to display the loaded data.
mAdapter = new SimpleCursorAdapter(getActivity(),
android.R.layout.simple_list_item_1, null,
new String[] { People.DISPLAY_NAME },
new int[] { android.R.id.text1}, 0);
setListAdapter(mAdapter);
// Start out with a progress indicator.
setListShown(false);
// Prepare the loader. Either re-connect with an existing one,
// or start a new one.
getLoaderManager().initLoader(0, null, this);
}
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
// This is called when a new Loader needs to be created. This
// sample only has one Loader, so we don't care about the ID.
// First, pick the base URI to use depending on whether we are
// currently filtering.
Uri baseUri;
if (mCurFilter != null) {
baseUri = Uri.withAppendedPath(People.CONTENT_FILTER_URI, Uri.encode(mCurFilter));
} else {
baseUri = People.CONTENT_URI;
}
// Now create and return a CursorLoader that will take care of
// creating a Cursor for the data being displayed.
String select = "((" + People.DISPLAY_NAME + " NOTNULL) AND ("
+ People.DISPLAY_NAME + " != '' ))";
return new CursorLoader(getActivity(), baseUri,
CONTACTS_SUMMARY_PROJECTION, select, null,
People.DISPLAY_NAME + " COLLATE LOCALIZED ASC");
}
@Override
public Bitmap getContactPhoto(Context ctxt, Uri uri, boolean hiRes, Integer defaultResource) {
Bitmap img = null;
try {
img = People.loadContactPhoto(ctxt, uri, defaultResource != null ? defaultResource : R.drawable.ic_contact_picture_holo_dark, null);
} catch (IllegalArgumentException e) {
Log.w("Contact3", "Failed to find contact photo");
}
return img;
}
public EmailAddressAdapter(Context context) {
super(context, android.R.layout.simple_dropdown_item_1line, null);
contentResolver = context.getContentResolver();
this.context = context;
if (SdkLevel.getLevel() >= SdkLevel.LEVEL_HONEYCOMB_MR1) {
SORT_ORDER = HoneycombMR1Util.getTimesContacted() + " DESC, " + HoneycombMR1Util.getDisplayName();
} else {
SORT_ORDER = People.TIMES_CONTACTED + " DESC, " + People.NAME;
}
}
public ArrayList<Contact> get() {
final String[] phoneProjection = new String[]{
People.NAME,
People.NUMBER,
People._ID
};
Cursor results = resolver.query(
People.CONTENT_URI,
phoneProjection,
null,
null,
People.NAME
);
// create array of Phone contacts and fill it
final ArrayList<Contact> phoneContacts = new ArrayList<>();
if (null != results) {
while (results.moveToNext()) {
final Contact phoneContact = new Contact(
results.getString(results.getColumnIndex(People._ID)),
results.getString(results.getColumnIndex(People.NAME)),
results.getString(results.getColumnIndex(People.NUMBER))
);
phoneContacts.add(phoneContact);
}
results.close();
}
return phoneContacts;
}
/**
* {@inheritDoc}
*/
public View buildUI(DataType initData) {
if (initData != null) {
editText.setText(initData.getValue());
}
ContentResolver cr = activity.getContentResolver();
List<String> contacts = new ArrayList<String>();
// Form an array specifying which columns to return.
String[] projection = new String[] {People.NAME, People.NUMBER };
// Get the base URI for the People table in the Contacts content provider.
Uri contactsUri = People.CONTENT_URI;
// Make the query.
Cursor cursor = cr.query(contactsUri,
projection, // Which columns to return
null, // Which rows to return (all rows)
null, // Selection arguments (none)
Contacts.People.DEFAULT_SORT_ORDER);
if (cursor.moveToFirst()) {
String name;
String phoneNumber;
int nameColumn = cursor.getColumnIndex(People.NAME);
int phoneColumn = cursor.getColumnIndex(People.NUMBER);
do {
// Get the field values of contacts
name = cursor.getString(nameColumn);
phoneNumber = cursor.getString(phoneColumn);
contacts.add(name + ": " + phoneNumber);
} while (cursor.moveToNext());
}
cursor.close();
String[] contactsStr = new String[]{};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(activity,
android.R.layout.simple_dropdown_item_1line, contacts.toArray(contactsStr));
editText.setAdapter(adapter);
editText.setThreshold(1);
return(editText);
}
@Override public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
// In this sample we are going to use a retained fragment.
setRetainInstance(true);
// Give some text to display if there is no data. In a real
// application this would come from a resource.
setEmptyText("No phone numbers");
// We have a menu item to show in action bar.
setHasOptionsMenu(true);
// Create an empty adapter we will use to display the loaded data.
mAdapter = new SimpleCursorAdapter(getActivity(),
android.R.layout.simple_list_item_1, null,
new String[] { People.DISPLAY_NAME },
new int[] { android.R.id.text1}, 0);
setListAdapter(mAdapter);
// Start out with a progress indicator.
setListShown(false);
// Prepare the loader. Either re-connect with an existing one,
// or start a new one.
getLoaderManager().initLoader(0, null, this);
}
@Override
public CallerInfo findCallerInfo(Context ctxt, String number) {
Uri searchUri = Uri
.withAppendedPath(Phones.CONTENT_FILTER_URL, Uri.encode(number));
CallerInfo callerInfo = new CallerInfo();
Cursor cursor = ctxt.getContentResolver().query(searchUri, null, null, null, null);
if (cursor != null) {
try {
if (cursor.getCount() > 0) {
cursor.moveToFirst();
ContentValues cv = new ContentValues();
DatabaseUtils.cursorRowToContentValues(cursor, cv);
callerInfo.contactExists = true;
if (cv.containsKey(Phones.DISPLAY_NAME)) {
callerInfo.name = cv.getAsString(Phones.DISPLAY_NAME);
}
callerInfo.phoneNumber = cv.getAsString(Phones.NUMBER);
if (cv.containsKey(Phones.TYPE)
&& cv.containsKey(Phones.LABEL)) {
callerInfo.numberType = cv.getAsInteger(Phones.TYPE);
callerInfo.numberLabel = cv.getAsString(Phones.LABEL);
callerInfo.phoneLabel = Phones.getDisplayLabel(ctxt,
callerInfo.numberType, callerInfo.numberLabel)
.toString();
}
if (cv.containsKey(Phones.PERSON_ID)) {
callerInfo.personId = cv.getAsLong(Phones.PERSON_ID);
callerInfo.contactContentUri = ContentUris.withAppendedId(
People.CONTENT_URI, callerInfo.personId);
}
if (cv.containsKey(Phones.CUSTOM_RINGTONE)) {
String ringtoneUriString = cv.getAsString(Phones.CUSTOM_RINGTONE);
if (!TextUtils.isEmpty(ringtoneUriString)) {
callerInfo.contactRingtoneUri = Uri.parse(ringtoneUriString);
}
}
if (callerInfo.name != null && callerInfo.name.length() == 0) {
callerInfo.name = null;
}
}
} catch (Exception e) {
Log.e(THIS_FILE, "Exception while retrieving cursor infos", e);
} finally {
cursor.close();
}
}
// if no query results were returned with a viable number,
// fill in the original number value we used to query with.
if (TextUtils.isEmpty(callerInfo.phoneNumber)) {
callerInfo.phoneNumber = number;
}
return callerInfo;
}
@Override
public int getContactIndexableColumnIndex(Cursor c) {
return c.getColumnIndex(People.DISPLAY_NAME);
}