I am starting a service from MainActivity, which run indefinitely looking for incoming and outgoing calls to record.
Why is that the sample "switch-case" doesn't work?
No error occurs but I can record only each of incoming and outgoing.
The service is killed after recording the call, it will be recreated START_STICKY
but never going to get started.
Please help. Thanks in advance!
Is there any other codes to record incoming and outgoing call in android?
package com.exampled.demoserv;
import java.io.File;
import java.io.IOException;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.media.MediaRecorder;
import android.os.Environment;
import android.os.Handler;
import android.os.IBinder;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.widget.Toast;
public class ParentalService extends Service
{
@Override
public void onCreate()
{
//Toast.makeText(getApplicationContext(), "Service Created", Toast.LENGTH_SHORT).show();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId)
{
// TODO Auto-generated method stub
//Toast.makeText(getApplicationContext(), "Subu's Monitoring sTARTED", Toast.LENGTH_SHORT).show();
startMonitor();
return START_STICKY_COMPATIBILITY;
}
@Override
public IBinder onBind(Intent arg0)
{
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "Service Binded", Toast.LENGTH_SHORT).show();
return null;
}
@Override
public void onDestroy()
{
// TODO Auto-generated method stub
super.onDestroy();
Toast.makeText(getApplicationContext(), "Destroyed", Toast.LENGTH_SHORT).show();
}
public void startMonitor()
{
TelephonyManager mTelephonyMgr = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
mTelephonyMgr.listen(new TeleListener(), PhoneStateListener.LISTEN_CALL_STATE);
}
class TeleListener extends PhoneStateListener
{
boolean recording = false;
final MediaRecorder recorder = new MediaRecorder();
String inc_num="", fname;
public void onCallStateChanged(int state, String incomingNumber) throws IllegalStateException
{
super.onCallStateChanged(state, incomingNumber);
switch (state) {
case TelephonyManager.CALL_STATE_IDLE:
//CALL_STATE_IDLE;
Toast.makeText(getApplicationContext(), "CALL_STATE_IDLE : "+Boolean.toString(recording), Toast.LENGTH_SHORT).show();
if(recording==true)
{
recorder.stop();
recorder.reset();
recorder.release();
Toast.makeText(getApplicationContext(), "Released_idle", Toast.LENGTH_SHORT).show();
}
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
Toast.makeText(getApplicationContext(), Boolean.toString(recording)+" : Offhook",Toast.LENGTH_SHORT).show();
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
String file= Environment.getExternalStorageDirectory().toString();
String filepath= file+"/111111111111Aandroid_Subui";
File dir= new File(filepath);
dir.mkdirs();
if(inc_num.length()==0)
{
fname="outgoingNum";
}
filepath+="/"+fname+".3gp";
recorder.setOutputFile(filepath);
try {
recorder.prepare();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
recorder.start();
recording=true;
break;
case TelephonyManager.CALL_STATE_RINGING:
//CALL_STATE_RINGING
inc_num = incomingNumber;
Toast.makeText(getApplicationContext(), "CALL_STATE_RINGING : "+incomingNumber, Toast.LENGTH_SHORT).show();
break;
default:
Toast.makeText(getApplicationContext(), "Default reached", Toast.LENGTH_SHORT).show();
break;
}
}
}
}
Finally Got the Result... Now I can record both incoming and outgoing calls(2.2)..
Changed the whole structure..
Here is my CallRecordingService.java Toasting will make you understood whats going arround... :)
ServiceCaller.java
MainActivity.java
PERMISSIONS
Instead of myAudioRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
use myAudioRecorder.setAudioSource(MediaRecorder.AudioSource.VOICE_CALL);
This particular option is described here> http://developer.android.com/reference/android/media/MediaRecorder.AudioSource.html Voice call uplink + downlink audio source That means the recording will contain both voices.
Be careful when you start recording. When the call is initiated or when the other-party answer the call.