I want to make an app which records the incoming and outgoing calls and it run automatically when user get or make any call.
iOS products have an app called voice memo, and Android has voice recorder, so if you take the phone call in loudspeaker mode, and set up the second device to record, you can pick up both sides of the conversation.
Ok, for this first of all you need to use Device Policy Manager, and need to make your device Admin device. After that you have to create one BroadCast receiver and one service. I am posting code here and its working fine.
MainActivity:
public class MainActivity extends Activity { private static final int REQUEST_CODE = 0; private DevicePolicyManager mDPM; private ComponentName mAdminName; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); try { // Initiate DevicePolicyManager. mDPM = (DevicePolicyManager) getSystemService(Context.DEVICE_POLICY_SERVICE); mAdminName = new ComponentName(this, DeviceAdminDemo.class); if (!mDPM.isAdminActive(mAdminName)) { Intent intent = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN); intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, mAdminName); intent.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION, "Click on Activate button to secure your application."); startActivityForResult(intent, REQUEST_CODE); } else { // mDPM.lockNow(); // Intent intent = new Intent(MainActivity.this, // TrackDeviceService.class); // startService(intent); } } catch (Exception e) { e.printStackTrace(); } } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (REQUEST_CODE == requestCode) { Intent intent = new Intent(MainActivity.this, TService.class); startService(intent); } } }
//DeviceAdminDemo class
public class DeviceAdminDemo extends DeviceAdminReceiver { @Override public void onReceive(Context context, Intent intent) { super.onReceive(context, intent); } public void onEnabled(Context context, Intent intent) { }; public void onDisabled(Context context, Intent intent) { }; }
//TService Class
public class TService extends Service { MediaRecorder recorder; File audiofile; String name, phonenumber; String audio_format; public String Audio_Type; int audioSource; Context context; private Handler handler; Timer timer; Boolean offHook = false, ringing = false; Toast toast; Boolean isOffHook = false; private boolean recordstarted = false; private static final String ACTION_IN = "android.intent.action.PHONE_STATE"; private static final String ACTION_OUT = "android.intent.action.NEW_OUTGOING_CALL"; private CallBr br_call; @Override public IBinder onBind(Intent arg0) { // TODO Auto-generated method stub return null; } @Override public void onDestroy() { Log.d("service", "destroy"); super.onDestroy(); } @Override public int onStartCommand(Intent intent, int flags, int startId) { // final String terminate =(String) // intent.getExtras().get("terminate");// // intent.getStringExtra("terminate"); // Log.d("TAG", "service started"); // // TelephonyManager telephony = (TelephonyManager) // getSystemService(Context.TELEPHONY_SERVICE); // TelephonyManager // // object // CustomPhoneStateListener customPhoneListener = new // CustomPhoneStateListener(); // telephony.listen(customPhoneListener, // PhoneStateListener.LISTEN_CALL_STATE); // context = getApplicationContext(); final IntentFilter filter = new IntentFilter(); filter.addAction(ACTION_OUT); filter.addAction(ACTION_IN); this.br_call = new CallBr(); this.registerReceiver(this.br_call, filter); // if(terminate != null) { // stopSelf(); // } return START_NOT_STICKY; } public class CallBr extends BroadcastReceiver { Bundle bundle; String state; String inCall, outCall; public boolean wasRinging = false; @Override public void onReceive(Context context, Intent intent) { if (intent.getAction().equals(ACTION_IN)) { if ((bundle = intent.getExtras()) != null) { state = bundle.getString(TelephonyManager.EXTRA_STATE); if (state.equals(TelephonyManager.EXTRA_STATE_RINGING)) { inCall = bundle.getString(TelephonyManager.EXTRA_INCOMING_NUMBER); wasRinging = true; Toast.makeText(context, "IN : " + inCall, Toast.LENGTH_LONG).show(); } else if (state.equals(TelephonyManager.EXTRA_STATE_OFFHOOK)) { if (wasRinging == true) { Toast.makeText(context, "ANSWERED", Toast.LENGTH_LONG).show(); String out = new SimpleDateFormat("dd-MM-yyyy hh-mm-ss").format(new Date()); File sampleDir = new File(Environment.getExternalStorageDirectory(), "/TestRecordingDasa1"); if (!sampleDir.exists()) { sampleDir.mkdirs(); } String file_name = "Record"; try { audiofile = File.createTempFile(file_name, ".amr", sampleDir); } catch (IOException e) { e.printStackTrace(); } String path = Environment.getExternalStorageDirectory().getAbsolutePath(); recorder = new MediaRecorder(); // recorder.setAudioSource(MediaRecorder.AudioSource.VOICE_CALL); recorder.setAudioSource(MediaRecorder.AudioSource.VOICE_COMMUNICATION); recorder.setOutputFormat(MediaRecorder.OutputFormat.AMR_NB); recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB); recorder.setOutputFile(audiofile.getAbsolutePath()); try { recorder.prepare(); } catch (IllegalStateException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } recorder.start(); recordstarted = true; } } else if (state.equals(TelephonyManager.EXTRA_STATE_IDLE)) { wasRinging = false; Toast.makeText(context, "REJECT || DISCO", Toast.LENGTH_LONG).show(); if (recordstarted) { recorder.stop(); recordstarted = false; } } } } else if (intent.getAction().equals(ACTION_OUT)) { if ((bundle = intent.getExtras()) != null) { outCall = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER); Toast.makeText(context, "OUT : " + outCall, Toast.LENGTH_LONG).show(); } } } } }
//Permission in manifest file
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.RECORD_AUDIO" /> <uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS" /> <uses-permission android:name="android.permission.READ_PHONE_STATE" /> <uses-permission android:name="android.permission.STORAGE" />
//my_admin.xml
<device-admin xmlns:android="http://schemas.android.com/apk/res/android" > <uses-policies> <force-lock /> </uses-policies> </device-admin>
//Declare following thing in manifest:
Declare DeviceAdminDemo class to manifest:
<receiver android:name="com.example.voicerecorder1.DeviceAdminDemo" android:description="@string/device_description" android:label="@string/device_admin_label" android:permission="android.permission.BIND_DEVICE_ADMIN" > <meta-data android:name="android.app.device_admin" android:resource="@xml/my_admin" /> <intent-filter> <action android:name="android.app.action.DEVICE_ADMIN_ENABLED" /> <action android:name="android.app.action.DEVICE_ADMIN_DISABLED" /> <action android:name="android.app.action.DEVICE_ADMIN_DISABLE_REQUESTED" /> </intent-filter> </receiver> <service android:name=".TService" > </service>
Below code is working for me to record a outgoing phone call
//Call Recording varibales private static final String AUDIO_RECORDER_FILE_EXT_3GP = ".3gp"; private static final String AUDIO_RECORDER_FILE_EXT_MP4 = ".mp4"; private static final String AUDIO_RECORDER_FOLDER = "AudioRecorder"; private MediaRecorder recorder = null; private int currentFormat = 0; private int output_formats[] = { MediaRecorder.OutputFormat.MPEG_4, MediaRecorder.OutputFormat.THREE_GPP }; private String file_exts[] = { AUDIO_RECORDER_FILE_EXT_MP4, AUDIO_RECORDER_FILE_EXT_3GP }; AudioManager audioManager;
//put this methods to outside of oncreate() method
private String getFilename() { String filepath = Environment.getExternalStorageDirectory().getPath(); File file = new File(filepath, AUDIO_RECORDER_FOLDER); if (!file.exists()) { file.mkdirs(); } return (file.getAbsolutePath() + "/" + System.currentTimeMillis() + file_exts[currentFormat]); } private MediaRecorder.OnErrorListener errorListener = new MediaRecorder.OnErrorListener() { @Override public void onError(MediaRecorder mr, int what, int extra) { Toast.makeText(CallActivity.this, "Error: " + what + ", " + extra, Toast.LENGTH_SHORT).show(); } }; private MediaRecorder.OnInfoListener infoListener = new MediaRecorder.OnInfoListener() { @Override public void onInfo(MediaRecorder mr, int what, int extra) { Toast.makeText(CallActivity.this, "Warning: " + what + ", " + extra, Toast.LENGTH_SHORT) .show(); } };
//below part of code to make your device on speaker
audioManager = (AudioManager)getApplicationContext().getSystemService(Context.AUDIO_SERVICE); audioManager.setMode(AudioManager.MODE_IN_CALL); audioManager.setSpeakerphoneOn(true);
//below part of code to start recording
recorder = new MediaRecorder(); recorder.setAudioSource(MediaRecorder.AudioSource.MIC); recorder.setOutputFormat(output_formats[currentFormat]); //recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP); recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB); recorder.setOutputFile(getFilename()); recorder.setOnErrorListener(errorListener); recorder.setOnInfoListener(infoListener); try { recorder.prepare(); recorder.start(); } catch (IllegalStateException e) { Log.e("REDORDING :: ",e.getMessage()); e.printStackTrace(); } catch (IOException e) { Log.e("REDORDING :: ",e.getMessage()); e.printStackTrace(); }
//For stop recording and keep in mind to set speaker off while call end or stop
audioManager.setSpeakerphoneOn(false); try{ if (null != recorder) { recorder.stop(); recorder.reset(); recorder.release(); recorder = null; } }catch(RuntimeException stopException){ }
And give permission to manifest file,
<uses-permission android:name="android.permission.CALL_PHONE" /> <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" /> <uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS"/> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With