Search code examples
androidphone-call

How to get recently dialled numbers


I am working on an application, where i need to fetch 5 recently dialled numbers. I am able to sort the numbers in descending order, so as the recently dialled numbers comes first. I am using the following code to fetch the recent numbers.

dialledCall = (TextView)findViewById(R.id.dialledCall);

              String strOrder = CallLog.Calls.DATE + " DESC";
              Cursor mCallCursor = getApplicationContext().getContentResolver().query(CallLog.Calls.CONTENT_URI,
                      null,
                      CallLog.Calls.TYPE 
                      ,null,
                      strOrder);
              mCallCursor.moveToFirst();
              do {
                  if( i >5 )
                      break;
                  mobileNumber =   mCallCursor.getString(mCallCursor.getColumnIndex(android.provider.CallLog.Calls.NUMBER));
              number += mobileNumber + "\n";

              i++;
              }
              while (mCallCursor.moveToNext());
         dialledCall.setText(number);

With this, i am able to sort the numbers in recent order and also i have just fetched last 5 numbers. But the query returns all the numbers, i mean Dialled, Missed and Received as well. But here i want just the dialled numbers.

I know there is something to do with CallLog.Calls.TYPE in the above query.

I have tried passing null value, but it returns all the numbers.

I also have tried CallLog.Calls.OUTGOING_TYPE in place of it, but it gives error that int value is not permitted, it should be a string value.

Any suggestions on how to replace the above code, so as to only get the dialled calls.

Note- I have already looked at https://stackoverflow.com/a/10480569/1626878, but it does not provide a solution to only fetch the dialled numbers.


Solution

  • I have solved the issue, so I am posting it for future visitors.

        String mobileNumber="";
        int i = 1;
        String number = "";
        TextView dialledCall;
            @Override
            public void onCreate(Bundle savedInstanceState)
            {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.main);
                dialledCall = (TextView)findViewById(R.id.dialledCall);
    
        //              String[] strFields = { android.provider.CallLog.Calls.NUMBER, android.provider.CallLog.Calls.TYPE, android.provider.CallLog.Calls.CACHED_NAME,         android.provider.CallLog.Calls.CACHED_NUMBER_TYPE, android.provider.CallLog.Calls.DURATION     };
                      String strOrder = CallLog.Calls.DATE + " DESC";
                      Cursor mCallCursor = getApplicationContext().getContentResolver().query(CallLog.Calls.CONTENT_URI,
                              null,
                              CallLog.Calls.TYPE + " = " + CallLog.Calls.OUTGOING_TYPE
                              ,null,
                              strOrder);
                      mCallCursor.moveToFirst();
                      do {
                          if( i >5 )
                              break;
                          mobileNumber =   mCallCursor.getString(mCallCursor.getColumnIndex(android.provider.CallLog.Calls.NUMBER));
                      number += mobileNumber + "\n";
    
                      i++;
                      }
                      while (mCallCursor.moveToNext());
                 dialledCall.setText(number);
            }
    

    Here CallLog.Calls.TYPE + " = " + CallLog.Calls.OUTGOING_TYPE in the query separates out the dialled calls.