android如何将联系人电话添加到应用中

android如何将联系人电话添加到应用中,第1张

private List<HashMap<String, String>>getData() {

ContentResolver cr = this.getContentResolver()

//取得电话本中开始一项的光标

Cursor cursor = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null)

List<HashMap<String, String>>data = new ArrayList<HashMap<String, String>>()

while (cursor.moveToNext())

{

// 取得联系人名字

int nameFieldColumnIndex = cursor.getColumnIndex(PhoneLookup.DISPLAY_NAME)

String name = cursor.getString(nameFieldColumnIndex)

// 取得联系人ID

String contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID))

Cursor phone = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = "

+ contactId, null, null)

// 取得电话号码(可能存在多个号码)

while (phone.moveToNext())

{

HashMap<String, String>map = new HashMap<String, String>()

String phoneNumber = phone.getString(phone.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER))

map.put("name", name)

map.put("phoneNum", phoneNumber)

data.add(map)

allPhone.add(phoneNumber)

}

phone.close()

}

cursor.close()

return data

}

代码中读取sim卡的代码如下:

1.在AndroidManifest.xml添加权限:

<!-- 添加访问手机位置的权限 --><uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/><!-- 添加访问手机状态的权限 --><uses-permission android:name="android.permission.READ_PHONE_STATE"/>

2.通过context对象获取TelephonyManager tManager类的实例

3.获取设备当前位置

String location = tManager.getCellLocation()==null? "未知地区":tManager.getCellLocation().toString()

4.获取手机制式

String simOperatorName = tManager.getSimOperatorName().equals("")?"未知":tManager.getSimOperatorName().toString()

5.获取SIM卡运营商名称

String networkOperatorName = tManager.getNetworkOperatorName()==null? "未知":tManager.getNetworkOperatorName().toString()

6.获取SIM卡号

String line1Number = tManager.getLine1Number()==null? "未知":tManager.getLine1Number().toString()


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/bake/11824951.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-19
下一篇 2023-05-19

发表评论

登录后才能评论

评论列表(0条)

保存