Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9018103
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T04:26:15+00:00 2026-06-16T04:26:15+00:00

I’m working in Android application where I have to fetch last 20 dialled calls.

  • 0

I’m working in Android application where I have to fetch last 20 dialled calls.

Here’s is my activity:

public class Calls extends Activity {
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.calls);
    TextView dialledCall = (TextView)findViewById(R.id.dialledCall);
        Cursor managedCursor;
    try {

StringBuffer sb = new StringBuffer();
            String strOrder = CallLog.Calls.DATE + " DESC";
            managedCursor = managedQuery( CallLog.Calls.CONTENT_URI,null, CallLog.Calls.TYPE + " = " + CallLog.Calls.OUTGOING_TYPE,null, strOrder + " LIMIT 0, 6");
            managedCursor.moveToFirst();
            int number = managedCursor.getColumnIndex( CallLog.Calls.NUMBER );
            int type = managedCursor.getColumnIndex( CallLog.Calls.TYPE );
            int date = managedCursor.getColumnIndex( CallLog.Calls.DATE);
            int duration = managedCursor.getColumnIndex( CallLog.Calls.DURATION);
            sb.append( "Dialled Calls :");

            while ( managedCursor.moveToNext() ) {
            String phNumber = managedCursor.getString( number );
            String callType = managedCursor.getString( type );
            String callDate = managedCursor.getString( date );
            Date callDayTime = new Date(Long.valueOf(callDate));
            String callDuration = managedCursor.getString( duration );
            String dir = null;
            int dircode = Integer.parseInt( callType );

            switch( dircode ) {
            case CallLog.Calls.OUTGOING_TYPE:
            dir = "Dialled";
            break;
            }

            sb.append( "\nPhone Number : "+phNumber +" \nCall Type : "+dir+" \nCall Date : "+callDayTime+" \nCall duration in sec : "+callDuration );
            sb.append("\n----------------------------------");          
            }
            sb.append("\n==================================");
            sb.append("\n\n");
            managedCursor.close();
            Toast.makeText(this, "Eureka :)", Toast.LENGTH_SHORT).show();

            managedCursor = managedQuery( CallLog.Calls.CONTENT_URI,null, CallLog.Calls.TYPE + " = " + CallLog.Calls.INCOMING_TYPE,null, strOrder + " LIMIT 0, 6");
            managedCursor.moveToFirst();
            int number1 = managedCursor.getColumnIndex( CallLog.Calls.NUMBER );
            int type1 = managedCursor.getColumnIndex( CallLog.Calls.TYPE );
            int date1 = managedCursor.getColumnIndex( CallLog.Calls.DATE);
            int duration1 = managedCursor.getColumnIndex( CallLog.Calls.DURATION);
            sb.append( "Received Calls :");
            while ( managedCursor.moveToNext() ) {

            String phNumber1 = managedCursor.getString( number1 );
            String callType1 = managedCursor.getString( type1 );
            String callDate1 = managedCursor.getString( date1 );
            Date callDayTime1 = new Date(Long.valueOf(callDate1));
            String callDuration1 = managedCursor.getString( duration1 );
            String dir1 = null;
            int dircode = Integer.parseInt( callType1 );

            switch( dircode ) {
            case CallLog.Calls.INCOMING_TYPE:
            dir1 = "Received";
            break;
            }
            sb.append( "\nPhone Number : "+phNumber1 +" \nCall Type : "+dir1+" \nCall Date : "+callDayTime1+" \nCall duration in sec : "+callDuration1 );
            sb.append("\n----------------------------------");          
            }
            sb.append("\n==================================");
            sb.append("\n\n");
            managedCursor.close();
            Toast.makeText(this, "Eureka :) 1", Toast.LENGTH_SHORT).show();
        managedCursor.close();
}


 catch (NullPointerException e) {
                // TODO: handle exception
                Toast.makeText(this, "Sorry Man :(", Toast.LENGTH_SHORT).show();
                e.printStackTrace();
            }
        }
        }

And here is the corresponding XML file:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:fillViewport="true" >

<Linearlayout android:layout_height="wrap_content"
 android:layout_width="fill_parent"
android:orientation="vertical">

        <TextView
            android:id="@+id/dialledCall"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:textSize="13dp" />

</Linearlayout>
</ScrollView>

Output: I am able to get first 5 dialled numbers, but in the received calls I’m getting even messages.

What am I doing wrong?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T04:26:16+00:00Added an answer on June 16, 2026 at 4:26 am

    Most probably you are missing a permission:

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

    Or your loop is not correct, try this kind of loop instead:

    int count = 0;
    if (cursor.moveToFirst()) {
      do {
        // Read your row here
    
        ++count;
      } while (cursor.moveToNext() && count<20);
    }
    cursor.close();
    

    It would be more efficient also to limit the number of rows directly in the query rather than in the loop itself:

    managedQuery( CallLog.Calls.CONTENT_URI, null, 
            CallLog.Calls.TYPE + " = " + CallLog.Calls.OUTGOING_TYPE, 
            null, strOrder + " LIMIT 0, 20");
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I have a jquery bug and I've been looking for hours now, I can't
this is what i have right now Drawing an RSS feed into the php,
I have a small JavaScript validation script that validates inputs based on Regex. I
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have a French site that I want to parse, but am running into

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.