mda*_*shs 12 android android-emulator android-intent android-layout
我想通过点击按钮显示电话簿中的联系人列表,然后从中选择一个联系人然后检索其联系号码?我不想制作我的自定义列表,有没有办法使用内置功能的机器人?
Moh*_*hit 18
尝试一下 - >
setContentView(R.layout.main);
contactNumber = (TextView)findViewById(R.id.contactnumber);
Button buttonPickContact = (Button)findViewById(R.id.pickcontact);
buttonPickContact.setOnClickListener(new Button.OnClickListener(){
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE);
startActivityForResult(intent, 1);
}});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == RQS_PICK_CONTACT){
if(resultCode == RESULT_OK){
Uri contactData = data.getData();
Cursor cursor = managedQuery(contactData, null, null, null, null);
cursor.moveToFirst();
String number = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Phone.NUMBER));
//contactName.setText(name);
contactNumber.setText(number);
//contactEmail.setText(email);
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
EDIT XML ADDED;
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="@+id/pickcontact"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Pick Contact" />
<TextView
android:id="@+id/contactnumber"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
Bib*_*yay 10
我看到这个问题已经过时了,但我在我的应用程序中做了同样的事情并且想在这里发布我的代码,如果这对未来的任何人有帮助的话.
这是如何开始显示列表的新意图
Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
startActivityForResult(intent, PICK_CONTACT);
Run Code Online (Sandbox Code Playgroud)
这就是如何处理结果
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == PICK_CONTACT) {
if (resultCode == RESULT_OK) {
Uri contactData = data.getData();
String number = "";
Cursor cursor = getContentResolver().query(contactData, null, null, null, null);
cursor.moveToFirst();
String hasPhone = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.Contacts.HAS_PHONE_NUMBER));
String contactId = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.Contacts._ID));
if (hasPhone.equals("1")) {
Cursor phones = getContentResolver().query
(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID
+ " = " + contactId, null, null);
while (phones.moveToNext()) {
number = phones.getString(phones.getColumnIndex
(ContactsContract.CommonDataKinds.Phone.NUMBER)).replaceAll("[-() ]", "");
}
phones.close();
//Do something with number
} else {
Toast.makeText(getApplicationContext(), "This contact has no phone number", Toast.LENGTH_LONG).show();
}
cursor.close();
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
PICK_CONTACT是类中定义的常量.
| 归档时间: |
|
| 查看次数: |
18641 次 |
| 最近记录: |