I follow the example of Record phone calls on android phone? and put it on a BroadcastReceiver to try to record MIC voice (I know it is still limited to record the other side) when phone call incoming and outgoing. My question is: how can I get the state when the user pick up the phone. Because when it is ringing, it also will go to the action of "android.intent.action.PHONE_STATE".
My code:
public class PhoneCallReceiver extends BroadcastReceiver
{
@Override
public void onReceive(Context context, Intent intent)
{
String action = intent.getAction();
if (action.equals("android.intent.action.PHONE_STATE")
{
// Phone call recording
try {
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);
recorder.setOutputFile(<my output dir>);
recorder.prepare();
recorder.start();
recordStarted = true;
telManager = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
telManager.listen(phoneListener, PhoneStateListener.LISTEN_CALL_STATE);
} catch(Exception ex) {
}
}
}
}
private final PhoneStateListener phoneListener = new PhoneStateListener() {
@Override
public void onCallStateChanged(int state, String incomingNumber) {
try {
switch (state) {
case TelephonyManager.CALL_STATE_RINGING: {
//
break;
}
case TelephonyManager.CALL_STATE_OFFHOOK: {
//
break;
}
case TelephonyManager.CALL_STATE_IDLE: {
if (recordStarted) {
recorder.stop();
recordStarted = false;
}
break;
}
default: { }
}
} catch (Exception ex) {
}
}
};
the code in AndroidManifest.xml
<receiver android:name=".PhoneCallReceiver" android:enabled="true">
<intent-filter android:priority="1000">
<action android:name="android.intent.action.PHONE_STATE" />
</intent-filter>
</receiver>
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS" />
Based on Android 2.1 SDK and tested in HTC EVO 4G (Android 2.2)