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

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() {        ...

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      *     ...

Improving Layout Performance

Improving Layout Performance Layouts are a key part of Android applications that directly affect the user experience. If implemented poorly, your layout can lead to a memory hungry application with slow UIs. The Android SDK includes tools to help you identify problems in your layout performance, which when combined the lessons here, you will be able to implement smooth scrolling interfaces with a minimum memory footprint. Lessons Optimizing Layout Hierarchies In the same way a complex web page can slow down load time, your layout hierarchy if too complex can also cause performance problems. This lesson shows how you can use SDK tools to inspect your layout and discover performance bottlenecks. Re-using Layouts with <include/> If your application UI repeats certain layout constructs in multiple places, this lesson shows you how to create efficient, re-usable layout constructs, then include them in the appropriate UI layouts. Loading Views On Demand Be...