Tuesday, February 24, 2009

Code : Log Phone Calls


BlackBerry JDE 4.0 introduces a phone logs API (net.rim.blackberry.api.phone). The phone logs API allows developers to:

  • manipulate phone data and phone features to further enhance business processes.
  • extract information regarding all phone call activities on a BlackBerry device and organize it accordingly.

Note: The phone logs API is very useful for companies that use time billing.

The functions that are used to navigate through a phone log are as follows:

  • callAt() returns a CallLog object which allows a user to extract data and/or set data.
  • getInstance() returns the current values stored in a Phone Log.

PhoneCallLog (extends CallLog) and PhoneCallLogID classes return data structures from a call record. PhoneCallLogID deals with data regarding a phone number. This class contains getNumber(), getName() and setName().

PhoneCallLog produces the rest of the data such as getDate(), getNotes(), getStatus() and setNotes(). Thus, it is crucial to use all the classes in this API to build an appropriate phone log application.

The following sample illustrates a custom application, which logs all answered calls and their details.

Phone Log Java Source Code

import net.rim.blackberry.api.phone.phonelogs.*;
import net.rim.device.api.ui.*;
import net.rim.device.api.ui.component.*;
import net.rim.device.api.ui.container.*;
import net.rim.device.api.system.*;
import java.util.*;
import net.rim.device.api.i18n.*;
/**
* This sample illustrates logging phone calls or keeping a record of them
* using the phonelogs api. This sample logs all calls except missed calls.
*/
class Phone_Log extends UiApplication{
/**
*an instance of MainScreen, to add display content
*and set display options on the device
*/
MainScreen _screen = new MainScreen();
/**
* instance of Phonethread(used in the background to retrieve data)
*/
Phonethread _phone;
Date _date;
/**
* instance of PhoneLogs API, used for filtering through the log
*/
PhoneLogs _logs;
/**
* will define the total talk time
*/
int _total = 0;
/**
* will hold the string value of the date variable
*/
SimpleDateFormat _formatedDate=
new SimpleDateFormat(DateFormat.DATE_DEFAULT);
/**
* main function that starts the event thread
*/
public static void main(String arg[])
{
Phone_Log application = new Phone_Log();
//create a new instance of the application
//and start the application on the event thread
application.enterEventDispatcher();
}
/**
* Class constructor
* Sets the application title, starts a separate phone thread,
* and pushes the screen on the device
*/
public Phone_Log(){
_screen.setTitle("Phone Log");
_screen.add(new RichTextField("Activities on your Phone"));
_screen.add(new SeparatorField());
pushScreen(_screen);
_phone = new Phonethread();
_phone.start();
}
/**
* Private class that extends thread.
* It will run in the background, and not affect the
* performance of the device.
*/
private class Phonethread extends Thread{
/**
* Class constructor, invokes super
*/
public Phonethread(){
super();
}
/**
* Run method that will receive a copy of the phone log
* and work through it to collect data.
* It will then display the log in chronological order.
* The items that will be displayed are: the date of a call;
* the number of the call; the duration of the call in seconds
* and if there were any notes.
* Uses the screen variable to display items on the device.
* The log will only be taken from the normal folder
* which contains all calls except the missed.
* For a log of missed calls please use missed folder.
*/
public void run(){
_date=new Date();
_logs=PhoneLogs.getInstance();
for(int i=0;
i<(_logs.numberOfCalls(_logs.FOLDER_NORMAL_CALLS));
i++){
//extracting the date from the log
_date=_logs.callAt(i, _logs.FOLDER_NORMAL_CALLS).getDate();
//referencing a call log to a PhoneCallLog so the caller ID,
//the number and notes can be extracted
PhoneCallLog callLog=(PhoneCallLog)_logs.callAt
(i, _logs.FOLDER_NORMAL_CALLS);
String number=callLog.getParticipant().getNumber();
String data=_formatedDate.formatLocal(_date.getTime());
//seeing if a call is received or dialed
//and outputting the correct information
if(callLog.getType() == callLog.TYPE_RECEIVED_CALL){
_screen.add(new RichTextField(
"You received a call from: "+number));
_screen.add(new RichTextField(
"it was received on: "+data));
}
if(callLog.getType() == callLog.TYPE_PLACED_CALL){
_screen.add(new RichTextField(
"The number you dialed was: "+number));
_screen.add(new RichTextField(
"it was made on: "+data));
}
_total+=callLog.getDuration();
//adding the duration of the call
//and any notes that were made
_screen.add(new RichTextField(
"It lasted for: "+ callLog.getDuration() + " secs"));
_screen.add(new RichTextField(""));
_screen.add(new RichTextField(
"These are your notes: " +callLog.getNotes()));
_screen.add(new SeparatorField());
}
//end for
_screen.add(new RichTextField(
"Total time spent on the phone: "+_total+" secs"));
}
}
}

1 comment:

  1. i am new in blackberry development and want to fatch call log and i am not able to run it in the eclipse plase guide me.tejas.goswami@enjayworld.com

    ReplyDelete

Place your comments here...