Android - 在电话上接收联系人更改
添加、删除或修改电话联系人时是否可以通知广播接收器或服务?
我正在制作一个需要快速访问电话联系人的应用程序,因为我正在考虑通过contactsContracts 访问一份sqlite 电话联系人副本。
如果不可能,有谁知道如何提高以下代码的响应速度,以查看电话号码是否在手机的联系人列表中?
public boolean isNumberInContacts(String Num){
try {
Cursor cursor = context.getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
while (cursor.moveToNext()) {
String colID = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
String hasPhone = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));
if (Integer.parseInt(hasPhone)==1) {
Cursor phone = context.getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null,ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ colID,null, null);
for (int i=0;phone.moveToNext();i++){
if (Num.equals(mNumber.getNumber((phone.getString(phone.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)))))){
return true;
}
}
}
}
cursor.close();
} catch (Exception e) {
e.printStackTrace();
Log.d(TAG, "Error when validate number in contacts: "+ e.toString());
}
return false;
}
谢谢
Is it possible to have a broadcast receiver or service to be notified when a phone contact is added, deleted or modified?
I am making an application that needs fast access to phone contact, for what i was thinking of one copy sqlite phone contacts as accessed through contactsContracts.
If it's not possible, Does anyone know how to improve the response speed of the following code to see if a number is in the phone's contact list?
public boolean isNumberInContacts(String Num){
try {
Cursor cursor = context.getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
while (cursor.moveToNext()) {
String colID = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
String hasPhone = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));
if (Integer.parseInt(hasPhone)==1) {
Cursor phone = context.getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null,ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ colID,null, null);
for (int i=0;phone.moveToNext();i++){
if (Num.equals(mNumber.getNumber((phone.getString(phone.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)))))){
return true;
}
}
}
}
cursor.close();
} catch (Exception e) {
e.printStackTrace();
Log.d(TAG, "Error when validate number in contacts: "+ e.toString());
}
return false;
}
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
添加联系人时不广播。在联系人数据库中注册一个内容观察器以检查其更改。
no broadcast when a contact is added. register a content observer in the contacts database to check for changes to it.