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

Volley for client and server interaction (Android)

Volley provides client and server interaction with QUEUE basis, each and every request process separately, we can cancel our request from QUEUE. HERE is a code sample for Volley with json request format. GenerateRequest.java -------------------------------------------------------------------- package com.entrata.maintenance.network_communication.networkutils; import android.content.Context; import com.android.volley.AuthFailureError; import com.android.volley.Response; import com.android.volley.VolleyError; import com.android.volley.toolbox.JsonObjectRequest; import com.entrata.maintenance.application.ERPUserDefaults; import com.entrata.maintenance.network_communication.networkconstant.NetworkConstant; import com.entrata.maintenance.network_communication.networkexception.Error; import com.entrata.maintenance.network_communication.networkutil.SubdomainChange; import com.entrata.maintenance.utils.Logger; import com.google.gson.Gson; import org.json.JSONException; import org.js...
Java Code Examples for javax.net.ssl.HttpsURLConnection The following code examples are extracted from open source projects. You can click to vote up the examples you like. Your votes will be used in an intelligent system to get more and better code examples. Thanks for your votes! Code Example 1:   9  From project ADFS , under directory /adfs-hdfs-project/adfs-hdfs/src/main/java/org/apache/hadoop/hdfs/ . Source HsftpFileSystem.java @Override protected HttpURLConnection openConnection ( String path , String query ) throws IOException { query = addDelegationTokenParam ( query ); final URL url = new URL ( "https" , nnAddr . getHostName (), nnAddr . getPort (), path + '?' + query ); HttpsURLConnection conn =( HttpsURLConnection ) url . openConnection (); conn . setHostnameVerifier ( new DummyHostnameVerifier ()); return ( HttpURLConnection ) conn ; } Code Example 2:  ...

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