how to get the call log details in android

How to get the call log details in android

Hi friends now we are going to learn how get call log details in android device. Call log is the call record details of the device. In this tutorial we learn how retrieve call record and populate in to the ListView. For doing that we take an Activity class (CallLogActivity.java) , one adapter class (LogAdapter.java) for showing the list and one data collection class(LogCollection.java). Before use below code we need to take ‘READ_CALL_LOG’ permission from manifest file.

To know how take permission read :-

How take permission in android manifest.

 

Permission:-

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

 

Shortcode for call log:

private void getMyCallLog() {

              Cursor cursor = getContentResolver().query(CallLog.Calls.CONTENT_URI,       null, null, null, null);

              int name_id = cursor.getColumnIndex( CallLog.Calls.CACHED_NAME);

              int number_id = cursor.getColumnIndex( CallLog.Calls.NUMBER );

              int duration_id = cursor.getColumnIndex( CallLog.Calls.DURATION);

              int type_id = cursor.getColumnIndex( CallLog.Calls.TYPE );

              int date_id = cursor.getColumnIndex( CallLog.Calls.DATE);

               while (cursor.moveToNext()) {

                      String name = cursor.getString( name_id );

                     String phone_no = cursor.getString( number_id );

                     String call_type = cursor.getString( type_id );

                     String call_duration = cursor.getString( duration_id );             

                     String call_date_st = cursor.getString( date_id );

               int id=Integer.parseInt(call_type);

       String type="";

              switch(id) {

              case CallLog.Calls.OUTGOING_TYPE:

                     type = "Outgoing";

              break;

 

              case CallLog.Calls.INCOMING_TYPE:

                     type = "Incomming";

              break;

 

              case CallLog.Calls.MISSED_TYPE:

                     type = "Missed";

              break;

              }

                      }

              cursor.close();

              }

Full Source Code:-

CallLogActivity.java


package com.androidfromhome.calllog;

import java.util.ArrayList;
import android.app.Activity;
import android.app.ProgressDialog;
import android.database.Cursor;
import android.os.AsyncTask;
import android.os.Bundle;
import android.provider.CallLog;
import android.widget.ListView;

public class CallLogActivity extends Activity {

    private ListView lv_log;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_call_log);
        lv_log=(ListView)findViewById(R.id.lv_calllog);
        
        new LogTask().execute("");
        }

    
public class LogTask extends AsyncTask<String, Void, Void> {
        
        private ProgressDialog progDailog;
        
        protected void onPreExecute() {
            
            LogCollection.logCollectionArr=new ArrayList<LogCollection>();
            
            progDailog=new ProgressDialog(CallLogActivity.this);        
            progDailog.setCancelable(false);
            progDailog.setMessage("Please Wait...");
            progDailog.show();    

        }

        @Override
        protected Void doInBackground(String... arg0) {
            
            getMyCallLog();
            
            return null;
            
            
            }
        

    @Override
    protected void onPostExecute(Void result) {
        // TODO Auto-generated method stub
        super.onPostExecute(result);
            
        LogAdapter ca=new LogAdapter(CallLogActivity.this,
                R.layout.list_item, LogCollection.logCollectionArr);
        
        lv_log.setAdapter(ca);
    
        progDailog.dismiss();
                
        
    }}
    
    
    private void getMyCallLog() {

        
        Cursor cursor = getContentResolver().query(CallLog.Calls.CONTENT_URI, null, null, null, null);
        int name_id = cursor.getColumnIndex( CallLog.Calls.CACHED_NAME);
        int number_id = cursor.getColumnIndex( CallLog.Calls.NUMBER );
        int duration_id = cursor.getColumnIndex( CallLog.Calls.DURATION);
        int type_id = cursor.getColumnIndex( CallLog.Calls.TYPE );
        int date_id = cursor.getColumnIndex( CallLog.Calls.DATE);

               
        while (cursor.moveToNext()) {
            
            String name = cursor.getString( name_id );
            String phone_no = cursor.getString( number_id );
            String call_type = cursor.getString( type_id );
            String call_duration = cursor.getString( duration_id );        
            String call_date_st = cursor.getString( date_id );
            
        
        int id=Integer.parseInt(call_type);
         String type="";
        switch(id) {
        case CallLog.Calls.OUTGOING_TYPE:
            type = "Outgoing";
        break;

        case CallLog.Calls.INCOMING_TYPE:
            type = "Incomming";
        break;

        case CallLog.Calls.MISSED_TYPE:
            type = "Missed";
        break;
         }
        
        if (name!=null) {
            LogCollection.logCollectionArr.add(new LogCollection(phone_no,
                    type, name, call_duration, call_date_st));
                
        }else{
            LogCollection.logCollectionArr.add(new LogCollection(phone_no,
                    type, "Unknown", call_duration, call_date_st));
                
        }
        
            }
        cursor.close();
        }

}

 


 

 

