getting the call logs of incoming and outgoing calls in android programmatically

前端 未结 5 1847
执笔经年
执笔经年 2021-02-02 04:34

I am making an app in which I want to get the call logs of all incoming, outgoing and missed calls. How can I do that?

5条回答
  •  遥遥无期
    2021-02-02 05:18

    All the answers here are using managedQuery which is now deprecated. It should be replaced with getContext().getContentResolver().query() method instead, as mentioned here and demonstrated here.

    Here is a short sample code, based on those examples:

    String[] projection = new String[] {
                    CallLog.Calls.CACHED_NAME,
                    CallLog.Calls.NUMBER,
                    CallLog.Calls.TYPE,
                    CallLog.Calls.DATE
            };
    // String sortOrder = ContactsContract.Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC";
    
     Cursor cursor =  mContext.getContentResolver().query(CallLog.Calls.CONTENT_URI, projection, null, null, null);
     while (cursor.moveToNext()) {
        String name = cursor.getString(0);
        String number = cursor.getString(1);
        String type = cursor.getString(2); // https://developer.android.com/reference/android/provider/CallLog.Calls.html#TYPE
        String time = cursor.getString(3); // epoch time - https://developer.android.com/reference/java/text/DateFormat.html#parse(java.lang.String
        }
    cursor.close();
    

提交回复
热议问题