8

I have an already built application and I want to add a feature that has to be started when a call ends. How can I achieve that? I thought that declaring in my manifest something like this

<activity android:name="Filter">
    <intent-filter>
            <category android:name="android.intent.SOMETHING" />
    </intent-filter>
</activity>

could be enough, but what kind of Intent I have to put on the filter?

Looking in the documentation I found only the intents that detects when a call is started.

Is what I'm looking for possible?

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
lbedogni
  • 7,917
  • 8
  • 30
  • 51

2 Answers2

21

I have done this using a broadcast receiver. Works! code looks like this -

AndroidManifest.xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.gopi"
      android:versionCode="1"
      android:versionName="1.0">
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.WRITE_CONTACTS" />
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />

    <application android:icon="@drawable/icon" android:label="@string/app_name">

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

    </application>
</manifest> 

The IncomingCallTracker code snippet looks like -

public class IncomingCallTracker extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {

        Bundle bundle = intent.getExtras();

            Set<String> keys = bundle.keySet();
        for (String key : keys) {
                Log.i("MYAPP##", key + "="+ bundle.getString(key));
        }       
    }

}

You can look for the key 'state' in the bundle. When its value is 'IDLE' it means call has ended and you can perform whatever action you want to based on this.

N J
  • 27,217
  • 13
  • 76
  • 96
Gopi
  • 10,073
  • 4
  • 31
  • 45
16

You can use the PhoneStateLisenter to listen out for changes in the call state.

So you listen for the LISTEN_CALL_STATE change.

With the onCallStateChanged method.

So when the state changes from OFFHOOK to IDLE start your application

Abhishek Jain
  • 3,562
  • 2
  • 26
  • 44
Donal Rafferty
  • 19,707
  • 39
  • 114
  • 191
  • 1
    Mmm... I wanted that this Intent could be caught even when my application isn't started, like a broadcasting intent. Is that possible? – lbedogni Mar 19 '10 at 14:47
  • There is no system intent for this I beleive you could create a small service that runs in the background that listens for Call state changes and when it changes from OFFHOOK to IDLE send a custom Intent to start your application – Donal Rafferty Mar 19 '10 at 14:53