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
{
       @Override
	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 : http://developer.android.com/reference/android/telephony/PhoneStateListener.html

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 {
	@Override
	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: http://developer.android.com/reference/android/content/BroadcastReceiver.html

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(R.id.toggleButton1);
        btn.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if(btn.isChecked())
				{
                                 //Do something
				}else
				{
					//Do Something else
				}}});

External Reference: http://developer.android.com/reference/android/widget/ToggleButton.html

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.

Definitions:

  • 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(R.id.listView1);
 lv.setAdapter(wla);

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);
}

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

tv1.setText(cursor.getString(1));
tv2.setText(cursor.getString(2));
}

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

External References:
http://developer.android.com

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 –

http://developer.android.com

Beginning Android Application Development – Wei-Meng Lee