android - 未获取 CACHED_NAME 的通话记录

标签 android call android-context android-cursoradapter android-cursor

我正在尝试从 CALL-LOGS 中获取所有号码的列表,这些号码NOT 在我的联系人中,

我的联系人 中的任何人给我打电话时,我遇到了一个问题

游标“c”正在返回该数字,因为“name”(CACHED_NAME) 为 null”。

但是当我打开 call-log 应用程序然后再次打开我的 application 时,该号码不会返回为现在 ""name"(CACHED_NAME)”具有值(value)。

我可以从我的应用程序中刷新通话记录中的数据吗?

我可以构建一个函数,它可以检查电话联系人中的号码是否存在。

但是我如何将这个函数与游标适配器一起使用。我尝试在 bindview 中使用此函数,但仍然为该数字创建了空白元素。我想使用 CusrorAdapter

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    pview = inflater.inflate(R.layout.fragment_call, container, false);

    ListView lvCall = (ListView) pview.findViewById(R.id.lvCall);
    Uri uri = Uri.parse("content://call_log/calls");
    ContentResolver cr = getActivity().getContentResolver();
    **Cursor c = cr.query(uri, null, "name is null", null, "date DESC");**
    adapter = new CursorAdapter(getActivity().getBaseContext(), c) {

        @Override
        public View newView(Context context, Cursor cursor, ViewGroup parent) {
            LayoutInflater li = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            return li.inflate(R.layout.call_list, parent, false);
        }

        @Override
        public void bindView(View view, Context context, Cursor cursor) {

            **if (contactExists(cursor.getString(cursor.getColumnIndex("NUMBER")))) {
                return;
            }**
            txt_call_number = (TextView) view.findViewById(R.id.txt_call_number);
            txt_call_id = (TextView) view.findViewById(R.id.txt_call_id);

            txt_call_number.setText(cursor.getString(cursor.getColumnIndex("NUMBER")));
            txt_call_id.setText(cursor.getString(cursor.getColumnIndex("_ID")).trim());

        }

    };

    lvCall.setAdapter(adapter);

    return pview;
}

谢谢

附言上面的示例代码中可能存在一些技术性错误缺失代码,因为我刚刚从我的应用程序中提取了所需的代码 >.

最佳答案

您无法使用您的应用程序直接访问更新后的通话记录“CACHED_NAME”。

public boolean contactExists(Context context, String number) {
           /// number is the phone number
          Uri lookupUri = Uri.withAppendedPath(
         PhoneLookup.CONTENT_FILTER_URI, 
         Uri.encode(number));
         String[] mPhoneNumberProjection = { PhoneLookup._ID, PhoneLookup.NUMBER, PhoneLookup.DISPLAY_NAME };
        Cursor cur = context.getContentResolver().query(lookupUri,mPhoneNumberProjection, null, null, null);
        try {
             if (cur.moveToFirst()) {
                return true;
             }
        } finally {
               if (cur != null)
               cur.close();
        }
  return false;
}

现在,您可以查看 bindView 函数。

        @Override
        public void bindView(View view, Context context, Cursor cursor) {

            txt_call_number = (TextView) view.findViewById(R.id.txt_call_number);
            txt_call_id = (TextView) view.findViewById(R.id.txt_call_id);
            if (contactExists(cursor.getString(cursor.getColumnIndex("NUMBER")))) {
                txt_call_number.setVisibility(View.GONE); 
                txt_call_id.setVisibility(View.GONE); 
                view.setVisibility(View.GONE);
                return;
            }else{
                txt_call_number.setText(cursor.getString(cursor.getColumnIndex("NUMBER")));
                txt_call_id.setText(cursor.getString(cursor.getColumnIndex("_ID")).trim());
            }  
        }

希望这对您有帮助。

快乐编码...:-)

关于android - 未获取 CACHED_NAME 的通话记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36908587/

相关文章:

android - 将上下文作为 DialogFragment 的参数传递

Android:自定义相机拍摄的图像太暗

android - 在 Android 设备中阅读 SELinux

java - Volley JSONException : End of input at character 0 of

java - 使用 Codename One NativeInterface 调用 native Android 库时仍然出现 Nullpointer

java - Android Holo Light 样式会根据所选上下文发生变化

javascript - 在 setTimeout 和函数内部调用函数

java - Android 来电时如何显示叠加

function - 如何实现函数调用的 MVVM?

java - Android:我对如何获得正确的上下文来查找我的 View 感到困惑