Skip to main content

Call Log Example in android



Description:
This example will show you how you can add or delete call logs on your phone from your code in android.
Algorithm:
1.) Create a new project by File-> New -> Android Project name it CallLogExample.
2.) Write following into your manifest file:

<?xml version="1.0" encoding="utf-8"?>
    package="com.example.calllogexample"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />

    <uses-permission android:name="android.permission.READ_CONTACTS"></uses-permission>
    <uses-permission android:name="android.permission.WRITE_CONTACTS"></uses-permission>
    <uses-permission android:name="android.permission.READ_CALL_LOG"/>
    <uses-permission android:name="android.permission.WRITE_CALL_LOG"/>
     
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.calllogexample.CallLogExampleActivity"
            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>
3.) Write following into 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"
    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=".CallLogExampleActivity" >

     

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView1"
        android:layout_below="@+id/textView1"
        android:layout_marginTop="24dp"
        android:text="Click to Add Call Log" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/button1"
        android:layout_below="@+id/button1"
        android:layout_marginTop="16dp"
        android:text="Click to Delete Call Log" />

</RelativeLayout>
4.) Create and write following into CallLogActivity.java:

package com.example.calllogexample;

import android.content.ContentResolver;
import android.content.ContentValues;
import android.net.Uri;
import android.provider.CallLog;
import android.util.Log;

public class CallLogActivity {

    public  void  AddNumToCallLog(ContentResolver resolver ,String strNum, int type, long timeInMiliSecond)
        {
            while(strNum.contains("-"))
            {
                strNum =strNum.substring(0,strNum.indexOf('-')) + strNum.substring(strNum.indexOf('-')+1,strNum.length());
            }
            ContentValues values = new ContentValues();
            values.put(CallLog.Calls.NUMBER, strNum);
            values.put(CallLog.Calls.DATE, timeInMiliSecond);
            values.put(CallLog.Calls.DURATION, 0);
            values.put(CallLog.Calls.TYPE, type);
            values.put(CallLog.Calls.NEW, 1);
            values.put(CallLog.Calls.CACHED_NAME, "");
            values.put(CallLog.Calls.CACHED_NUMBER_TYPE, 0);
            values.put(CallLog.Calls.CACHED_NUMBER_LABEL, "");
            Log.d("AddToCallLog", "Inserting call log placeholder for " + strNum);

            if(null != resolver)
            {
                resolver.insert(CallLog.Calls.CONTENT_URI, values);
            }
            //getContentResolver().delete(url, where, selectionArgs)
        }

        public void DeleteNumFromCallLog(ContentResolver resolver, String strNum)
        {
            try
            {
                String strUriCalls = "content://call_log/calls";
                Uri UriCalls = Uri.parse(strUriCalls);
                //Cursor c = res.query(UriCalls, null, null, null, null);
                if(null != resolver)
                {
                    resolver.delete(UriCalls,CallLog.Calls.NUMBER +"=?",new String[]{ strNum});
                }
            }
            catch(Exception e)
            {
                e.getMessage();
            }
        }
}
5.) Run for output.

Steps:
1.) Create a project named CallLogExample and set the information as stated in the image.
Build Target: Android 4.2
Application Name: CallLogExample
Package Name: com.example.CallLogExample
Activity Name: CallLogExampleActivity
Min SDK Version: 4.2

2.) Open CallLogExampleActivity.java file and write following code there:

package com.example.calllogexample;

import android.app.Activity;
import android.os.Bundle;
import android.provider.CallLog;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class CallLogExampleActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
         
        Button addButton = (Button) findViewById(R.id.button1);
        addButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                  //Add number into calllog
                  long  callTimeInMiliSecond    = System.currentTimeMillis(); //time stamp
                  //CallLogUtility is the class where we have written our add/delete function
                  CallLogActivity utility = new CallLogActivity();
                  //number to add
                  String numberStr = "+919867034241";
                  utility.AddNumToCallLog(getBaseContext().getContentResolver(),numberStr, CallLog.Calls.OUTGOING_TYPE, callTimeInMiliSecond);
            }
        });
         
        Button deleteButton = (Button) findViewById(R.id.button2);
        deleteButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                //if you want to delete any number from call log then follow below 2 lines of code
                String numberStr = "+919867034241"; // delete this number from call log
                CallLogActivity utility = new CallLogActivity();
                utility.DeleteNumFromCallLog(getBaseContext().getContentResolver(), numberStr);
                }
        });
    }

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

}
3.) Compile and build the project.

Comments

Popular posts from this blog

Bitmap scalling and cropping from center

How to Bitmap scalling and cropping from center? public class ScalingUtilities {     /**      * Utility function for decoding an image resource. The decoded bitmap will      * be optimized for further scaling to the requested destination dimensions      * and scaling logic.      *      * @param res      *            The resources object containing the image data      * @param resId      *            The resource id of the image data      * @param dstWidth      *            Width of destination area      * @param dstHeight      *     ...

Custom camera using SurfaceView android with autofocus & auto lights & more

Custom camera using SurfaceView android with autofocus & auto lights & much more /**  * @author Tatyabhau Chavan  *  */ public class Preview extends SurfaceView implements SurfaceHolder.Callback {     private SurfaceHolder mHolder;     private Camera mCamera;     public Camera.Parameters mParameters;     private byte[] mBuffer;     private Activity mActivity;     // this constructor used when requested as an XML resource     public Preview(Context context, AttributeSet attrs) {         super(context, attrs);         init();     }     public Preview(Context context) {         super(context);         init();     }     public Camera getCamera() {        ...

Get Android phone call history/log programmatically

Get Android phone call history/log programmatically To get call history programmatically first add read conact permission in Manifest file : <uses-permission android:name="android.permission.READ_CONTACTS" /> Create xml file. Add the below code in xml file : <Linearlayout android:layout_height="fill_parent"  android:layout_width="fill_parent" android:orientation="vertical"> <Textview android:id="@+id/call" android:layout_height="fill_parent" android:layout_width="fill_parent"> </Textview> </Linearlayout> Now call the getCallDetails() method in java class : private void getCallDetails() { 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 = managedCur...