1

我正在尝试找出是否来自 Android 中的收藏夹联系人的来电。到目前为止,我的代码是:

    public class PhoneCallReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(final Context context, Intent intent) {
            TelephonyManager telephony = (TelephonyManager) context
                    .getSystemService(Context.TELEPHONY_SERVICE);
            telephony.listen(new PhoneStateListener() {
                @Override
                public void onCallStateChanged(int state, String incomingNumber) {
                    super.onCallStateChanged(state, incomingNumber);
                    if (state == TelephonyManager.CALL_STATE_RINGING) {
                        if (ContactHelper.fromFavourites(context, incomingNumber)) {
                             //do stuff
                        }
                    }
                };

我的 ContactHelper 是这样的:

public static boolean fromFavourites(Context context, String phoneNumber) {
        final String[] projection = new String[] {ContactsContract.PhoneLookup._ID};
        Uri lookupUri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(phoneNumber)); //use this to look up a phone number
        Cursor cursor = context.getContentResolver().query(lookupUri, projection, "starred=?", new String[] { "1" }, null);
        if (cursor != null && cursor.getCount() != 0) {
            System.out.println("OUTPUT: "+cursor0.getCount());
            return true;
        } else return false;
    }

我已经尝试过这个解决方案,但它只给了我所有最喜欢的联系人。我正在尝试使用 PhoneLookup 因为来自Android doc,它说

联系人表中的列也可通过连接获得。

所以我想我可以查询 PhoneLookUp 和 Contacts 表之间的连接,但内容提供者似乎无法进行连接。我打算为此编写一个原始 SQLite 脚本,但我不知道如何加入 PhoneLookUp 和联系人表,找不到他们的外键 :( 感谢所有帮助

4

2 回答 2

0

您的第一个链接可以获取所有喜欢的联系人。现在要确定来电号码是否来自收藏夹,您必须检测来电。所以使用 aPhoneStateListener来检测 inocming 调用。当有来电检测时,只需检查收藏夹

检测来电

public class CustomPhoneStateListener extends PhoneStateListener {

public void onCallStateChange(int state, String number){
    switch(state){
        case TelephonyManager.CALL_STATE_RINGING:
            //call from number. check whether it is favorite or not
            break;
    }   
}

也使用以下权限

< uses-permission android:name="android.permission.READ_PHONE_STATE" />
于 2013-04-26T15:56:25.707 回答
0
public static boolean fromFavourites(Context context, String phoneNumber) {
    final String[] projection = new String[] {ContactsContract.PhoneLookup.STARRED};
    Uri lookupUri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(phoneNumber)); //use this to look up a phone number
    Cursor cursor = context.getContentResolver().query(lookupUri, projection,  
                   ContactsContract.PhoneLookup.NUMBER + "=?", 
                    new String[] { phoneNumber}, null);
    if (cursor.moveToFirst()) {
        while (!cursor.isAfterLast()) {
            if (cursor.getInt(cursor.getColumnIndex(ContactsContract.PhoneLookup.STARRED)) == 1) {
            System.out.println("OUTPUT: " + cursor.getInt(0) );
            return true;
            }
            cursor.moveToNext();
        }   
    } 
    return false;
}
于 2013-04-26T16:43:33.713 回答