Listener for incoming and outgoing calls in Android : Example

This example code shows a listener to a outgoing and incoming calls to device. I wanted to handle something on both incoming and outgoing of calls and wanted to revert to normal state when call was dropped.

This uses a PhoneStateListener : A listener class for monitoring changes in specific telephony states on the device, including service state, signal strength, message waiting indicator (voicemail), and others.

public class CallListener extends PhoneStateListener
	public void onCallStateChanged (int state, String incomingNumber)
    if(TelephonyManager.CALL_STATE_RINGING == state) {
    	//Incoming call handling
    if(TelephonyManager.CALL_STATE_OFFHOOK == state) {
        //Outgoing call handling
    if(TelephonyManager.CALL_STATE_IDLE == state) {
        //Device back to normal state (not in a call)

Do not forget to add following permission(s) in your manifest.xml

<uses-permission android:name="android.permission.READ_PHONE_STATE" />

External References :

Starting a service on boot example

This is an example code for starting a service whenever the phone starts up. You can create service simply or in a separate thread depending on your needs.

You create a broadcast receiver

public class broadcastRcv extends android.content.BroadcastReceiver {
	public void onReceive(Context context, Intent intent) {
		// TODO Auto-generated method stub
		context.startService(new Intent(context,ServiceExample.class));
               // This is just an example service being started

Do not forget to add the following permission(s) in your manifest.xml

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

External Reference:

ToggleButton Example

ToggleButton : Displays checked/unchecked states as a button with a “light” indicator and by default accompanied with the text “ON” or “OFF”.

final ToggleButton btn = (ToggleButton) findViewById(;
        btn.setOnClickListener(new View.OnClickListener() {

			public void onClick(View v) {
				// TODO Auto-generated method stub
                                 //Do something
					//Do Something else

External Reference:

ListView using Custom CursorAdapter : example

This is an example of using Lists (ListView) in Android and populating the List with the data from your database which comes in a cursor. So this is an example using custom CursorAdapter.


  • ListView: A view that shows items in a vertically scrolling list.
  • Cursor: This interface provides random read-write access to the result set returned by a database query.
  • CursorAdapter: Adapter that exposes data from a Cursor to a ListView widget. The Cursor must include a column named “_id” or this class will not work.
Code in your Activity class
wordsListAdapter wla = new wordsListAdapter(this, cursor);
 ListView lv = (ListView)findViewById(;

Code for defining custom CursorAdapter

public class wordsListAdapter extends CursorAdapter
LayoutInflater inflater;
public wordsListAdapter(Context context, Cursor c) {
super(context, c);
inflater = LayoutInflater.from(context);

public void bindView(View view, Context context, Cursor cursor) {
// TODO Auto-generated method stub
TextView tv1 = (TextView)view.findViewById(;
TextView tv2 = (TextView)view.findViewById(;


public View newView(Context context, Cursor cursor, ViewGroup parent) {
return inflater.inflate(R.layout.word_list_item, parent, false);

External References:

Activities Life Cycle

Activities in Android are the windows which contain the user interface and this is what user interacts with.

It has a  life cycle (as shown in Fig.) which might be really important, so its good to know about this.

  •  onCreate() — Called when the activity is first created (for eg. when you start the application and you see an activity /screen for first time)
  • onStart() — Called when the activity becomes visible to the user
  • onResume() — Called when the activity starts interacting with the user
  • onPause() — Called when the current activity is being paused and the previous activity is being resumed
  • onStop() — Called when the activity is no longer visible to the user
  • onDestroy() — Called before the activity is destroyed by the system (either manually or by the system to conserve memory)
  • onRestart() — Called when the activity has been stopped and is restarting again

State diagram for an Android Activity Lifecycle.

References –

Beginning Android Application Development – Wei-Meng Lee