Search code examples
androidcallandroid-contextandroid-cursoradapterandroid-cursor

Call Logs not getting CACHED_NAME


I am trying to get list of all number from CALL-LOGS which are NOT in my contacts,

I am facing an issue when anyone who is in my contacts calls me.

Cursor "c" is returning that number because "name"(CACHED_NAME) is null".

But when I open call-log application and then again I open my application, that number is not returned as now ""name"(CACHED_NAME)" has value.

Could I refresh data in call-logs from my application?

I can build one function which can check if number exists or not in phone contact.

But how can I use this function with cursor-adapter. I tried using this function in bindview, but still blank element is created for that number. I would like to use 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;
}

Thanks

P.S. There can be some technical error or missing code in above sample code as I have just extracted required code from my application.


Solution

  • You can't directly access the updated call log `CACHED_NAME' with your application.

    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;
    }
    

    Now , You can check into bindView function.

            @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());
                }  
            }
    

    May this helps lot.

    Happy coding...:-)