在手机联系人中:由姓名查找到电话号码(根据模糊“汉字”查找/姓名查找/电话查找到光标cursor,然后通过光标获取该组的信息:姓名,联系电话,头像等)
由光标获取信息时:
1、光标首先指到某个位置
cursor.move(offset);
cursor.moveToPosition(position);
cursor.moveToFirst();
2、获取多少个元组,>0才进行之后的步骤。
cursor.getCount()
3、获取姓名…..
cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
4、别忘加权限哈:
<!--打电话和发信息的-->
<uses-permission android:name="android.permission.CALL_PHONE"/>
<uses-permission android:name="android.permission.SEND_SMS"/>
String[] projection =
{ ContactsContract.PhoneLookup.DISPLAY_NAME,
ContactsContract.CommonDataKinds.Phone.NUMBER };
Cursor cursor = this
.getContentResolver()
.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
projection, // Which columns to return.
// 这是模糊查询
ContactsContract.PhoneLookup.DISPLAY_NAME
+ " LIKE"
+ " '%"
+ call_name
+ "%'",
// WHERE clause.
// (如果你根据具体的条件来查询也可以这样写:)
// ContactsContract.CommonDataKinds.Phone.NUMBER
// +
// " = '"
// + number + "'", // WHERE clause.
// // 这是根据电话号码来查询
// ContactsContract.Contacts.DISPLAY_NAME
// + " = '"
// + call_name + "'", // WHERE
// clause. //
// 这是根据姓名来查询
null, // WHERE clause value
// substitution
null); // Sort order.
if (cursor == null)
{
// 找不到该人
Log.i("TAG", "getPeople null");
} else
{
Log.i("TAG", "getPeople cursor.getCount() = "
+ cursor.getCount());
if (cursor.getCount() > 0)
{
// 找到该人
cursor.moveToFirst();
callName = cursor
.getString(cursor
.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
callNumber = cursor
.getString(cursor
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
}
打电话:
//Intent.ACTION_CALL:直接拨打:拨打按钮已经按下
//Intent.ACTION_VIEW:联系人列表
// 打电话(启动的页面是未拨打:拨打按钮还未按下)
startActivity(new Intent(Intent.ACTION_DIAL,Uri.parse("tel:" + callNumber)));
发短信:
SmsManager smsManager = SmsManager.getDefault();
// 发送短信
//mNumber号码,message短信内容
if (message.length() > 70)
{
List<String> contents = smsManager.divideMessage(message);
for (String sms : contents)
{
smsManager.sendTextMessage(mNumber, null, sms, null,
null);
}
} else
{
smsManager.sendTextMessage(mNumber, null, message, null,
null);
}