Thursday 18 December 2014

Reading Call Log

Hello Friends


Here i am showing you how to read call log's information in android.
Its pretty simple.

First in order to read call logs we need to give permission to manifest file.

The manifest file code:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="in.blogspot.longjamcode"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="14"
        android:targetSdkVersion="19" />

Mandatory to give this permission
    <uses-permission android:name="android.permission.READ_CALL_LOG" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>


Here i am using a TextView to print call logs information and its inside a Scroll View.

The layout file code:


<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" >

    <ScrollView
        android:id="@+id/scrollView1"
        android:layout_width="match_parent"
        android:padding="5dp"
        android:layout_height="wrap_content" >

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

            <TextView
                android:id="@+id/callLogTextView"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />

        </LinearLayout>
    </ScrollView>

</LinearLayout>

Here the MainActivity java file code:

package in.blogspot.longjamcode;

import java.util.Date;

import android.app.Activity;
import android.content.ContentResolver;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.CallLog;
import android.widget.TextView;

public class MainActivity extends Activity {

TextView callLogTextView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
callLogTextView = (TextView) findViewById(R.id.callLogTextView);

StringBuilder sb = new StringBuilder();
ContentResolver contentResolver = getContentResolver();
Cursor cursor = contentResolver.query(CallLog.Calls.CONTENT_URI, null,null, null, null);
while (cursor.moveToNext()) {
String phoneNumber = cursor.getString(cursor.getColumnIndex(CallLog.Calls.NUMBER));
String name = cursor.getString(cursor.getColumnIndex(CallLog.Calls.CACHED_NAME));
String callType = cursor.getString(cursor.getColumnIndex(CallLog.Calls.TYPE));
String callDate = cursor.getString(cursor.getColumnIndex(CallLog.Calls.DATE));
String callDuration = cursor.getString(cursor.getColumnIndex(CallLog.Calls.DURATION));
String callTypeTemp = null;
Date date = new Date(Long.parseLong(callDate));
if(callType.equals("1"))
callTypeTemp = "INCOMING_TYPE";
else if(callType.equals("2"))
callTypeTemp = "OUTGOING_TYPE";
else if(callType.equals("3"))
callTypeTemp = "MISSED_TYPE";
sb.append("\nPhone Number:   " + phoneNumber + " \nName:   " + name + " \nCall Type:   "
+ callTypeTemp + " \nCall Date:   " + date + " \nCall duration:   " + callDuration+" sec");
sb.append("\n \n");
}
cursor.close();
callLogTextView.setText(sb);
}
}




For full source code click here

No comments:

Post a Comment