Android - 在电话上接收联系人更改

发布于 2024-12-13 22:32:36 字数 1436 浏览 0 评论 0原文

添加、删除或修改电话联系人时是否可以通知广播接收器或服务?

我正在制作一个需要快速访问电话联系人的应用程序,因为我正在考虑通过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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

尝蛊 2024-12-20 22:32:36

添加联系人时不广播。在联系人数据库中注册一个内容观察器以检查其更改。

no broadcast when a contact is added. register a content observer in the contacts database to check for changes to it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文