我想选择构建一个应用程序,其中包括从本机联系人中选择电子邮件ID。如何在应用程序内获取所选的电子邮件值。如何实现这一点任何独立的示例都将非常有用。谢谢。
答案 0 :(得分:1)
实施联系人列表,如下所示:
public class contDetail extends Activity implements OnItemClickListener {
private static final String[] PROJECTION = new String[] {
ContactsContract.Contacts._ID,
ContactsContract.Contacts.DISPLAY_NAME,
ContactsContract.Contacts.HAS_PHONE_NUMBER,
ContactsContract.Contacts.LOOKUP_KEY
};
private int mIdColumnIndex;
private int mHasPhoneColumnIndex;
private TextView mPhone;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.pickcontact);
mPhone = (TextView) findViewById(R.id.test);
ListView myList=(ListView)findViewById(R.id.list1);
Cursor c = managedQuery(ContactsContract.Contacts.CONTENT_URI,
PROJECTION, null, null, null);
mIdColumnIndex = c.getColumnIndex(ContactsContract.Contacts._ID);
mHasPhoneColumnIndex = c.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER);
ListAdapter adapter = new SimpleCursorAdapter(this,
android.R.layout.simple_list_item_1,c,
new String[] { ContactsContract.Contacts.HAS_PHONE_NUMBER },
new int[] { android.R.id.text1 });
myList.setAdapter(adapter);
myList.setOnItemClickListener(this);
//setOnItemSelectedListener(this);
}
public void onItemClick(AdapterView<?>parent, View v, int position, long id) {
if (position >= 0) {
final Cursor c = (Cursor) parent.getItemAtPosition(position);
if (c.getInt(mHasPhoneColumnIndex) > 0) {
final long contactId = c.getLong(mIdColumnIndex);
final Cursor phones = getContentResolver().query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
new String[] { ContactsContract.CommonDataKinds.Phone.NUMBER },
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + "=" + contactId, null,
ContactsContract.CommonDataKinds.Phone.IS_SUPER_PRIMARY + " DESC");
try {
phones.moveToFirst();
mPhone.setText(phones.getString(0));
String ph = phones.getString(0);
Intent intent = this.getIntent();
intent.putExtra("SOMETHING",ph );
this.setResult(RESULT_OK, intent);
finish();
} finally {
phones.close();
}
} else {
mPhone.setText(R.string.button_info_text);
}}
}
public void onNothingSelected(AdapterView parent) {
mPhone.setText(R.string.button_info_text);
}
}
将活动的EditText框中的所选联系人复制如下:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(resultCode==RESULT_OK && requestCode==1)
ed1.setText(data.getStringExtra("SOMETHING"));
if(resultCode==RESULT_OK && requestCode==2)
ed2.setText(data.getStringExtra("SOMETHING"));
}