Description:
This example will show you how you can add or delete call logs on your phone from your code in android.
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:
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
{
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
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;
}
}
|
Comments
Post a Comment