['Gautam Chekuri', 'gautam.chekuri@gmail.com', 'http://github.com/gautamc']
[Observe ∿ deconstruct ∿ Generalize ∿ Reflect ∿ Repeat]

« Back to /index


An android app + Web app setup to push an incoming phone call’s number to the browser.

29 Dec 2012


1. Certain web applications, for example CRM systems, involve searching for a user/member profile based on a phone number. This typically happens when the user/member makes a phone call to the “support team”.
It can be time consuming for the “support team” to ask the user/member his/her phone number, key it into a user interface/form provided by the web application and pull up the relevant data/web page.

2. To automate this process, we can use a simple android app that can listen for incoming phone calls and push the corresponding phone calls to the web app. The web app can then use a comet server to push events/notifications to the support team’s browsers.

3. To test this scenario, I put in place a toy implementation of an android app and a web application – as described below in “the details” section.

For real applications, I could add a “login” workflow in the Android app. This way a support team member can use the same login credentials for both the web app and the Android app and every time they get a call from a member/user/customer on their Android phone, the web application will automatically pull up the “profile” of that member/user/customer.

The Details:

3. As an illustration I implemented a simple android app.

=> This application first creates a PhoneStateListener subclass to wait for incoming phone calls, gets the phone number when incoming call arrives and submits it to a web server.

=> On application startup, an instance of this PhoneStateListener subclass is registered with the TelephonyManager. Due to this the Android OS will send the required events to our PhoneStateListener subclass’s object.

=> The PhoneStateListener subclass

public class IncomingCallListener extends android.telephony.PhoneStateListener {

    private NotifyOnCall myActivity;

    public IncomingCallListener(NotifyOnCall myActivity){
	this.myActivity = myActivity;
    public void onCallStateChanged(int state, String incomingCallNumber) {
	if( TelephonyManager.CALL_STATE_RINGING == state ) {
	    System.out.println(" -- In the listener -- " + incomingCallNumber);
	    try {
	    } catch(Exception e) {

    private void executeHttpGet(String phone_number) throws Exception {
	HttpClient client = new DefaultHttpClient();
	HttpGet request = new HttpGet();
	request.setURI(new URI(myActivity.getNotifyURL() + phone_number));
	HttpResponse response = client.execute(request);

=> The TelephonyManager setup method in the main activity

    private void setupIncomingCallListener() {
	IncomingCallListener incomingCallListener = new IncomingCallListener(this);
	TelephonyManager telephonyManager =
	    (TelephonyManager) this.getSystemService(Context.TELEPHONY_SERVICE);
	    incomingCallListener, IncomingCallListener.LISTEN_CALL_STATE

4. On the web application, I setup a new action that implements the URL to which the android app submits the phone number.

 def push
   Notification.notify("Reciving Incoming Call from #{params[:phone]}")
   render :text => "OK"

The web application uses a faye server to push notifications to connected browsers.

class Notification < ActiveRecord::Base

  def self.notify(msg)
      uri = URI.parse("#{FAYE_PROTO}://#{FAYE_HOST}:6296/faye")
      http = Net::HTTP.new(uri.host, uri.port)
      if FAYE_PROTO == "https"
        http.use_ssl = true

      notification = {
        :channel => "/notifications",
        :data => msg
      post_request = Net::HTTP::Post.new(uri.path)
      post_request.set_form_data({:notification => notification.to_json})
      post_response = http.request(post_request)



« Back to /index