我的目标是只向用户显示带有电话号码的联系人,让用户选择我想在本地存储的少数联系人。
我在下面的方法中使用了各种选项代替ContactsContract.Contacts.CONTENT_URI。但是我收到很多联系人(很多都是垃圾邮件,只有电子邮件ID)。
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.contact_selector);
((Button)findViewById(R.id.btnphonecontactlist)).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent contactIntent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
startActivityForResult(contactIntent, 1);
}
});
}
如果我将ContactsContract.Contacts.CONTENT_URI作为上述方法的参数传递,并且在下面的处理程序方法的情况下,将查询方法的String []作为投影参数(显示为注释),则该方法将失败并返回java.lang。 IllegalArgumentException异常。如果我在下面的方法中传递null,那么无论我选择什么联系人,我都找不到任何与电话号码或电子邮件相关的列。
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if(data != null)
{
Uri uri = data.getData();
if(uri != null)
{
Cursor c = null;
try
{
c = getContentResolver().query(uri, null
// new String[] {
//ContactsContract.CommonDataKinds.Phone.NUMBER,
//ContactsContract.CommonDataKinds.Phone.TYPE},
, null, null, null);
if(c != null && c.moveToFirst())
{
String number = c.getString(0);
String type = c.getString(1);
}
}
finally
{
if(c != null && !c.isClosed())
c.close();
}
}
}
}
有没有办法只显示用户可以看到的联系人,通常是当用户转到电话簿并且有可用的电话号码时?
我尝试了遍历stackoverflow和其他网站中的所有线程,但找不到解决此问题的任何解决方案,尽管很多人都发布了这个问题。我没有在Android平台上工作太多,我可能错过了一些细微的细节,我相信必须有一个简单的方法来实现这一点。
请建议。感谢您的帮助。
感谢。
答案 0 :(得分:18)
请使用以下代码
Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
intent.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_TYPE);
startActivityForResult(intent, 1);
答案 1 :(得分:2)
*-> Add a permission to read contacts data to your application manifest.
<uses-permission android:name="android.permission.READ_CONTACTS"/>
-> Use Intent.ACTION_PICK in your Activity
Intent contactPickerIntent = new Intent(Intent.ACTION_PICK,
ContactsContract.CommonDataKinds.Phone.CONTENT_URI);
startActivityForResult(contactPickerIntent, RESULT_PICK_CONTACT);
-> Then Override the onActivityResult() and retrieve the ID,Phone number and Name in the data.
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// check whether the result is ok
if (resultCode == RESULT_OK) {
// Check for the request code, we might be usign multiple startActivityForReslut
switch (requestCode) {
case RESULT_PICK_CONTACT:
Cursor cursor = null;
try {
String phoneNo = null ;
String name = null;
Uri uri = data.getData();
cursor = getContentResolver().query(uri, null, null, null, null);
cursor.moveToFirst();
int phoneIndex =cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
phoneNo = cursor.getString(phoneIndex);
textView2.setText(phoneNo);
} catch (Exception e) {
e.printStackTrace();
}
break;
}
} else {
Log.e("MainActivity", "Failed to pick contact");
}
}
This will work check it out*
答案 2 :(得分:1)
以下代码将执行您想要的操作。
Intent intent = new Intent(Intent.ACTION_PICK,ContactsContract.CommonDataKinds.Phone.CONTENT_URI);
startActivityForResult(Intent.createChooser(intent, "Contact"), PICK_CONTACT);
答案 3 :(得分:1)
Uri uri = Uri.parse("content://contacts");
Intent intent = new Intent(Intent.ACTION_PICK, uri);
intent.setType(Phone.CONTENT_TYPE);
startActivityForResult(intent, REQUEST_CODE);
答案 4 :(得分:0)
使用此:
Intent intent = new Intent(Intent.ACTION_PICK, Uri.parse("content://contacts/people"));
intent.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_TYPE);
startActivityForResult(intent, 1);