0

I am trying to record phone calls there is no error is showing in code but there is no recording in phone storage

I tried two ways and I am providing them here provide me a solution

First Application code::

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="ke.lesson1.MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</android.support.constraint.ConstraintLayout>

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="ke.lesson1">

    <uses-permission android:name="android.permission.READ_PHONE_STATE" />
    <uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.RECORD_AUDIO" />
    <uses-permission android:name="android.permission.STORAGE" />
    <uses-permission android:name="android.permission.READ_CALL_LOG"/>
    <uses-permission android:name="android.permission.READ_CONTACTS"/>





    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

MainActivity.java

public class MainActivity extends AppCompatActivity {

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

     //   Intent i  = new Intent(MainActivity.this, CallReceiver.class);
      //  startActivity(i);

    }

   /* @Override
    public void startActivity(Intent intent) {

        Intent i  = new Intent(MainActivity.this, CallReceiver.class);
        MainActivity.this.startActivity(i);
    }*/



    private static int lastState = TelephonyManager.CALL_STATE_IDLE;
    private static Date callStartTime;
    private static boolean isIncoming;
    private static String savedNumber;
    String voiceStoragePath;
    MediaRecorder mediaRecorder;


    public void onReceive(Context context, Intent intent) {
        //Log.w("intent " , intent.getAction().toString());

        if (intent.getAction().equals("android.intent.action.NEW_OUTGOING_CALL")) {
            savedNumber = intent.getExtras().getString("android.intent.extra.PHONE_NUMBER");

        }
        else{
            String stateStr = intent.getExtras().getString(TelephonyManager.EXTRA_STATE);
            String number = intent.getExtras().getString(TelephonyManager.EXTRA_INCOMING_NUMBER);
            int state = 0;
            if(stateStr.equals(TelephonyManager.EXTRA_STATE_IDLE)){
                state = TelephonyManager.CALL_STATE_IDLE;
            }
            else if(stateStr.equals(TelephonyManager.EXTRA_STATE_OFFHOOK)){
                state = TelephonyManager.CALL_STATE_OFFHOOK;
            }
            else if(stateStr.equals(TelephonyManager.EXTRA_STATE_RINGING)){
                state = TelephonyManager.CALL_STATE_RINGING;
            }

            onCallStateChanged(context, state, number);
        }
    }


    public void onCallStateChanged(Context context, int state, String number) {
        if(lastState == state){
            //No change, debounce extras
            return;
        }
        switch (state) {
            case TelephonyManager.CALL_STATE_RINGING:
                isIncoming = true;
                callStartTime = new Date();
                savedNumber = number;

                SimpleDateFormat sdfDate = new SimpleDateFormat("dd-MM-yyyy_HH:mm:ss");
                Date now = new Date();
                String strDate = sdfDate.format(now);

                voiceStoragePath = Environment.getExternalStorageDirectory().getAbsolutePath();
                File audioVoice = new File(voiceStoragePath + File.separator + "MyCall" + File.separator + "Incoming");
                if(!audioVoice.exists()){
                    audioVoice.mkdir();
                }
                voiceStoragePath = voiceStoragePath + File.separator + "MyCall" + File.separator + "Incoming/" + "mc_incall_"+ strDate + savedNumber + ".amr";
                System.out.println("Audio path : " + voiceStoragePath);




                MediaRecorderReady();

                try {
                    mediaRecorder.prepare();
                    mediaRecorder.start();
                } catch (IllegalStateException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }



                //  Toast.makeText(context, "Incoming Call Ringing" , Toast.LENGTH_SHORT).show();
                break;
            case TelephonyManager.CALL_STATE_OFFHOOK:
                //Transition of ringing->offhook are pickups of incoming calls.  Nothing done on them
                if(lastState != TelephonyManager.CALL_STATE_RINGING){
                    isIncoming = false;
                    callStartTime = new Date();

                    SimpleDateFormat sdffDate = new SimpleDateFormat("dd-MM-yyyy_HH:mm:ss");
                    Date noow = new Date();
                    String strrDate = sdffDate.format(noow);

                    voiceStoragePath = Environment.getExternalStorageDirectory().getAbsolutePath();
                    File audiooVoice = new File(voiceStoragePath + File.separator + "MyCall" + File.separator + "Outgoing");
                    if(!audiooVoice.exists()){
                        audiooVoice.mkdir();
                    }
                    voiceStoragePath = voiceStoragePath + File.separator + "MyCall" + File.separator + "Outgoing/" + "mc_outcall_"+ strrDate + savedNumber + ".amr";
                    System.out.println("Audio path : " + voiceStoragePath);




                    MediaRecorderReady();

                    try {
                        mediaRecorder.prepare();
                        mediaRecorder.start();
                    } catch (IllegalStateException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }



                    Toast.makeText(context, "Outgoing Call Started" , Toast.LENGTH_SHORT).show();
                }

                break;
            case TelephonyManager.CALL_STATE_IDLE:

                mediaRecorder.stop();

                //Went to idle-  this is the end of a call.  What type depends on previous state(s)
                if(lastState == TelephonyManager.CALL_STATE_RINGING){
                    //Ring but no pickup-  a miss
                    Toast.makeText(context, "Ringing but no pickup" + savedNumber + " Call time " + callStartTime +" Date " + new Date() , Toast.LENGTH_SHORT).show();
                }
                else if(isIncoming){

                    Toast.makeText(context, "Incoming " + savedNumber + " Call time " + callStartTime  , Toast.LENGTH_SHORT).show();
                }
                else{

                    Toast.makeText(context, "outgoing " + savedNumber + " Call time " + callStartTime +" Date " + new Date() , Toast.LENGTH_SHORT).show();

                }

                break;
        }
        lastState = state;
    }

    public void MediaRecorderReady(){
        mediaRecorder=new MediaRecorder();
        mediaRecorder.setAudioSource(MediaRecorder.AudioSource.VOICE_COMMUNICATION);
        mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
        mediaRecorder.setAudioEncoder(MediaRecorder.OutputFormat.AMR_NB);
        mediaRecorder.setOutputFile(voiceStoragePath);
    }




}

