title: Automatic voice listener used in mic and speaker in background services
body: <p>I'm developing an app which have the feature of automatic voice listener if app also in background service, if user use mic at any point, need to record the voice and to save in Internal storage</p>
  
  <p>I have tried record mic when app is running it's working, but closing app its not working or using mic I have sent voice message, it also not saving in Internal storage</p>
  
  <p>(<a href="https://developer.android.com/guide/topics/media/mediarecorder#java" rel="nofollow noreferrer">https://developer.android.com/guide/topics/media/mediarecorder#java</a>), </p>
  
  <p>I have referred to this example for record mic</p>
  
  <p>My code:</p>
  
  <pre><code>public class MainActivity extends AppCompatActivity {
  
  AudioManager audioManager;
  private static final String LOG_TAG = "AudioRecordTest";
  private static final int REQUEST_RECORD_AUDIO_PERMISSION = 200;
  private static String mFileName=null;
  private MediaRecorder mRecorder = null;
  public static final int REQUEST_CODE = 1234;
  
  //Requesting permission
  private boolean permissionToRecordAccepted = false;
  private String [] permissions = {Manifest.permission.RECORD_AUDIO};
  
  @Override
  protected void onCreate(Bundle savedInstanceState){
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
  
      mFileName = getExternalCacheDir().getAbsolutePath();
      mFileName+= "/audiorecordtest.mp3";
      ActivityCompat.requestPermissions(this,permissions,REQUEST_RECORD_AUDIO_PERMISSION);
  
      startRecording();
  
      audioManager = (AudioManager)getApplicationContext().getSystemService(MainActivity.AUDIO_SERVICE);
      audioManager.setMode(AudioManager.MODE_IN_CALL);
      audioManager.setSpeakerphoneOn(true);
      audioManager.setSpeakerphoneOn(false);
  
      try{
          if (null != mRecorder) {
              mRecorder.stop();
              mRecorder.reset();
              mRecorder.release();
  
              mRecorder = null;
          }
      }catch(RuntimeException stopException){
  
      }
  
     }
  
     private void startRecording(){
  
      mRecorder=new MediaRecorder();
      mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
      mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
      mRecorder.setOutputFile(mFileName);
      mRecorder.setOnErrorListener(errorListener);
      mRecorder.setOnInfoListener(infoListener);
      mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
  
      try {
          mRecorder.prepare();
          mRecorder.start();
          Toast.makeText(this, "Workingg Da", 
      Toast.LENGTH_SHORT).show();
          Log.v(LOG_TAG,"Working");
      } catch (IllegalStateException e){
          Log.v(LOG_TAG,"IllegalState Exception");
      } catch (IOException e){
          Log.v(LOG_TAG,"prepare()failed");
          Toast.makeText(this, "Failed Recording", 
   Toast.LENGTH_SHORT).show();
      }
     }
  
  @Override
  public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
      super.onRequestPermissionsResult(requestCode, permissions, grantResults);
      switch (requestCode){
          case REQUEST_RECORD_AUDIO_PERMISSION:
              permissionToRecordAccepted  = grantResults[0] == PackageManager.PERMISSION_GRANTED;
              break;
      }
      if (!permissionToRecordAccepted ) finish();
  
  }
  
  private MediaRecorder.OnErrorListener errorListener = new MediaRecorder.OnErrorListener() {
      @Override
      public void onError(MediaRecorder mr, int what, int extra) {
          Toast.makeText(MainActivity.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(MainActivity.this,
                  "Warning: " + what + ", " + extra, Toast.LENGTH_SHORT)
                  .show();
      }
  };
  </code></pre>
  
  <p>I expect the output, when user using mic or speaker need to record, even the app is closed and store it in internal storage, any listener method kindly post here, it will help me to complete.</p>
  
  <p>Thanks Friends  </p>