问题描述
我的目标是仅向用户显示带有电话号码的联系人,并让用户选择要存储在本地的联系人.
My goal is to only display contacts with phone number to user and let user select few contacts which I want to store locally.
我在以下方法中使用了各种选项来代替ContactsContract.Contacts.CONTENT_URI.但是我看到了很多联系人(很多都是垃圾邮件,只有电子邮件ID).
I have used various options in place of ContactsContract.Contacts.CONTENT_URI in below method. But I am getting lot many of the contacts (many are junk with only email ids) displayed.
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,那么无论我选择哪个联系人,都不会找到与电话号码或电子邮件相关的任何列.
If I pass ContactsContract.Contacts.CONTENT_URI as parameter for above method and in case of below handler method, the String[] for the query method as projection parameters (which are shown commented), the method fails with java.lang.IllegalArgumentException. If I pass null in below method, then whatever contact I select, I don't find any column related to phone number or email.
@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();
}
}
}
}
有什么方法可以只显示通常在用户访问电话簿且具有可用电话号码时对用户可见的联系人?
Is there any way to display only contacts visible to user usually when user goes to phone book and which has phone numbers available?
我尝试遍历stackoverflow和其他站点中的所有线程,但是尽管有很多人发布了此问题,但是找不到解决此问题的任何解决方案.我在Android平台上工作不多,可能错过了一些次要细节,我相信必须有一种简单的方法来实现这一目标.
I tried going through all the threads in stackoverflow and other sites, but could not find any solution which resolve issue around this though many people have posted the issue. I haven't worked much with the Android platform and I might have missed out certain minor details and I believe there must be an easy way to achieve this.
建议.感谢您的帮助.
谢谢.
推荐答案
请使用以下代码
Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
intent.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_TYPE);
startActivityForResult(intent, 1);
这篇关于在Android设备中使用ACTION_PICK意向仅显示带有电话号码的联系人的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!