In this Android tip, I am going to show you how to use the query() method of the ContentResolver class to retrieve call logs in an Android device. The query method has the following prototype:
query(Uri url, String[] projection, String selection, String selectionArgs, String sortOrder)
With the url argument, you can specify a table in which the data will be retrieved. To refer to the call logs table, you can write CallLog.Calls.CONTENT_URI . The projection is a list of columns to fetch the data. The selection is WHERE clause (excluding the WHERE itself) to filter rows of data. Passing null will return all rows from the table. You may include ?s in selection, which will be replaced by the values from selectionArgs. The selectionArgs are values to replace the ? symbol in the selection argument. The sortOrder specified how to sort the rows. Passing null will use the default sort order, which may be unordered.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity"
>
<ListView
android:id="@+id/lst_calls"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="5dp" >
<TextView
android:id="@+id/txt_number"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginRight="10dp"
/>
<TextView
android:id="@+id/txt_date"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_toRightOf="@+id/txt_number"
android:layout_marginRight="10dp"
/>
<TextView
android:id="@+id/txt_type"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_toRightOf="@+id/txt_date"
/>
</RelativeLayout>
package com.example.andtip;
import java.util.ArrayList;
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 CustomAdapter extends ArrayAdapter<String> {
int groupid;
ArrayList<String> records;
Context context;
public CustomAdapter(Context context, int vg, int id, ArrayList<String> records){
super(context,vg, id, records);
this.context=context;
groupid=vg;
this.records=records;
}
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View itemView = inflater.inflate(groupid, parent, false);
String[] row_items=records.get(position).split("__");
TextView textNumber= (TextView) itemView.findViewById(R.id.txt_number);
textNumber.setText(row_items[0]);
TextView textDate= (TextView) itemView.findViewById(R.id.txt_date);
textDate.setText(row_items[1]);
TextView textType= (TextView) itemView.findViewById(R.id.txt_type);
textType.setText(row_items[2]);
return itemView;
}
}
package com.example.andtip;
import java.util.ArrayList;
import java.util.Date;
import android.app.Activity;
import android.app.PendingIntent;
import android.app.ProgressDialog;
import android.content.ContentResolver;
import android.database.Cursor;
import android.os.Bundle;
import android.text.format.DateFormat;
import android.widget.ListView;
import android.provider.CallLog;
public class MainActivity extends Activity{
ArrayList<String> records;
CustomAdapter adapter;
ListView listview;
protected void onCreate(Bundle savedInstanceState) {
//TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
context=this;
records=new ArrayList<String>();
listview=(ListView)findViewById(R.id.lst_calls);
adapter=new CustomAdapter(this, R.layout.list_item, R.id.txt_number,records);
listview.setAdapter(adapter);
}
public void onStart(){
super.onStart();
//get an instance of ContentResolver
ContentResolver cr=getContentResolver();
//define columns (number, date, and type) to fetch data
String[] projection={CallLog.Calls.NUMBER,CallLog.Calls.DATE, CallLog.Calls.TYPE};
//use query method to fetch data from the call logs table
Cursor cs=cr.query(CallLog.Calls.CONTENT_URI, projection, null, null, null);
if(cs.getCount()>0)
while(cs.moveToNext()){
String number=cs.getString(cs.getColumnIndex(projection[0]));
Long date=cs.getLong(cs.getColumnIndex(projection[1]));
Date d=new Date(date);
CharSequence callDate=DateFormat.format("yyyy-MM-dd hh:mm:ss", d);
String type=cs.getString(cs.getColumnIndex(projection[2]));
String item=number+"__"+callDate.toString()+"__"+type;
records.add(item);
}
//notify listview of dataset changed
adapter.notifyDataSetChanged();
}
}
<uses-permission android:name="android.permission.READ_CALL_LOG"/>
Posted by: Dara | post date: 08-10-2014 | Subject: Android Apps Development
|
This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.