在我的应用程序中,我想显示手机中的所有联系人号码。我写了代码。但它没有用。请任何人帮助我。 这是我的代码:
public class MobileFragment extends Fragment{
iv_contacts = (ImageView) rootView.findViewById(R.id.iv_contacts);
ic_contacts.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
browseContacts();
}
});
}
private void browseContacts() {
Intent pickContactIntent = new Intent(Intent.ACTION_PICK,
Uri.parse("content://contacts"));
pickContactIntent.setType(Phone.CONTENT_TYPE); // Show user only
startActivityForResult(pickContactIntent, PICK_CONTACT_REQUEST);
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == PICK_CONTACT_REQUEST) {
if (resultCode == getActivity().RESULT_OK) {
Uri contactUri = data.getData();
String[] projection = { Phone.NUMBER };
Cursor cursor = getActivity().getContentResolver().query(contactUri, projection, null, null, null);
cursor.moveToFirst();
// Retrieve the phone number from the NUMBER column
int column = cursor.getColumnIndex(Phone.NUMBER);
String number = cursor.getString(column);
number = number.replace(" ", "");
}
}
}
这里我的onActivity结果没有在我的片段中调用。