2

I am making an application which needs to run when there is a incoming call on an Android phone. I want that my application to just listen to incoming calls and run its own Activity in the background. Actually I am making an application which works like when there is a incoming call then an LED blinks on the Arduino board.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
code singh
  • 63
  • 1
  • 2
  • 11

4 Answers4

2

I think Chapter 12. Telephone Applications in Android Cookbook should prove helpful:

The short version, however, is that you need to listen to a broadcast message indicating that the phone state has change. To do that, you subclass BroadcastReceiver and add some code in your manifest to capture the event.

File AndroidManifest.xml:

<application android:icon="@drawable/icon" android:label="Incoming Call Interceptor">

    <receiver android:name="IncomingCallInterceptor">
        <intent-filter>
             <action android:name="android.intent.action.PHONE_STATE"/>
        </intent-filter>
    </receiver>

</application>

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

File IncomingCallInterceptor.java:

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.telephony.TelephonyManager;
import android.widget.Toast;

public class IncomingCallInterceptor extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);
        String msg = "Phone state changed to " + state;

        if (TelephonyManager.EXTRA_STATE_RINGING.equals(state)) {
            String incomingNumber = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);
            msg += ". Incoming number is " + incomingNumber;

            // TODO This would be a good place to "Do something when the phone rings" ;-)
        }
        Toast.makeText(context, msg, Toast.LENGTH_LONG).show();
    }
}

The page I linked to has more information about how this actually works and additional things you could detect (like when the user answers or rejects the call). So make sure to read it.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
entropy
  • 3,134
  • 20
  • 20
  • 2
    What android are you running on? 4.x? If, so, there was a new security measure that was added in ICS. Basically, an app will not receive broadcasts until it has been run by the user for the first time. So make sure your app has a GUI and open it at least once before trying this. – entropy Jul 20 '12 at 12:05
  • Relevant SO question about that: http://stackoverflow.com/questions/9952562/broadcast-receiver-not-working-in-ics-if-the-app-is-not-started-atleast-once – entropy Jul 20 '12 at 12:06
1

Do something when the phone rings explains how to listen for the phone ringing to start, and then you would need to start a service.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
beyerss
  • 1,302
  • 3
  • 21
  • 38
0

You could set an intent filter for that. The intent will be bounded to an activity.

This is what you have to add in your Manifest (inside an activity):

<intent-filter> 
            <action android:name="android.intent.action.CALL" />

As far as I know, this works for outgoing calls, you'll have to check if it works for incoming also..

rMaero
  • 195
  • 4
  • 13
0

If you want to do this using Bluetooth, then look at a project called Amarino which you can rig up with a cheap $6 bluetooth module off the web.

I did this and it works fine.

Cups
  • 6,901
  • 3
  • 26
  • 30