Hi Guys!,
Today I am sharing the code to get the call logs of a android device. For Access the call logs we need to first the aware about Content Provider.
Content providers manage access to a structured set of data. They encapsulate the data, and provide mechanisms for defining data security. Content providers are the standard interface that connects data in one process with code running in another process.
More About the Content Provider you can visit the android developer site Here.
One of the most commonly used Content Providers is CallLog.Calls which is used to provide access to call logs data. Call logs contain information about outgoing, incoming and missed calls.
Lets start the coding for call logs data accessing from the device.
You can download the source code from Here Call Logs
Today I am sharing the code to get the call logs of a android device. For Access the call logs we need to first the aware about Content Provider.
Content providers manage access to a structured set of data. They encapsulate the data, and provide mechanisms for defining data security. Content providers are the standard interface that connects data in one process with code running in another process.
More About the Content Provider you can visit the android developer site Here.
One of the most commonly used Content Providers is CallLog.Calls which is used to provide access to call logs data. Call logs contain information about outgoing, incoming and missed calls.
Lets start the coding for call logs data accessing from the device.
main_activity.xml
list_row.xml
MainActivity.java
package com.sunil.callloginfo;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import android.app.Activity;
import android.content.Context;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.CallLog;
import android.widget.ListView;
public class MainActivity extends Activity {
private ListView listview=null;
private String callType=null;
private String phoneNumber=null;
private String callDate=null;
private String callDuration=null;
private Date callDateTime=null;
private Listlist=new ArrayList ();
private Context context=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
context=this;
listview=(ListView)findViewById(R.id.listView_calldata);
getCallDetails();
CustomAdapter adapter=new CustomAdapter(MainActivity.this, list);
listview.setAdapter(adapter);
}
public void getCallDetails()
{
@SuppressWarnings("deprecation")
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);
while (managedCursor.moveToNext())
{
phoneNumber = managedCursor.getString(number);
callType = managedCursor.getString(type);
callDate = managedCursor.getString(date);
callDateTime = new Date(Long.valueOf(callDate));
callDuration = managedCursor.getString(duration);
String cType = null;
int cTypeCode = Integer.parseInt(callType);
switch(cTypeCode)
{
case CallLog.Calls.OUTGOING_TYPE:
cType = "OUTGOING";
break;
case CallLog.Calls.INCOMING_TYPE:
cType= "INCOMING";
break;
case CallLog.Calls.MISSED_TYPE:
cType = "MISSED";
break;
}
CallData calldata=new CallData(cType, phoneNumber, callDateTime, callDuration);
list.add(calldata);
}
managedCursor.close();
}
}
CallData.java
package com.sunil.callloginfo;
import java.io.Serializable;
import java.util.Date;
public class CallData implements Serializable{
private String calltype;
private String callnumber;
private Date calldatetime;
private String callduration;
public CallData()
{
}
public CallData(String calltype, String callnumber, Date calldatetime, String callduration)
{
this.calldatetime=calldatetime;
this.callduration=callduration;
this.callnumber=callnumber;
this.calltype=calltype;
}
public String getCalltype() {
return calltype;
}
public void setCalltype(String calltype) {
this.calltype = calltype;
}
public String getCallnumber() {
return callnumber;
}
public void setCallnumber(String callnumber) {
this.callnumber = callnumber;
}
public Date getCalldatetime() {
return calldatetime;
}
public void setCalldatetime(Date calldatetime) {
this.calldatetime = calldatetime;
}
public String getCallduration() {
return callduration;
}
public void setCallduration(String callduration) {
this.callduration = callduration;
}
}
CustomAdapter.java
package com.sunil.callloginfo;
import java.util.Date;
import java.util.List;
import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
public class CustomAdapter extends ArrayAdapter{
private Listlistdata=null;
private LayoutInflater mInflater=null;
public CustomAdapter(Activity context, Listcalldata) {
super(context, 0);
this.listdata=calldata;
mInflater = context.getLayoutInflater();
}
@Override
public int getCount() {
return listdata.size();
}
public View getView(int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
if (convertView == null || convertView.getTag() == null) {
holder = new ViewHolder();
convertView = mInflater.inflate(R.layout.list_row, null);
holder.callnumber = (TextView) convertView.findViewById(R.id.textView_callnumber);
holder.calltype = (TextView) convertView.findViewById(R.id.textView_calltype);
holder.calldate = (TextView) convertView.findViewById(R.id.textView_calldate);
holder.callduration = (TextView) convertView.findViewById(R.id.textView_callduration);
convertView.setTag(holder);
}
else {
holder = (ViewHolder) convertView.getTag();
}
CallData calldatalist=listdata.get(position);
String callnumber=calldatalist.getCallnumber();
String calltype=calldatalist.getCalltype();
Date calldate= calldatalist.getCalldatetime();
String callduration=calldatalist.getCallduration();
holder.callnumber.setText("Call Number: "+callnumber);
holder.calltype.setText("Call Type: "+calltype);
holder.calldate.setText("Call Date: "+String.valueOf(calldate));
holder.callduration.setText("Duration: "+callduration);
return convertView;
}
private static class ViewHolder {
TextView callnumber;
TextView calltype;
TextView calldate;
TextView callduration;
}
}
Manifest.xml
You can download the source code from Here Call Logs

0 comments:
Post a Comment