Search code examples
androiddatetimecalllog

Getting Android CallLogs Date Issue


I am trying to list incoming calls with names and dates/times;

        String[] _Projection = new String[]{CallLog.Calls.NUMBER,
                CallLog.Calls.TYPE,
                CallLog.Calls.CACHED_NAME,
                CallLog.Calls.DATE};
        String[] _ARGS = {String.valueOf(CallLog.Calls.INCOMING_TYPE)};
        Cursor _cursor = managedQuery(CallLog.Calls.CONTENT_URI, _Projection, CallLog.Calls.TYPE + "=?", _ARGS, null);
        int _num = _cursor.getColumnIndex(CallLog.Calls.NUMBER);
        int _tip = _cursor.getColumnIndex(CallLog.Calls.TYPE);
        int _name = _cursor.getColumnIndex(CallLog.Calls.CACHED_NAME);
        int _date = _cursor.getColumnIndex(CallLog.Calls.DATE);

When I add these two lines the application stops;

        String strDate = _cursor.getString(_date);
        long lng = Long.parseLong(strDate);

Or with this line;

        long __lngDate = _cursor.getLong(_date);

Without these lines , application works without dates/times... What am i doing wrong?

NOTE: There is also permission with READ_CALL_LOG

<uses-permission android:name="android.permission.READ_CALL_LOG"/>

Thanks and Best Regards


Solution

  • I found the answer myself

    public Loader<Cursor> onCreateLoader(int loaderID, Bundle args) {
        Log.d("CallLogs", "onCreateLoader() >> loaderID : " + loaderID);
    
        switch (loaderID) {
            case URL_LOADER:
    
                String mSelectionClause = android.provider.CallLog.Calls.DATE+ " >= ?";
                    String[]mSelectionArgs = {createDate()};
                // Returns a new CursorLoader
                return new CursorLoader(
                        mContext,   // Parent activity context
                        CallLog.Calls.CONTENT_URI,        // Table to query
                        null,     // Projection to return
                        mSelectionClause,            // No selection clause
                        mSelectionArgs,            // No selection arguments
                        CallLog.Calls.DATE + " desc"             // Default sort order
                );
            default:
                return null;
        }
    
    }
    
    
     public  String createDate()
    {
        Calendar calendar = Calendar.getInstance();
          calendar.add(Calendar.DATE, -1);
    
        return String.valueOf(calendar.getTimeInMillis());
    }