Search code examples
androidaudio-recordingvoice-recording

How to start something when voice call starts?


I have built full voice recorder application.

I would like to start recording when a voice call starts on the phone, how can I detect the Calls state? tried some code and it didn't work for me.

I just need to know hot to start recording when a voice call starts (incoming and outgoing).


Solution

  • Here is an example of what you need.

    Declare receiver in AndroidManifest

    <receiver android:name=".IncomingCall">   
                <intent-filter>
                   <action android:name="android.intent.action.PHONE_STATE" />
                </intent-filter>
        </receiver>
    

    Give read phone state permission in AndroidManifest

    <uses-permission android:name="android.permission.READ_PHONE_STATE"/>
    

    Create a class IncomingCall with extends BroadcastReceiver class

    import android.content.BroadcastReceiver;
    import android.content.Context;
    import android.content.Intent;
    import android.telephony.PhoneStateListener;
    import android.telephony.TelephonyManager;
    import android.util.Log;
    import android.widget.Toast;
    
    /**
     * Created by matheszabi on Aug/20/2017 0020.
     */
    
    public class IncomingCall extends BroadcastReceiver {
    
        private Context context;
    
        public void onReceive(Context context, Intent intent) {
    
            this.context = context;
            try {
                // TELEPHONY MANAGER class object to register one listner
                TelephonyManager tmgr = (TelephonyManager) context
                        .getSystemService(Context.TELEPHONY_SERVICE);
    
                //Create Listner
                MyPhoneStateListener PhoneListener = new MyPhoneStateListener();
    
                // Register listener for LISTEN_CALL_STATE
                tmgr.listen(PhoneListener, PhoneStateListener.LISTEN_CALL_STATE);
    
            } catch (Exception e) {
                Log.e("Phone Receive Error", " " + e);
            }
    
        }
    
        private class MyPhoneStateListener extends PhoneStateListener {
    
            public void onCallStateChanged(int state, String incomingNumber) {
    
                Log.d("MyPhoneListener",state+"   incoming no:"+incomingNumber);
    
                if (state == 1) {
    
                    String msg = "New Phone Call Event. Incomming Number : "+incomingNumber;
                    int duration = Toast.LENGTH_LONG;
                    Toast toast = Toast.makeText(context, msg, duration);
                    toast.show();
    
                }
            }
        }
    }
    

    Above Android 6.0 you need to handle a bit different the permissions:

    import android.Manifest;
    import android.content.pm.PackageManager;
    import android.os.Bundle;
    import android.support.v7.app.AppCompatActivity;
    import android.widget.Toast;
    
    public class MainActivity extends AppCompatActivity {
    
        private static final int MY_REQUEST_CODE = 1234;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
        }
    
    
    
        @Override
        protected void onResume() {
            super.onResume();
    
            if (checkSelfPermission(Manifest.permission.READ_PHONE_STATE)
                    != PackageManager.PERMISSION_GRANTED) {
    
                requestPermissions(new String[]{Manifest.permission.READ_PHONE_STATE},
                        MY_REQUEST_CODE);
            }
        }
    
    
        public void onRequestPermissionResult(int requestCode, String[] permissions, int[] grantResults) {
            if (requestCode == MY_REQUEST_CODE) {
                if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    // Now user should be able to use camera
                    Toast.makeText(this, "I have  access", Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(this, "I DON'T have  access", Toast.LENGTH_SHORT).show();
                }
    
    
            }
        }
    }
    

    You must allow the permissions at the first time run:

    allow permission

    Here is the screenshot of the working code:

    phone ring