provide me a solution for this......

Second Application is Same activity_main.xml and AndroidManifest.xml

MainActivity.java

public class MainActivity extends Activity {

    String voiceStoragePath;
    private static String savedNumber;
    MediaRecorder mediaRecorder;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        TelephonyManager telephonyManager =
                (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);

        PhoneStateListener callStateListener = new PhoneStateListener() {
            public void onCallStateChanged(int state, String incomingNumber)
            {
                if(state==TelephonyManager.CALL_STATE_RINGING){
                    Toast.makeText(getApplicationContext(),"Phone Is Riging",
                            Toast.LENGTH_LONG).show();
                }
                if(state==TelephonyManager.CALL_STATE_OFFHOOK){



                    SimpleDateFormat sdfDate = new SimpleDateFormat("dd-MM-yyyy_HH:mm:ss");
                    Date now = new Date();
                    String strDate = sdfDate.format(now);

                    voiceStoragePath = Environment.getExternalStorageDirectory().getAbsolutePath();
                    File audioVoice = new File(voiceStoragePath + File.separator + "MyCall" + File.separator + "Incoming");
                    if(!audioVoice.exists()){
                        audioVoice.mkdir();
                    }
                    voiceStoragePath = voiceStoragePath + File.separator + "MyCall" + File.separator + "Incoming/" + "mc_incall_"+ strDate + savedNumber + ".amr";
                    System.out.println("Audio path : " + voiceStoragePath);




                    mediaRecorder=new MediaRecorder();
                    mediaRecorder.setAudioSource(MediaRecorder.AudioSource.VOICE_COMMUNICATION);
                    mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
                    mediaRecorder.setAudioEncoder(MediaRecorder.OutputFormat.AMR_NB);
                    mediaRecorder.setOutputFile(voiceStoragePath);

                    try {
                        mediaRecorder.prepare();
                        mediaRecorder.start();
                    } catch (IllegalStateException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }



                    Toast.makeText(getApplicationContext(),"Phone is Currently in A call",
                            Toast.LENGTH_LONG).show();
                }

                if(state==TelephonyManager.CALL_STATE_IDLE){
                    Toast.makeText(getApplicationContext(),"phone is neither ringing nor in a call",
                            Toast.LENGTH_LONG).show();
                }
            }
        };
        telephonyManager.listen(callStateListener,PhoneStateListener.LISTEN_CALL_STATE);

    }

}
  • tell me what I've done wrong – krishna hks Dec 05 '17 at 13:35
  • please provide me a soultion – krishna hks Dec 05 '17 at 13:35
  • Basically this can't be done on an unrooted modern Android system for security reasons - there are good reasons take a look at https://stackoverflow.com/questions/18887636/how-to-record-phone-calls-in-android and try searching around this issue in stack oveflow – Elemental Dec 05 '17 at 14:09
  • 2
    Possible duplicate of [How to record phone calls in android?](https://stackoverflow.com/questions/18887636/how-to-record-phone-calls-in-android) – Elemental Dec 05 '17 at 14:09
  • I think most of you answering to increasing your reputation only not to solve the problem – krishna hks Dec 06 '17 at 07:16

0 Answers0