Developing Call Stats Android Application
Problem Statement:
The application is triggered whenever an outgoing call is made.It fetches the phone number to which outgoing call is made and displays information regarding calls to that number and general information regarding total calls.
Design:
Class Diagram:
In Android,Broadcast is sent whenever an event is occurred. All the applications which are registered for that particular event will receive an intent.For an application to receive outgoing call event It has to register for outgoing call event.After getting broadcast It has to override onReceive method because this is the method which is called when a broadcast is received.All the actions to be performed when a broadcast is received are to be included in this method.
Now,I have to get the phone number to which outgoing call is made.This is passed as an intent when a broadcast is sent.After getting phone number I have to wrap phone number as a bundle and associate this bundle to Intent object and start new activity using this intent.
Now my new activity will be started.since this will be the starting point for that activity I have to override onCreate method.A new layout should be made for this activity and should be set when the app starts for the first time.
The layout should consist of a Textview and a Button.The text in this Textview should be set after all the necessary computations are made.When a Button is clicked The present activity should be finish() ed and the Dialler Activity should appear on the screen.This is all about the design part.It is complete with the class diagram and a sequence diagram below.The details of those diagrams will be given under the diagrams respectively.
Class Diagram showing Associations and Inheritance. |
Details:-
- CallReceiver extends BroadcastReceiver Class and it overrides onReceive() method.
- NewActivity extends Activity Class and it overrides onCreate() method.
- After getting Broadcast and phone number,CallReceiver Class puts phone number in a Bundle, associates it with Intent object and then Starts NewActivity passing Intent object as parameter.
- After NewActivity is started It's onCreate() method is called.Here,we have to include all the code.
- NewActivity gets the phone number sent by CallReceiver class using the getIntent() and getString() methods.
- Context class calls sendBroadcast() method.
- This makes onReceive method to be called which is in CallReceiver.
- Inside onReceive() startActivity is called which starts NewActivity.
- NewActivity gets the phoneNumber using getString() method.
Implemetation:
Let us go through each file and look at what changes I made in each file.
The file structure of the application as seen in eclipse is given below:
FileStructure of Android Project in Eclipse |
- manifest.xml:
Since the application processes outgoing calls,the following code has to be included
<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS"></uses-permission>
For regestering outgoing call broadcast we have to include below code under <receiver> tag
<intent-filter>
<action android:name="android.intent.action.NEW_OUTGOING_CALL" />
since the application acceses call log database we ahve to include following code
<uses-permission android:name="android.permission.READ_CONTACTS"></uses-permission>
That is all about manifest.xml.Note that there are more changes to be made but only important ones are mentioned.
2. CallReceiver.java:
The main changes to be made are shown below
public class CallReceiver extends BroadcastReceiver
{ @Override
public void onReceive(Context context, Intent intent)
{
//do something
bundle1.putString("phone_number", phoneNumber);
intent1.putExtras(bundle1);
context.startActivity(intent1);
}
}
Only the basic changes are shown.
3. NewActivity.java
public class NewActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Bundle bundle = getIntent().getExtras();
final String phoneNumber = bundle.getString("phone_number");
setContentView(R.layout.temp);
//do more
}
}
This code shows how to get phoneNumber and how to set view which is defined in res->layout as temp.xml.
The final screenhsots of the app in sequence are shown below:
Call Log |
NewActivity displayed when a call is made. |
when Proceed button is clicked the above screein is displayed |