我按照developers.android.com上给出的教程,我成功地使用ContactsContract类检索了所有人的名字。 但我的要求是根据电话号码检索所有人的姓名。
我的ContactsQuery.java如下:
@TargetApi (Build.VERSION_CODES.HONEYCOMB)
public interface ContactsQuery {
final static int QUERY_ID = 1;
final static Uri CONTENT_URI = ContactsContract.Data.CONTENT_URI;
final static Uri FILTER_URI = ContactsContract.CommonDataKinds.Phone.CONTENT_FILTER_URI;
final static String SELECTION = ContactsContract.CommonDataKinds.Phone.NUMBER + " = ? "
+ " AND " +
ContactsContract.CommonDataKinds.Phone.MIMETYPE + " = '" + ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE + "'";
final static String SORT_ORDER = hasHoneycomb () ?
ContactsContract.CommonDataKinds.Phone.SORT_KEY_PRIMARY :
ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME;
final static String[] PROJECTION = {
// The contact's row id
ContactsContract.CommonDataKinds.Phone._ID ,
// A pointer to the contact that is guaranteed to be more permanent than _ID. Given
// a contact's current _ID value and LOOKUP_KEY, the Contacts Provider can generate
// a "permanent" contact URI.
ContactsContract.CommonDataKinds.Phone.LOOKUP_KEY ,
// In platform version 3.0 and later, the Contacts table contains
// DISPLAY_NAME_PRIMARY, which either contains the contact's displayable name or
// some other useful identifier such as an email address. This column isn't
// available in earlier versions of Android, so you must use Contacts.DISPLAY_NAME
// instead.
ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME ,
ContactsContract.CommonDataKinds.Phone.NUMBER
};
final static String[] SELECTION_ARGS = { "7276844531" };
public static final int ID = 0;
public static final int LOOKUP_KEY = 1;
public static final int DISPLAY_NAME = 2;
public static final int SORT_KEY = 3;
}
我的ContactsFragment.java如下:
public class ContactsFragment extends SherlockListFragment implements AdapterView.OnItemClickListener,
LoaderManager.LoaderCallbacks<Cursor> {
public static ContactsFragment getInstance () {return new ContactsFragment ();}
private ContactsAdapter mAdapter;
private Context mContext;
private String[] users;
private String mSearchTerm = String.valueOf ( getAllUsers () );
public ContactsFragment () {}
@Override
public void onCreate (Bundle savedInstanceState) {
super.onCreate ( savedInstanceState );
setHasOptionsMenu ( true );
mContext = getActivity ();
mAdapter = new ContactsAdapter ( mContext );
}
private String[] getAllUsers () {
ParseQuery<ParseUser> query = ParseUser.getQuery ();
query.whereExists ( "phone" );
query.setCachePolicy ( ParseQuery.CachePolicy.CACHE_THEN_NETWORK );
query.findInBackground ( new FindCallback<ParseUser> () {
@Override
public void done (List<ParseUser> parseUsers, ParseException e) {
if ( parseUsers != null ) {
users = new String[parseUsers.size ()];
int index = 0;
for ( ParseUser user : parseUsers ) {
users[index] = String.valueOf ( user );
index++;
}
} else {
Log.e ( "ABHIMANYU", e.getMessage () );
}
}
} );
return users;
}
@Override
public View onCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate ( R.layout.fragment_contact_list, container, false );
}
@Override
public void onActivityCreated (Bundle savedInstanceState) {
super.onActivityCreated ( savedInstanceState );
setListAdapter ( mAdapter );
getListView ().setOnItemClickListener ( this );
getLoaderManager ().initLoader ( ContactsQuery.QUERY_ID, null, this );
}
@Override
public Loader<Cursor> onCreateLoader (int id, Bundle bundle) {
if ( id == ContactsQuery.QUERY_ID ) {
Uri contentUri = Uri.withAppendedPath ( ContactsQuery.FILTER_URI, Uri.encode ( "7276844531" ) );
return new CursorLoader (
mContext,
contentUri,
ContactsQuery.PROJECTION,
ContactsQuery.SELECTION,
ContactsQuery.SELECTION_ARGS,
ContactsQuery.SORT_ORDER
);
}
return null;
}
@Override
public void onLoadFinished (Loader<Cursor> cursorLoader, Cursor cursor) {
if ( cursorLoader.getId () == ContactsQuery.QUERY_ID ) {
mAdapter.swapCursor ( cursor );
}
}
@Override
public void onLoaderReset (Loader<Cursor> cursorLoader) {
if ( cursorLoader.getId () == ContactsQuery.QUERY_ID ) {
mAdapter.swapCursor ( null );
}
}
@TargetApi (Build.VERSION_CODES.ECLAIR)
@Override
public void onItemClick (AdapterView<?> parent, View view, int position, long id) {
final Cursor cursor = mAdapter.getCursor ();
cursor.moveToPosition ( position );
String displayName = cursor.getString ( cursor.getColumnIndexOrThrow ( ContactsContract.Contacts.DISPLAY_NAME
) );
final Uri uri = ContactsContract.Contacts.getLookupUri (
cursor.getLong ( ContactsQuery.ID ),
cursor.getString ( ContactsQuery.LOOKUP_KEY )
);
Log.w ( "ABHIMANYU", "Uri: " + uri.toString () );
Intent intent = new Intent ( mContext, MessagingActivity.class );
intent.setData ( uri );
intent.putExtra ( "name", displayName );
mContext.startActivity ( intent );
}
}
即使我将手机号码作为手机中的选择参数传递,我也没有结果。任何人都可以帮我解决我的问题吗?