Android Phone call history/log programmatically


activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    <TextView
        android:id="@+id/call"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="@string/hello_world" />

</RelativeLayout>

MainActivity.java

public class MainActivity extends Activity {

    private static final int MISSED_CALL_TYPE = 0;
    private TextView txtcall;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        txtcall = (TextView) findViewById(R.id.call);

        StringBuffer sb = new StringBuffer();
        Cursor managedCursor = managedQuery(CallLog.Calls.CONTENT_URI, null,
                null, null, null);
        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("Call Details :");
        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 = "OUTGOING";
                break;

            case CallLog.Calls.INCOMING_TYPE:
                dir = "INCOMING";
                break;

            case CallLog.Calls.MISSED_TYPE:
                dir = "MISSED";
                break;
            }
            sb.append("\nPhone Number:--- " + phNumber + " \nCall Type:--- "
                    + dir + " \nCall Date:--- " + callDayTime
                    + " \nCall duration in sec :--- " + callDuration);
            sb.append("\n----------------------------------");
        }
        managedCursor.close();
        txtcall.setText(sb);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }

}

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



Android Phone call history/log programmatically Android Phone call history/log programmatically Reviewed by Nirav on 11:12 PM Rating: 5

4 comments:

  1. the code doesn't work !!! the application has crashed .any help please??

    ReplyDelete
  2. hi i am ganesh u r blog is awesome .........am a fresher in android development,i have learnt so many things from u r blog...
    i am developing smschat App,,it contains list of messages,i want to set the date & time to each message,the date & time shoud update every time....in textview but am unable get set the date & time
    so plz could u help me in this app....
    my mail id is:ganeshmudhiraj12@gmail.com
    my code is as follows......


    GregorianCalendar calCreationDate = new GregorianCalendar();
    int intMilli = calCreationDate.get(Calendar.MILLISECOND);
    int intSeconds = calCreationDate.get(Calendar.SECOND);
    calCreationDate.add(Calendar.MILLISECOND, (-1*intMilli));
    calCreationDate.add(Calendar.SECOND, -1*intSeconds);
    calCreationDate.add(Calendar.MINUTE, 1);
    Date dateStartDate = calCreationDate.getTime();

    String time =dateStartDate.toString();




    Thanks.....

    ReplyDelete
  3. In the manifest file change to:
    android:targetSdkVersion="10"
    then the code will run
    i dont know exactly why, i think its compatibility, someone can explain it?? because if targetSdkVersion="18" for example, the app crashes

    ReplyDelete

Random Posts

My Android Tuts. Powered by Blogger.