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.

- 30,738
- 21
- 105
- 131

- 63
- 1
- 2
- 11
-
Why is this tagged "arduino" by the way? – entropy Jul 20 '12 at 11:47
-
because i want to show the incoming call status on arduino – code singh Jul 20 '12 at 11:59
4 Answers
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.

- 30,738
- 21
- 105
- 131

- 3,134
- 20
- 20
-
2What 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
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.

- 30,738
- 21
- 105
- 131

- 1,302
- 3
- 21
- 38
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..

- 195
- 4
- 13
-
He needs PHONE_STATE for incoming calls. CALL only works for outgoing calls. – entropy Jul 20 '12 at 11:49
-