我在网上看到大量帖子显示“如何打开 Android 联系人列表”,但它们都已过时,有些代码甚至不再工作。
编辑:
现在有以下内容:
public class MessagePreview extends Activity {
final int PICK_CONTACT = 3;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button recipientContact = (Button) findViewById(R.id.buttonAddRecipient);
recipientContact.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(Intent.ACTION_PICK,
ContactsContract.Contacts.CONTENT_URI);
startActivityForResult(intent, PICK_CONTACT);
}
});
}
@Override
public void onActivityResult(int reqCode, int resultCode, Intent data) {
super.onActivityResult(reqCode, resultCode, data);
switch (reqCode) {
case (PICK_CONTACT):
if (resultCode == Activity.RESULT_OK) {
Uri contactData = data.getData();
Cursor c = managedQuery(contactData, null, null, null, null);
if (c.moveToFirst()) {
String name = c.getString(c
.getColumnIndexOrThrow(People.NAME));
// TODO Whatever you want to do with the selected contact
// name.
}
}
break;
}
}
}
没有错误,但它确实说
- 字段 Contacts.PeopleColumns.NAME 已弃用
- Contacts.People 类型已弃用
- 字段 Contacts.PeopleColumns.NAME 已弃用
- Contacts.People 类型已弃用
在这条线上:
字符串名称 = c.getString(c.getColumnIndexOrThrow(People.NAME));
我必须在该行之后添加更多代码,但我不知道要添加什么。