Sahil Mahajan Mj
Sahil Mahajan Mj

Reputation: 11141

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.

Upvotes: 3

Views: 3207

Answers (2)

Sahil Mahajan Mj
Sahil Mahajan Mj

Reputation: 11141

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.

Upvotes: 1

Artyom Kiriliyk
Artyom Kiriliyk

Reputation: 2513

You can use the CallLog class to get at this information (see http://developer.android.com/reference/android/provider/CallLog.Calls.html).

You're interested in the getLastOutgoingCall method, which returns the last phone number called.

Check the call logs. You can easily access this. Do something like,

Define, public Cursor mCallCursor;

Then define the fields you want to obtain from the call log,

public static final String[] STR_FIELDS = {
        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.DATE,
        android.provider.CallLog.Calls.DURATION, android.provider.CallLog.Calls.CACHED_NUMBER_LABEL,android.provider.CallLog.Calls.NUMBER
        };

Set the order, public static final String STR_ORDER = android.provider.CallLog.Calls.DATE + " DESC";

Call the cursor.

mCallCursor = getContentResolver().query(
            android.provider.CallLog.Calls.CONTENT_URI,
            STR_FIELDS,
            null,
            null,
            STR_ORDER);     

Upvotes: 1

Related Questions