Listen incoming calls through BroadcastReceiver, without PhoneStateIntentReceiver or PhoneStateListener

CR Sardar picture CR Sardar · Mar 30, 2011 · Viewed 12.2k times · Source

Is there any way to listen to incoming calls by extending BroadcastReceiver to listen to OS's broadcast,without using PhoneStateIntentReceiver or PhoneStateListener. Also please tell me what will be action and permissions in manifest.

I've tried with as follows but it is not working for incoming calls but working for outgoing

The only one .java file of app is as follows(the app only one .java file and one manifest file)

package com.crsardar.media.audio;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

public class IncommingCallReceiverCRS extends BroadcastReceiver{

    @Override
    public void onReceive(Context context, Intent intent) {
        Log.e("Chitta : ", "Its working");          
    }
}

manifest

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.crsardar.media.audio"
      android:versionCode="1"
      android:versionName="1.0">
  <application android:icon="@drawable/icon" android:label="@string/app_name">

    <receiver android:name="IncommingCallReceiverCRS" android:enabled="true"> 
        <intent-filter>
            <!--action android:name="android.intent.action.NEW_OUTGOING_CALL"/-->
            <action  android:name="android.intent.action.ANSWER" >
            <category  android:name="android.intent.category.DEFAULT" /> 
        </intent-filter>
    </receiver>

  </application>

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

Answer

Muzikant picture Muzikant · May 20, 2011

The action you have defined in your manifest is incorrect. this is an Action Intent that can be used to answer a call and not monitor incoming calls.

You can use two broadcast receivers that listen to ACTION_PHONE_STATE_CHANGED and NEW_OUTGOING_CALL broadcast intents.

The ACTION_PHONE_STATE_CHANGED will be received when there is a new incoming call, call answered or hangup (See the documentation for the EXTRAs received with this Intent).

The NEW_OUTGOING_CALL will be received when there is a new outgoing call placed on your device.

As for permissions, I think you got it about right in your manifest (I assume the RECORD_AUDIO permission is used for something else in your application)