我的应用从用户获取联系人姓名,然后如果联系人存在则应该在联系人列表中搜索。如果是,则拉出其号码,然后自动拨打此号码。
我似乎无法理解如何查找联系人姓名,然后提取其号码(如果存在)。
public boolean seekAction(String s)
{
Intent callIntent= new Intent(Intent.ACTION_PICK, Uri.parse("content://contacts"));
callIntent.setType(Phone.CONTENT_TYPE);
if (callIntent.resolveActivity(getPackageManager()) != null)
{
startActivityForResult(callIntent, CONTACT_CALL_CODE_REQUEST);
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
if(resultCode == RESULT_OK)
{
if(requestCode == CONTACT_CALL_CODE_REQUEST)
{
// Get the URI and query the content provider for the phone number
Uri contactUri = data.getData();
String[] s = new String[]{Phone.NUMBER};
Cursor cursor = getContentResolver().query(contactUri, s, null, null, null);
// If the cursor returned is valid, get the phone number
if (cursor != null && cursor.moveToFirst())
{
int numberIndex = cursor.getColumnIndex(Phone.NUMBER);
String number = cursor.getString(numberIndex);
// Do something with the phone number
Log.i("actionCallResult()", "phone number= "+number);
Intent intent = new Intent(Intent.ACTION_DIAL);//calling intent
intent.setData(Uri.parse("tel:" + number));
if (intent.resolveActivity(getPackageManager()) != null)
{
startActivity(intent);
}
}
}
}
super.onActivityResult(requestCode, resultCode, data);
}
我有这段代码,但它无法帮助我按名称查找联系人。
编辑:
public boolean actionCallResult(String str)
{
Log.i("actionCallResult()", "inside actionCallResult");
Cursor contacts = getContentResolver().query(Phone.CONTENT_URI, null, null, null, null);
boolean contactFound = false; // If the cursor returned is valid, get the phone number
if (contacts != null && contacts.moveToFirst())
{
do{
String contactName = contacts.getString(contacts.getColumnIndex(Phone.DISPLAY_NAME));
if (contactName.contains(str)) //contactName.equals(str)
{
contactFound = true;
break;
}
else
{
if(str.charAt(0)== 'ל')
{
if (contactName.contains(str.substring(1)))
{
contactFound = true;
break;
}
}
}
}while (contacts.moveToNext());
}
if(contactFound)
{
String number= contacts.getString(contacts.getColumnIndex(Phone.NUMBER));
Log.i("actionCallResult()", "phone number= "+number);
Intent intent = new Intent(Intent.ACTION_DIAL);//calling intent
intent.setData(Uri.parse("tel:" + number));
if (intent.resolveActivity(getPackageManager()) != null)
{
startActivity(intent);
}
}
contacts.close();
return contactFound;
}
答案 0 :(得分:5)
我编写了一个简单的代码,用于查找名为“ Mike Peterson ”的联系人,如果找到联系人号码,则会发送消息,否则会显示“未找到”消息。
此代码是原始的,绝不应该为任何专业实施提供服务。更好的方法是使用自定义 AsyncTasks
和Array Storage
。
请注意,以下许可声明必须出现在您的清单中:
<uses-permission android:name="android.permission.READ_CONTACTS" />
以下是在一项简单的主要活动中测试的:
String str = "Mike Peterson"; // As an example
Cursor contacts = getApplicationContext().getContentResolver()
.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null);
boolean contactFound = false;
while (contacts.moveToNext()) {
String contactName =
contacts.getString(contacts.getColumnIndex
(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
if (contactName.equals(str)) {
contactFound = true;
break;
}
}
Toast.makeText(getApplicationContext(), contactFound ?
contacts.getString(contacts.getColumnIndex
(ContactsContract.CommonDataKinds.Phone.NUMBER)) : "Contact not found!"
, Toast.LENGTH_LONG).show();
contacts.close();