0

I am trying to store incoming and outgoing calls record in my SD Card. but the issue is that i am not able to find that in my SD Card. Following is my Code Can any one help

DeviceAdminDemo.java

public class DeviceAdminDemo extends DeviceAdminReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        super.onReceive(context, intent);

        context.stopService(new Intent(context, TService.class));
        Intent myIntent = new Intent(context, TService.class);
        context.startService(myIntent);

    }

    public void onEnabled(Context context, Intent intent) {
    };

    public void onDisabled(Context context, Intent intent) {
    };
}

TService.java

public class TService extends Service {
    MediaRecorder recorder;
    File audiofile;
    String name, phonenumber;
    String audio_format;
    public String Audio_Type;
    int audioSource;
    Context context;

    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();
                            System.out.println("PATH"+path);

                            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();
                }
            }
        }
    }

}

1 Answer 1

2

UPDATED ANSWER

Main2Activity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main2);
}

ListenIncomingCalls.java

public class ListenIncomingCalls extends BroadcastReceiver {

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

        TelephonyManager tmgr = (TelephonyManager) context
                .getSystemService(Context.TELEPHONY_SERVICE);
        CallListener phoneListener = new CallListener();
        tmgr.listen(phoneListener, PhoneStateListener.LISTEN_CALL_STATE);

    }
}

class CallListener extends PhoneStateListener {

    @Override
    public void onCallStateChanged(int state, String incomingNumber) {
        super.onCallStateChanged(state, incomingNumber);
        System.out.println("Call state changed ");
        if (state == TelephonyManager.CALL_STATE_RINGING) {
            //phone ringing
            System.out.println("This number is " + incomingNumber);
        }
        if(state == TelephonyManager.CALL_STATE_OFFHOOK)
        {
            System.out.println("Call is picked ");
            //start CallRecordService Here to start recording
        }
    }


}

CallRecordService.java

public class CallRecordService extends Service {

    MRecorder mRecorder;
    long passedSeconds = 0;
    NotificationCompat.Builder mBuilder;
    NotificationManager mNotificationManager;

    public CallRecordService()
    {
        mRecorder = MRecorder.getInstance();
    }




    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        System.out.println("Service binded");
        return null;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        System.out.println("Service called ");

        mRecorder.startRecording();

        Intent stopService = new Intent(this,StopPhoneReciever.class);
        stopService.putExtra("stopRecording",true);
        PendingIntent pendingIntent = PendingIntent.getService(getApplicationContext(),68,stopService,PendingIntent.FLAG_UPDATE_CURRENT);
        mBuilder = (NotificationCompat.Builder) new NotificationCompat.Builder(this)
                .setSmallIcon(R.mipmap.ic_launcher)
                .setOngoing(true)
                .setContentIntent(pendingIntent)
                .setContentTitle("Recording Call...")
                .setContentText("Tap to stop recording.....");
        Notification notification = mBuilder.build();
        notification.flags = Notification.FLAG_AUTO_CANCEL;
        NotificationManager mNotificationManager =
                (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        mNotificationManager.notify(68,notification);
    }

    @Override
    public void onDestroy() {
        mRecorder.stopRecording();
        super.onDestroy();
    }

}

Manifest.xml

 <!--incoming calls receiver -->
        <receiver android:name=".recorder.ListenIncomingCalls">
            <intent-filter>
                <action android:name="android.intent.action.PHONE_STATE"/>
            </intent-filter>
        </receiver>
<service android:name=".recorder.CallRecordService" />

Few useful links
1)https://www.codeproject.com/Articles/548416/Detecting-incoming-and-outgoing-phone-calls-on-And
2)http://androidexample.com/Incomming_Phone_Call_Broadcast_Receiver__-_Android_Example/index.php?view=article_discription&aid=61 Hope this helps:)

13
  • thanks ayush.. you are right folder is showing in my phone..but the issue is that sometimes it works well but sometimes not. can you help with that
    – chris
    Mar 8, 2017 at 10:20
  • @chris It wont record if the phone call is not going on ....mrecorder.setAudioSource(MediaRecorder.AudioSource.VOICE_CALL) because the source is VOICE_CALL. Please post the log for the cases not working. :) Mar 8, 2017 at 10:28
  • my app is not crashing..what i want is whenever user calls or get calls recording should start at same time..i do not want notification
    – chris
    Mar 8, 2017 at 10:32
  • my code works fine..now the issue is that it does not save outgoingcalls. i follw this stackoverflow.com/questions/18887636/…
    – chris
    Mar 8, 2017 at 11:08
  • Did you try codeproject.com/Articles/548416/… Mar 9, 2017 at 5:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.