LogCollection.java


 

package com.androidfromhome.calllog;

import java.util.ArrayList;

public class LogCollection {

    public static ArrayList<LogCollection> logCollectionArr=null;

    public String phone_no="";
    public String call_type="";
    public String name="";
    public String call_duration="";
    public String call_date_st="";
    
       
    public LogCollection(String phone_no,String call_type,String name,String call_duration,String call_date_st){
        
        this.phone_no=phone_no;
        this.call_type=call_type;
        this.name=name;
        this.call_duration=call_duration;
        this.call_date_st=call_date_st;
        
    }
    
}

 


 

 

LogAdapter.java


 

package com.androidfromhome.calllog;

import java.util.ArrayList;
import java.util.Date;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

public class LogAdapter extends ArrayAdapter<LogCollection>{
    
    private final Context context;
    private final ArrayList<LogCollection> values;
    private ViewHolder viewHolder;
    private final int resourceId;

    public LogAdapter(Context context, int resourceId,ArrayList<LogCollection> values) {
        super(context, resourceId, values);
        // TODO Auto-generated constructor stub
        
        this.context = context;
        this.values = values;
        this.resourceId = resourceId;
    }
    
    
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        
        if (convertView == null) {
            LayoutInflater inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(resourceId, parent, false);
            
                
            viewHolder = new ViewHolder();
            viewHolder.tv_name = (TextView) convertView.findViewById(R.id.tv_name);
            viewHolder.tv_type = (TextView) convertView.findViewById(R.id.tv_type);
            viewHolder.tv_phone = (TextView) convertView.findViewById(R.id.tv_phone);
            
            
                convertView.setTag(viewHolder);
            
            
        }else
        {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        LogCollection list_obj=values.get(position);
        viewHolder.tv_name.setText(list_obj.name);
        viewHolder.tv_phone.setText(list_obj.phone_no+" ("+list_obj.call_type+")");
        
        Date call_date = new Date(Long.valueOf(list_obj.call_date_st));
    
        viewHolder.tv_type.setText("Duration: "+list_obj.call_duration+"Sec.| Date: "+call_date);
            
        
            
        return convertView;
    }
    

    public class ViewHolder {

         TextView tv_name;
         TextView tv_phone;
         TextView tv_type;
        
    }

}


xml view code:-

 

activity_call_log.xml


 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <LinearLayout
        android:id="@+id/LinearLayout2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#A758AB"
        android:gravity="center"
        android:orientation="vertical"
        android:padding="15dp" >

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="2dp"
            android:text="Android From Home"
            android:textColor="#ffffff"
            android:textSize="22dp"
            android:textStyle="bold" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="center"
        android:gravity="center"
        android:orientation="vertical" >

        <ListView
            android:id="@+id/lv_calllog"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >
        </ListView>

    </LinearLayout>

</LinearLayout>


list_item.xml


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:background="#ffffff"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:padding="1dp" >

        <TextView
            android:id="@+id/tv_name"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:padding="2dp"
            android:text="TextView"
            android:textColor="#000000"
            android:textSize="16dp"
            android:textStyle="bold" />

        <TextView
            android:id="@+id/tv_phone"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:ellipsize="end"
            android:maxLength="40"
            android:padding="2dp"
            android:text="Some Text Goes here.."
            android:textColor="#343434"
            android:textSize="12dp"
            android:textStyle="bold" />

        <TextView
            android:id="@+id/tv_type"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:ellipsize="end"
            android:padding="2dp"
            android:text="Some Text Goes here.."
            android:textColor="#343434"
            android:textSize="12dp"
            android:textStyle="bold" />
    </LinearLayout>

    <TextView
        android:id="@+id/textView1"
        android:layout_width="match_parent"
        android:layout_height="2dp"
        android:background="#dfdfdf" />

</LinearLayout>

 


 

Download Full Source Code

Related Post:-

How Retrieve contact list in android

ListView in android example with custom layout

Internet connection checking in android device

Switching One activity  to another activity in Android by intent

Leave a Reply

Your email address will not be published. Required fields are marked *

Get Widget